Block(块)介绍之五:系统框架中的Block

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的大小、位置、透明度等等。

 
 
  1. @interface UIView(UIViewAnimationWithBlocks)
  2.  
  3. + (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);
  4.  
  5. + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0
  6.  
  7. + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL
  8.  
  9. + (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);
  10.  
  11. + (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);
  12.  
  13. + (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
  14.  
  15. + (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);
  16.  
  17. @end
  18.  

示例:

 
 
  1. [UIView animateWithDuration:0.2 animations:^{
  2. view.alpha = 0.0;
  3. } completion:^(BOOL finished){
  4. [view removeFromSuperview];
  5. }];

3、完成与错误处理

完成处理Block在某个动作完成后,通过回调的方式进行执行。错误处理Block会在执行某个操作时,假如发生错误而被执行。

 
 
  1. - (IBAction)pressBtn:(id)sender {
  2. CGRect cacheFrame = self.imageView.frame;
  3. [UIView animateWithDuration:1.5 animations:^{//播放动画Block
  4. CGRect newFrame = self.imageView.frame;
  5. newFrame.origin.y = newFrame.origin.y + 150.0;
  6. self.imageView.frame = newFrame;
  7. self.imageView.alpha = 0.2;
  8. }
  9. completion:^ (BOOL finished) {//结束回调Block
  10. if (finished) {
  11. // Revert image view to original.
  12. self.imageView.frame = cacheFrame;
  13. self.imageView.alpha = 1.0;
  14. }
  15. }];
  16. }

4、通知

在注册通知观察者中,有如下的方法,可以在添加/注册观察者时,编写收到通知后需要执行的Block代码。使用这个方法来注册通知,可以使代码简单明了。

 
 
  1. - (id )addObserverForName:(nullable NSString *)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;

示例:

 
 
  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3.  
  4. //注册观察者
  5. [[NSNotificationCenter defaultCenter] addObserverForName:@"AnimationCompleted"
  6. object:nil queue:[NSOperationQueue mainQueue]
  7. usingBlock:^(NSNotification *notif) {
  8. NSLog(@"ViewController动画结束");
  9. }];
  10.  
  11. }

5、线程操作(GCD/NSOperation)

在有关线程操作的GCD以及NSOperation中,也会使用到Block。例如,延迟执行方法。

 
 
  1. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  2. //延迟N秒后执行的代码
  3. });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值