iOS 制作framework(功能类、资源+View+三方库)

2020-07-24  本文已影响0人  精神病患者link常

导出功能类

  1. 创建项目选择 Framework
  2. 创建功能类
@interface ShowLog : NSObject
+ (void)log;
@end

+ (void)log{
    NSLog(@"来自framework中的一条log");
}
  1. 配置项目
  1. 生成framework
  1. 合并framework
  1. 使用
#import "ViewController.h"
#import <TestFramework/TestFramework.h>
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [ShowLog log];
}


@end

导出资源+View+三方库

  1. pod init
  2. 添加三方库
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'TestFramework' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for TestFramework
  pod 'SDWebImage'
  pod 'AFNetworking'    
  pod 'Masonry'
  pod 'IQKeyboardManager'
  pod 'MBProgressHUD'

  target 'TestFrameworkTests' do
    # Pods for testing
  end
end

  1. pod install
  2. 创建View
//
//  MyInfoView.h
//  TestFramework
//
//  Created by big-dog on 2020/7/24.
//  Copyright © 2020 big-dog. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyInfoView : UIView

@property(nonatomic,strong)NSDictionary *dataSource;

@end

NS_ASSUME_NONNULL_END

//
//  MyInfoView.m
//  TestFramework
//
//  Created by big-dog on 2020/7/24.
//  Copyright © 2020 big-dog. All rights reserved.
//

#import "MyInfoView.h"
#import "UIImageView+WebCache.h"

@interface MyInfoView ()

@property (nonatomic,strong) UIImageView *headerImageView;
@property (nonatomic,strong) UIImageView *webImageView;
@property (nonatomic,strong) UILabel *titleLabel;

@end

@implementation MyInfoView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        [self addSubview:self.headerImageView];
        [self addSubview:self.webImageView];
        [self addSubview:self.titleLabel];
    }
    return self;
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.headerImageView.frame = CGRectMake(0, 0, 100, 100);
    self.webImageView.frame = CGRectMake(0, 110, 100, 100);
    self.titleLabel.frame = CGRectMake(0, 220, 100, 20);
}

- (void)setDataSource:(NSDictionary *)dataSource {
    _dataSource = dataSource;
    [self.webImageView sd_setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595592232821&di=ce5ee664c70a8eae7d5ef8ef47638691&imgtype=0&src=http%3A%2F%2Ft8.baidu.com%2Fit%2Fu%3D1484500186%2C1503043093%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1280%26h%3D853"]];
    self.headerImageView.image = [UIImage imageNamed:@"Source.bundle/iamge.jpg"];
    self.titleLabel.text = dataSource[@"name"];
    
}

- (UIImageView *)headerImageView {
    if(_headerImageView == nil){
        _headerImageView = [[UIImageView alloc]init];
    }
    return _headerImageView;
}

- (UIImageView *)webImageView {
    if (_webImageView == nil){
        _webImageView = [[UIImageView alloc]init];
    }
    return _webImageView;
}

- (UILabel *)titleLabel {
    if (_titleLabel == nil){
        _titleLabel = [[UILabel alloc]init];
    }
    return _titleLabel;
}





@end

5.创建类 导出 View


#import <Foundation/Foundation.h>
#import "MyInfoView.h"

NS_ASSUME_NONNULL_BEGIN

@interface ShowLog : NSObject

+ (void)log;

+ (MyInfoView *)getMyInfoViewWithDataSource:(NSDictionary *)data;

@end

NS_ASSUME_NONNULL_END

#import "ShowLog.h"
@implementation ShowLog

+ (void)log{
    NSLog(@"来自framework中的一条log");
}

+ (MyInfoView *)getMyInfoViewWithDataSource:(NSDictionary *)data{
    MyInfoView *infoView = [[MyInfoView alloc]initWithFrame:CGRectMake(50, 100, 200, 500)];
    [infoView setDataSource:data];
    return infoView;
}

@end
  1. 创建 Source.bundle,将图片拉进去,在此删除

    image.png
  2. 得到 frameworkSource.bundle

  3. 拷贝到测试项目中,测试项目需要 pod init ,安装和framework 一样的库

 MyInfoView *infoView = [ShowLog getMyInfoViewWithDataSource:@{
        @"name":@"张三"
    }];
    infoView.backgroundColor = [UIColor redColor];
    [self.view addSubview:infoView];
上一篇 下一篇

猜你喜欢

热点阅读