
本文共 2841 字,大约阅读时间需要 9 分钟。
IOS������������������������������
���������������������������FOX���������������������������������������������������������������������������������������������������������������iOS������������������������������UI���������������
Project Overview
���������������������������������������������������������������������������������������������������������������������������������������������������������������������NSOperationQueue������������������������������UI���������������������������������������
ViewController Setup
���Xcode������������������������ViewController������������������IBOutlet���IBAction���
@interface ViewController ()@property (weak, nonatomic) IBOutlet UILabel *lbl1;@property (weak, nonatomic) IBOutlet UILabel *lbl2;@property (weak, nonatomic) IBOutlet UILabel *lbl3;@property (IBAction) start:(id)sender;@property (weak, nonatomic) IBOutlet UIButton *startButton;@property (nonatomic, strong) NSOperationQueue *queue;@end
@implementation ViewController- (NSOperationQueue *)queue{ if (_queue == nil) { _queue = [[NSOperationQueue alloc] init]; _queue.maxConcurrentOperationCount = 2; } return _queue;}
���viewDidLoad
���������������������������������������������
- (void)viewDidLoad{ [super viewDidLoad]; self.startButton.backgroundColor = [UIColor grayColor];}
Button Click Handler
������������������������������������������������������������������������������
- (IBAction)start:(UIButton *)sender{ if (self.queue.operationCount == 0) { [self.queue addOperationWithBlock:^{ [self random]; }]; self.startButton.backgroundColor = [UIColor orangeColor]; self.queue.suspended = NO; } else if (!self.queue.isSuspended) { self.queue.suspended = YES; self.startButton.backgroundColor = [UIColor blueColor]; }}
###TouchEvent Handler������������������������������������������������
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{ NSLog(@"������������%zd", self.queue.operationCount);}
###RandomNumber Generation������������������������UI���
- (void)random{ while (!self.queue.isSuspended) { [NSThread sleepForTimeInterval:0.05]; int num1 = arc4random_uniform(10); int num2 = arc4random_uniform(10); int num3 = arc4random_uniform(10); [[[NSOperationQueue mainQueue] addOperationWithBlock:^{ self.lbl1.text = [NSString stringWithFormat:@"%d", num1]; self.lbl2.text = [NSString stringWithFormat:@"%d", num2]; self.lbl3.text = [NSString stringWithFormat:@"%d", num3]; }]]; }}
���������������������������������NSOperationQueue���������������������������������������UI������������������������������������������������������������������������������������������������������������UI���������������������������������������
发表评论
最新留言
关于作者
