首页 > 代码库 > 李洪强iOS开发之静态库的打包一
李洪强iOS开发之静态库的打包一
李洪强iOS开发之静态库的打包一
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } span.s1 { font: 18.0px Menlo } span.s2 { } span.s3 { color: #000000 } span.s4 { font: 18.0px "PingFang SC" }</style>//静态库一般做一下几种事情
//1 工具类 算法逻辑
新建工具类LHQTools
定义类方法
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } span.s1 { } span.s2 { color: #703daa }</style>+ (NSInteger)sumWithNum1: (NSInteger)num1 andNum2:(NSInteger)num2;
类方法的实现
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } span.s1 { } span.s2 { color: #703daa } span.s3 { color: #c42275 }</style>+(NSInteger)sumWithNum1:(NSInteger)num1 andNum2:(NSInteger)num2{
return num1 + num2;
}
使用
在主控制器计算值
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #294c50 } span.s1 { color: #000000 } span.s2 { color: #3e1e81 } span.s3 { color: #c81b13 } span.s4 { color: #c42275 } span.s5 { color: #3c828b } span.s6 { } span.s7 { color: #0435ff }</style>NSLog(@"%ld",(long)[LHQTools sumWithNum1:10 andNum2:10]);
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } span.s1 { font: 18.0px Menlo } span.s2 { }</style>
//2 实现加载一定的资源,放在bundle中避免资源重名
将存放图片的bundle拖入文件夹
定义类方法
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } span.s1 { } span.s2 { color: #6122ae }</style>+ (UIImage *)loadLogo;
实现类方法
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } span.s1 { } span.s2 { color: #6122ae } span.s3 { color: #000000 } span.s4 { font: 18.0px "PingFang SC" } span.s5 { color: #c42275 } span.s6 { color: #3e1e81 }</style>+(UIImage *)loadLogo{
//把图片封装到bundle里面
return [UIImage imageNamed:@"CZTools.bundle/logo.png"];
}
来到主控制器中使用
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } span.s1 { color: #6122ae } span.s2 { } span.s3 { color: #3e1e81 } span.s4 { color: #0435ff } span.s5 { color: #703daa } span.s6 { color: #3c828b } span.s7 { color: #294c50 } span.s8 { color: #c42275 }</style>UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView1.image = [LHQTools loadLogo];
[self.view addSubview:imageView1];
这个时候,运行程序,会显示这张图片
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } span.s1 { color: #000000 } span.s2 { } span.s3 { font: 18.0px "PingFang SC" }</style>
//3 封装视图
新建继承自UIView的类
定义类方法
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275 } span.s1 { color: #822d0f } span.s2 { } span.s3 { color: #c42275 } span.s4 { color: #6122ae } span.s5 { color: #703daa }</style>#import <UIKit/UIKit.h>
@interface LHQDemoView : UIView
- (instancetype)initWithFrame:(CGRect)frame andCompelete:(void(^)(NSString *msg))block;
@end
实现类方法
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3c828b } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421; min-height: 21.0px } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } p.p10 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } span.s1 { color: #822d0f } span.s2 { } span.s3 { color: #c42275 } span.s4 { color: #000000 } span.s5 { font: 18.0px "PingFang SC" } span.s6 { font: 18.0px Menlo } span.s7 { color: #6122ae } span.s8 { color: #703daa } span.s9 { color: #3e1e81 } span.s10 { color: #0435ff } span.s11 { color: #c81b13 } span.s12 { font: 18.0px "PingFang SC"; color: #c81b13 } span.s13 { color: #539aa4 }</style>
#import "LHQDemoView.h"
@interface LHQDemoView()
//block定义的时候一定要用copy
/*
block默认在栈中 栈中内存归系统管理
系统管理有个弊端:到作用于结束就被干掉
执行了一个copy操作之后,就会把block从栈中放到堆中
会自动有一个强引用来指向它
*/
@property(nonatomic,copy)void(^block)(NSString *);
@end
@implementation LHQDemoView
- (instancetype)initWithFrame:(CGRect)frame andCompelete:(void(^)(NSString *msg))block{
if(self = [super initWithFrame:frame]){
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[btn setTitle:@"提示" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
self.block = block;
}
return self;
}
- (void)btnClicked: (UIButton *)btn{
self.block(@"点击了某个按钮");
NSLog(@"btnClicked");
}
- (void)drawRect:(CGRect)rect{
//画一个圆
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
[[UIColor redColor]setFill];
// [path stroke];
[path fill];
}
来到主控制器中调用:
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } span.s1 { } span.s2 { font: 18.0px "PingFang SC" } span.s3 { color: #3c828b } span.s4 { color: #3e1e81 } span.s5 { color: #294c50 } span.s6 { color: #0435ff } span.s7 { color: #6122ae } span.s8 { color: #000000 } span.s9 { color: #c81b13 } span.s10 { font: 18.0px "PingFang SC"; color: #c81b13 } span.s11 { color: #c42275 } span.s12 { color: #703daa }</style>//3 封装视图
LHQDemoView *demoView = [[LHQDemoView alloc]initWithFrame:CGRectMake(100, 200, 100, 100) andCompelete:^(NSString *msg) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[alert show];
}];
[self.view addSubview:demoView];
效果:
李洪强iOS开发之静态库的打包一