標籤:nts get uikit oval 載入 copy add lin orm
李洪強iOS開發之靜態庫的打包一
//靜態庫一般做一下幾種事情
//1 工具類 演算法邏輯
建立工具類LHQTools
定義類方法
+ (NSInteger)sumWithNum1: (NSInteger)num1 andNum2:(NSInteger)num2;
類方法的實現
+(NSInteger)sumWithNum1:(NSInteger)num1 andNum2:(NSInteger)num2{
return num1 + num2;
}
使用
在主控制器計算值
NSLog(@"%ld",(long)[LHQTools sumWithNum1:10 andNum2:10]);
//2 實現載入一定的資源,放在bundle中避免資源重名
將存放圖片的bundle拖入檔案夾
定義類方法
+ (UIImage *)loadLogo;
實作類別方法
+(UIImage *)loadLogo{
//把圖片封裝到bundle裡面
return [UIImage imageNamed:@"CZTools.bundle/logo.png"];
}
來到主控制器中使用
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView1.image = [LHQTools loadLogo];
[self.view addSubview:imageView1];
這個時候,運行程式,會顯示這張圖片
//3 封裝視圖
建立繼承自UIView的類
定義類方法
#import <UIKit/UIKit.h>
@interface LHQDemoView : UIView
- (instancetype)initWithFrame:(CGRect)frame andCompelete:(void(^)(NSString *msg))block;
@end
實作類別方法
#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];
}
來到主控制器中調用:
//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開發之靜態庫的打包一