GCD一次性代码

本文深入探讨了ViewController类的方法实现,包括初始化、视图加载、触摸事件处理等,并通过实例代码展示了如何在实际场景中应用这些方法。
@interface ViewController ()

@property (nonatomic,assign) BOOL hasExecuted;
@property (nonatomic,strong) ImageDownloader *imageDownloader;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
//    self.imageDownloader = [[ImageDownloader alloc] init];
   
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    /*
    if (self.hasExecuted) {
        return;
    }
    
    NSLog(@"下载图片");
    self.hasExecuted = YES;
     */
//    ImageDownloader *imageDownloader = [[ImageDownloader alloc] init];
//    [imageDownloader download];
//    [self.imageDownloader download];
   
    NSLog(@"touchBegin");
    static dispatch_once_t onceToken;
     // 保证只执行一次
    dispatch_once(&onceToken, ^{
        NSLog(@"once");
    });
    
    NSLog(@"touchEnd");
    
    /** 点击两次
     *  2015-08-10 17:26:23.219 10 - 一次性代码[5583:156692] touchBegin
     2015-08-10 17:26:23.221 10 - 一次性代码[5583:156692] once
     2015-08-10 17:26:23.221 10 - 一次性代码[5583:156692] touchEnd
     2015-08-10 17:26:23.963 10 - 一次性代码[5583:156692] touchBegin
     2015-08-10 17:26:23.963 10 - 一次性代码[5583:156692] touchEnd
     */
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值