blocks实践指南

1.为什么要使用blocks
将一个blocks作为函数或者方法的参数传递,可以使函数或者方法在恰当的点实现回调。以各种NSNotification类为例子,传统的方法是一个对象添加自己作为此通知的观察者,实现处理接到通知应该调用的方法(通过addObserver....)。
如下:
- (void)viewDidLoad {
   [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWillShow:)
        name:UIKeyboardWillShowNotification object:nil];
}
 
- (void)keyboardWillShow:(NSNotification *)notification {
    // Notification-handling code goes here.
}
然而通过方法  addObserverForName:object:queue:usingBlock: 我们也可以实现对通知的监控。
实现如下:
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification
         object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
             // Notification-handling code goes here. 
    }];
}

2.系统框架API中的blocks
另一个使用blocks的明显动机就是系统框架中提供了很多方法和函数用blocks作为参数。一般都存在于:
completion handlers,notification handlers,error handlers,enumeration,view animation and transitions,sorting 中,下面讲详细介绍。
(1)completion and Error handlers
        completion handlers是回调,其允许当框架方法或者函数完成任务时,在进行一些处理。多数情况下,用completion handler去free state 或者更新用户界面。很多框架方法让我们用blocks实现completion handlers,而不是代理方法或者notification handlers。
       UIView类有很多关于animation和view transitions的方法有completion handler block 参数。下面是 animateWithDuration:animations:completion: 方法的一个实现例子。

- (IBAction)animateView:(id)sender {
    CGRect cacheFrame = self.imageView.frame;
    [UIView animateWithDuration:1.5 animations:^{
        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) {
                         if (finished) {
                             // Revert image view to original.
                             sleep(3);
                             self.imageView.frame = cacheFrame;
                             self.imageView.alpha = 1.0;
                         }
                     }];
}

一些框架方法有error handlers,和completion handlers很相似。这些方法当其因为一些错误条件不能完成自己的任务的时候,调用这些error handlers blocks。我们典型的实现这些error handlers去通知用户出现错误。
(2)Notification handlers
     NSNotificationCenter的 addObserverForName:object:queue:usingBlock:方法让我们去实现一个通知的handler去建立一个observation。下面是一个例子:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    opQ = [[NSOperationQueue alloc] init];
    [[NSNotificationCenter defaultCenter] addObserverForName:@"CustomOperationCompleted"
             object:nil queue:opQ
        usingBlock:^(NSNotification *notif) {
        NSNumber *theNum = [notif.userInfo objectForKey:@"NumberOfItemsProcessed"];
        NSLog(@"Number of items processed: %i", [theNum intValue]);
    }];
}
上面的方法要求传入一个NSOperationQueue实例,因此应用程序可以指定执行block handler 的execution context。

(3)Enumeration
     Foundation framework的集合类(NSArray ,NSDictionary, NSSet 和 NSIndexSet)声明了一些方法,可以用blocks来枚举整个集合,其实等价与下代码段:
for (id item in collection) {
    // Code to operate on each item in turn.
}

       大概有两种形式。第一种方法名字以enumerate开始,没有返回值。
       第二种方法 block参数之前的名字为passingTest,这种方法返回整数或者一个NSIndexSet对象。这种方法的block一般是对迭代的每一个元素进行一个test,当test通过时返回YES。返回整数或者一个标识通过test的对象的集合。
     下面的代码对NSArray分别做了实例。一个方法的block返回数组中每个string有特定前缀的集合。
NSString *area = @"Europe";
NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
NSMutableArray *areaArray = [NSMutableArray arrayWithCapacity:1];
NSIndexSet *areaIndexes = [timeZoneNames indexesOfObjectsWithOptions:NSEnumerationConcurrent
                                passingTest:^(id obj, NSUInteger idx, BOOL *stop) {
    NSString  *tmpStr = (NSString *)obj;
    return [tmpStr hasPrefix:area];
}];
 
NSArray *tmpArray = [timeZoneNames objectsAtIndexes:areaIndexes];
[tmpArray enumerateObjectsWithOptions:NSEnumerationConcurrent|NSEnumerationReverse
                           usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                               [areaArray addObject:[obj substringFromIndex:[area length]+1]];
}];
NSLog(@"Cities in %@ time zone:%@", area, areaArray);

       尽管NSString类不属于集合,但是它也有两个有block参数的方法,为 enumerateSubstringsInRange:options:usingBlock:  enumerateLinesUsingBlock:。第一个方法的枚举单元可以是行,段,词,句,等等,但是第二个方法的就只是行。
下面的代码是使用block去匹配substring。
NSString *musician = @"Beatles";
NSString *musicDates = [NSString stringWithContentsOfFile:
    @"/usr/share/calendar/calendar.music"
    encoding:NSASCIIStringEncoding error:NULL];
[musicDates enumerateSubstringsInRange:NSMakeRange(0, [musicDates length]-1)
    options:NSStringEnumerationByLines
    usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
           NSRange found = [substring rangeOfString:musician];
           if (found.location != NSNotFound) {
                NSLog(@"%@", substring);
           }
      }];

(4)view animation 和 transitions
ios4.0之后UIView类引入了一些用于动画和view transitions的带block参数的方法。block参数分两类:
       block用于改变view的属性以完成动画。
       completion handlers。
下面的代码讲述了 animateWithDuration:animations:completion:方法的用法。
[UIView animateWithDuration:0.2 animations:^{
        view.alpha = 0.0;
    } completion:^(BOOL finished){
        [view removeFromSuperview];
    }];
这个动画就是让view逐渐消失,然后从父视图上移除。

    方法 transitionWithView:duration:options:animations:completion:用于两个view的transitions,包括flips和curls。
下面的代码段实现了两个views 的flip transitions。
[UIView transitionWithView:containerView duration:0.2
                   options:UIViewAnimationOptionTransitionFlipFromLeft                  animations:^{
                    [fromView removeFromSuperview];
                    [containerView addSubview:toView]
                }
                completion:NULL];

(5)sorting
foundation framework声明了NSComparator类型用于比较两个元素。
typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);

NSComparator是一个block类型,用于两个对象的比较,然后返回一个NSComparisonResult值。下面是一个例子:
NSArray *stringsArray = [NSArray arrayWithObjects:
                                 @"string 1",
                                 @"String 21",
                                 @"string 12",
                                 @"String 11",
                                 @"String 02", nil];
static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch |
        NSWidthInsensitiveSearch | NSForcedOrderingSearch;
NSLocale *currentLocale = [NSLocale currentLocale];
NSComparator finderSort = ^(id string1, id string2) {
    NSRange string1Range = NSMakeRange(0, [string1 length]);
    return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];
};
NSLog(@"finderSort: %@", [stringsArray sortedArrayUsingComparator:finderSort]);



3.block 和concurrency
     block封装了一段代码,可以异步执行。基于这一事实,块可以用在GCD和NSOperationQueue上面。
     GCD函数dispatch_sync和函数dispatch_async的第二个参数就是用block。
     




(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值