李洪強iOS開發之靜態庫的打包一

來源:互聯網
上載者:User

標籤: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開發之靜態庫的打包一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.