pple定義的系統(tǒng)框架的API中洪橘,對(duì)Block的使用也比較集中济瓢,主要在動(dòng)畫荠割、通知等等幾個(gè)方面,因此旺矾,對(duì)于普通開發(fā)者來說蔑鹦,重點(diǎn)掌握系統(tǒng)框架API中Block的使用也是比較有必要的。
1箕宙、系統(tǒng)框架API中的Block
在iOS4以后嚎朽,越來越多的系統(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
接下來扒吁,給大家介紹幾個(gè)常用的火鼻。
2、動(dòng)畫與形變
在UIView類的定義中雕崩,提供了若干個(gè)包含Block參數(shù)的方法魁索,用來設(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è)方法來注冊(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í)行的代碼
});