Apple定义的系统框架的API中,对Block的使用也比较集中,主要在动画、通知等等几个方面,因此,对于普通开发者来说,重点掌握系统框架API中Block的使用也是比较有必要的。
1、系统框架API中的Block
在iOS4以后,越来越多的系统级的API在使用Block。苹果对于Block的使用主要集中在如下几个方面:
- 完成处理–Completion Handlers
- 通知处理–Notification Handlers
- 错误处理–Error Handlers
- 枚举–Enumeration
- 动画与形变–View Animation and Transitions
- 分类–Sorting
- 线程管理:GCD/NSOperation
接下来,给大家介绍几个常用的。
2、动画与形变
在UIView类的定义中,提供了若干个包含Block参数的方法,用来设置动画,例如修改View的大小、位置、透明度等等。
- @interface UIView(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
示例:
- [UIView animateWithDuration:0.2 animations:^{
- view.alpha = 0.0;
- } completion:^(BOOL finished){
- [view removeFromSuperview];
- }];
3、完成与错误处理
完成处理Block在某个动作完成后,通过回调的方式进行执行。错误处理Block会在执行某个操作时,假如发生错误而被执行。
- - (IBAction)pressBtn:(id)sender {
- CGRect cacheFrame = self.imageView.frame;
- [UIView animateWithDuration:1.5 animations:^{//播放动画Block
- CGRect newFrame = self.imageView.frame;
- newFrame.origin.y = newFrame.origin.y + 150.0;
- self.imageView.frame = newFrame;
- self.imageView.alpha = 0.2;
- }
- completion:^ (BOOL finished) {//结束回调Block
- if (finished) {
- // Revert image view to original.
- self.imageView.frame = cacheFrame;
- self.imageView.alpha = 1.0;
- }
- }];
- }
4、通知
在注册通知观察者中,有如下的方法,可以在添加/注册观察者时,编写收到通知后需要执行的Block代码。使用这个方法来注册通知,可以使代码简单明了。
- - (id )addObserverForName:(nullable NSString *)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;
示例:
- - (void)viewDidLoad {
- [super viewDidLoad];
- //注册观察者
- [[NSNotificationCenter defaultCenter] addObserverForName:@"AnimationCompleted"
- object:nil queue:[NSOperationQueue mainQueue]
- usingBlock:^(NSNotification *notif) {
- NSLog(@"ViewController动画结束");
- }];
- }
5、线程操作(GCD/NSOperation)
在有关线程操作的GCD以及NSOperation中,也会使用到Block。例如,延迟执行方法。
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- //延迟N秒后执行的代码
- });