Block(塊)介紹之五:系統(tǒng)框架中的Block

pple定義的系統(tǒng)框架的API中,對(duì)Block的使用也比較集中,主要在動(dòng)畫、通知等等幾個(gè)方面,因此,對(duì)于普通開發(fā)者來(lái)說(shuō),重點(diǎn)掌握系統(tǒng)框架API中Block的使用也是比較有必要的。

1、系統(tǒng)框架API中的Block

在iOS4以后,越來(lái)越多的系統(tǒng)級(jí)的API在使用Block。蘋果對(duì)于Block的使用主要集中在如下幾個(gè)方面:

完成處理–Completion Handlers

通知處理–Notification Handlers

錯(cuò)誤處理–Error Handlers

枚舉–Enumeration

動(dòng)畫與形變–View Animation and Transitions

分類–Sorting

線程管理:GCD/NSOperation

接下來(lái),給大家介紹幾個(gè)常用的。

2、動(dòng)畫與形變

在UIView類的定義中,提供了若干個(gè)包含Block參數(shù)的方法,用來(lái)設(shè)置動(dòng)畫,例如修改View的大小、位置、透明度等等。

@interfaceUIView(UIViewAnimationWithBlocks)

+(void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void(^)(void))animations completion:(void(^__nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

+(void)animateWithDuration:(NSTimeInterval)duration animations:(void(^)(void))animations completion:(void(^__nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);// delay = 0.0, options = 0

+(void)animateWithDuration:(NSTimeInterval)duration animations:(void(^)(void))animations NS_AVAILABLE_IOS(4_0);// delay = 0.0, options = 0, completion = NULL

+(void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void(^)(void))animations completion:(void(^__nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);

+(void)transitionWithView:(UIView*)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void(^__nullable)(void))animations completion:(void(^__nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

+(void)transitionFromView:(UIView*)fromView toView:(UIView*)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void(^__nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);// toView added to fromView.superview, fromView removed from its superview

+(void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray*)views options:(UIViewAnimationOptions)options animations:(void(^__nullable)(void))parallelAnimations completion:(void(^__nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);

@end

示例:

[UIViewanimateWithDuration:0.2animations:^{

view.alpha=0.0;

}completion:^(BOOL finished){

[view removeFromSuperview];

}];

3、完成與錯(cuò)誤處理

完成處理Block在某個(gè)動(dòng)作完成后,通過回調(diào)的方式進(jìn)行執(zhí)行。錯(cuò)誤處理Block會(huì)在執(zhí)行某個(gè)操作時(shí),假如發(fā)生錯(cuò)誤而被執(zhí)行。

-(IBAction)pressBtn:(id)sender{

CGRectcacheFrame=self.imageView.frame;

[UIViewanimateWithDuration:1.5animations:^{//播放動(dòng)畫Block

CGRectnewFrame=self.imageView.frame;

newFrame.origin.y=newFrame.origin.y+150.0;

self.imageView.frame=newFrame;

self.imageView.alpha=0.2;

}

completion:^(BOOL finished){//結(jié)束回調(diào)Block

if(finished){

// Revert image view to original.

self.imageView.frame=cacheFrame;

self.imageView.alpha=1.0;

}

}];

}

4、通知

在注冊(cè)通知觀察者中,有如下的方法,可以在添加/注冊(cè)觀察者時(shí),編寫收到通知后需要執(zhí)行的Block代碼。使用這個(gè)方法來(lái)注冊(cè)通知,可以使代碼簡(jiǎn)單明了。

-(id)addObserverForName:(nullableNSString*)nameobject:(nullable id)obj queue:(nullableNSOperationQueue*)queue usingBlock:(void(^)(NSNotification*note))block;

示例:

-(void)viewDidLoad{

[superviewDidLoad];

//注冊(cè)觀察者

[[NSNotificationCenterdefaultCenter]addObserverForName:@"AnimationCompleted"

object:nilqueue:[NSOperationQueuemainQueue]

usingBlock:^(NSNotification*notif){

NSLog(@"ViewController動(dòng)畫結(jié)束");

}];

}

5、線程操作(GCD/NSOperation)

在有關(guān)線程操作的GCD以及NSOperation中,也會(huì)使用到Block。例如,延遲執(zhí)行方法。

dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)),dispatch_get_main_queue(),^{

//延遲N秒后執(zhí)行的代碼

});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,374評(píng)論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,844評(píng)論 4 61
  • 每年這個(gè)時(shí)候我就會(huì)寫6封信,給:一年前,五年前,十年前的自己,一年后,五年后,十年后的自己。 根據(jù)積極心理學(xué)開山鼻...
    女巫在人間閱讀 354評(píng)論 0 0
  • 想了解更多健康知識(shí)?點(diǎn)擊右上角↗,關(guān)注我們吧! “養(yǎng)樹護(hù)根,養(yǎng)人護(hù)腳”。雙腳是運(yùn)行氣血、聯(lián)絡(luò)臟腑、溝通內(nèi)外、貫穿上...
    肥貓愛你閱讀 660評(píng)論 0 0
  • 日語(yǔ)中蔬菜: 1.八寶飯 はっぽはん(がゆ) 2.棒棒糖 ボンボン 3.爆米花 ポップコーン 4.便餐 けいしょく...
    知辰課堂閱讀 184評(píng)論 0 0

友情鏈接更多精彩內(nèi)容