导出功能类
- 创建项目选择
Framework
- 创建功能类
@interface ShowLog : NSObject
+ (void)log;
@end
+ (void)log{
NSLog(@"来自framework中的一条log");
}
- 配置项目
-
设置frameWork支持的架构
image.png
-
设置Framework支持的系统版本
image.png
-
选择Static Library
image.png
- 暴露
.h
将要暴露的.h
拖入到Public
中
image.png
- 将所有要暴露的
.h
都导入到TestFramework.h
image.png
- 生成
framework
- 选择任意模拟器,
command + b
,生成 模拟器 使用的framework
- 选择
Generic iOS Device
,command + b
,生成 真机 使用的framework
- 合并
framework
lipo -create 真机路径 模拟器路径 -output 输出路径
- 将得到的文件 拷贝 到
真机路径
下,选择替换,得到最终的framework
image.png
- 使用
-
拷贝到项目中
image.png
#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+三方库
pod init
- 添加三方库
# 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
pod install
- 创建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
-
创建
Source.bundle
,将图片拉进去,在此删除
image.png
-
得到
framework
和Source.bundle
-
拷贝到测试项目中,测试项目需要
pod init
,安装和framework
一样的库
MyInfoView *infoView = [ShowLog getMyInfoViewWithDataSource:@{
@"name":@"张三"
}];
infoView.backgroundColor = [UIColor redColor];
[self.view addSubview:infoView];
网友评论