IOS的pch文件,NSTimer定时器,运行消息循环,随机色使用
IOS的pch文件,NSTimer定時(shí)器,運(yùn)行消息循環(huán),隨機(jī)顏色獲取使用
xcode新創(chuàng)建一個(gè)項(xiàng)目命名pchTest
右鍵創(chuàng)建文件,選擇other 下面的pch,名字不要管默認(rèn)就好
選中項(xiàng)目 : Buding Setting 搜索框輸入 Prefix
Prefix Header 雙擊一下,有個(gè)模態(tài)彈框,直接把pch往里面拖,xocde軟件會(huì)自動(dòng)讀取到文件的路徑。直接運(yùn)行就好了
Precompile Prefix Header 默認(rèn)是NO,設(shè)置成YES,可以提高預(yù)編譯速度,相當(dāng)于把pch文件的代碼加入緩存里面。
storyBoard拖入一個(gè)按鈕. 拖動(dòng)到點(diǎn)m文件里面,給action起個(gè)名字,我起成stop
PCH文件默認(rèn)的全部刪掉加入
#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))
這是隨機(jī)顏色
ViewController.m關(guān)鍵性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
點(diǎn)擊屏幕可以改變屏幕顏色,以及重復(fù)時(shí)間設(shè)置,點(diǎn)擊按鈕可以停掉定時(shí)器。
總結(jié)
以上是生活随笔為你收集整理的IOS的pch文件,NSTimer定时器,运行消息循环,随机色使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FPGA数据传输模块设计
- 下一篇: 最少编码原则