IOS的pch文件,NSTimer定时器,运行消息循环,随机色使用
IOS的pch文件,NSTimer定時器,運行消息循環,隨機顏色獲取使用
xcode新創建一個項目命名pchTest
右鍵創建文件,選擇other 下面的pch,名字不要管默認就好
選中項目 : Buding Setting 搜索框輸入 Prefix
Prefix Header 雙擊一下,有個模態彈框,直接把pch往里面拖,xocde軟件會自動讀取到文件的路徑。直接運行就好了
Precompile Prefix Header 默認是NO,設置成YES,可以提高預編譯速度,相當于把pch文件的代碼加入緩存里面。
storyBoard拖入一個按鈕. 拖動到點m文件里面,給action起個名字,我起成stop
PCH文件默認的全部刪掉加入
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#define AHLJColor(r,g,b) [UIColor colorWithRed:?/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define AHLJRandomColor AHLJColor(arc4random_uniform(256),arc4random_uniform(256),arc4random_uniform(256))
這是隨機顏色
ViewController.m關鍵性code
#import “ViewController.h”
@interface ViewController ()
@property(nonatomic,weak)NSTimer *ti;
@end
@implementation ViewController
- (IBAction)stop:(id)sender {
[self.ti invalidate];
} - (void)viewDidLoad {
[super viewDidLoad];
self.title = demo;
self.view.backgroundColor = AHLJColor(144, 144, 122);
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeBg) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
self.ti = timer;
}
-(void)changeBg{
self.view.backgroundColor = AHLJRandomColor;
}
@end
點擊屏幕可以改變屏幕顏色,以及重復時間設置,點擊按鈕可以停掉定時器。
總結
以上是生活随笔為你收集整理的IOS的pch文件,NSTimer定时器,运行消息循环,随机色使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FPGA数据传输模块设计
- 下一篇: 最少编码原则