IOS开发基础之摇奖机案例
发布日期:2021-05-11 08:38:33 浏览次数:12 分类:精选文章

本文共 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���������������������������������������

上一篇:IOS基础NSOperation的操作优先级和操作依赖
下一篇:IOS开发基础之socket的使用

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月07日 16时19分09秒