动画(四)

注:动画四主要是用第三方动画库pop完成一个小效果。源码会在下个开源项目中贴出

讲真这个框架真的很牛逼
FaceBook 开源框架地址pop:https://github.com/facebook/pop

先看下开始展示没有自定义动画效果


开始展示的时候,没有动画效果

没有动画的效果吧:
可能太快了 看不出来和下面的对比效果


再看下有自定义动画效果的展示


JF.gif

展示:

/*
 * 创建多个按钮
 */
- (void)setupButtons
{
    // 数据
    NSArray *images = @[@"ch2_xinjianrenwu_bg_new", @"ch2_xinjianricheng_bg_new", @"ch2_qiandaodaka_bg_new", @"ch2_fabudongtai_bg_new", @"ch2_fabutongzhi_bg_new", @"ch2_crm_bg_new"];
    NSArray *titles = @[@"新建任务", @"新建日程", @"签到打卡", @"发布动态", @"发布通知", @"JF"];
    
    // 类似九宫格创建法
    NSUInteger count = images.count;
    int maxColsCount = 3; // 一行的列数
    NSUInteger rowsCount = (count + maxColsCount - 1) / maxColsCount;
    
    // 按钮尺寸
    CGFloat buttonW = JFSCREENWIDTH / maxColsCount;
    CGFloat buttonH = buttonW * 1.2;
    CGFloat buttonStartY = (JFSCREENHEIGHT - rowsCount * buttonH) * 0.5;
    for (int i = 0; i < count; i++) {
        // 创建、添加
        JFAddTaskButton *button = [JFAddTaskButton buttonWithType:UIButtonTypeCustom];
        button.width = -1; // 按钮的尺寸为0,还是能看见文字缩成一个点,设置按钮的尺寸为负数,那么就看不见文字了
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttons addObject:button];
        [self.view addSubview:button];
        
        // 内容
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        
        // frame
        CGFloat buttonX = (i % maxColsCount) * buttonW;
        CGFloat buttonY = buttonStartY + (i / maxColsCount) * buttonH;
        
        // 初始化一个pop动画对象
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        // 从哪里来
        anim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY - JFSCREENHEIGHT, buttonW, buttonH)];
        
        // 到哪里去
        anim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY, buttonW, buttonH)];
        // 动画速率
        anim.springSpeed = JFSpringFactor;
        // 摩擦系数
        anim.springBounciness = JFSpringFactor;
        // CACurrentMediaTime()获得的是当前时间
        anim.beginTime = CACurrentMediaTime() + [self.times[i] doubleValue];
        [button pop_addAnimation:anim forKey:nil];
    }
}

消失:

 // 禁止交互
    self.view.userInteractionEnabled = NO;
    
    // 让按钮执行动画
    for (int i = 0; i < self.buttons.count; i++) {
        JFAddTaskButton *button = self.buttons[i];
        
        POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionY];
        anim.toValue = @(button.layer.position.y + JFSCREENHEIGHT);
        // CACurrentMediaTime()获得的是当前时间
        anim.beginTime = CACurrentMediaTime() + [self.times[i] doubleValue];
        [button.layer pop_addAnimation:anim forKey:nil];
    }
    
    JFWeakSelf;
    // 让标题执行动画
    POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    anim.toValue = @(self.sloganImageView.layer.position.y + JFSCREENHEIGHT);
    // CACurrentMediaTime()获得的是当前时间
    anim.beginTime = CACurrentMediaTime() + [self.times.lastObject doubleValue];
    [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        [weakSelf dismissViewControllerAnimated:NO completion:nil];
    }];
    [self.sloganImageView.layer pop_addAnimation:anim forKey:nil];

标题语

- (void)setupSloganView
{
    CGFloat sloganY = JFSCREENHEIGHT * 0.2;
    
    // 添加
    UIImageView *sloganView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"app_slogan"]];
    sloganView.y = sloganY - JFSCREENHEIGHT;
    sloganView.centerX = JFSCREENWIDTH * 0.5;
    [self.view addSubview:sloganView];
    self.sloganImageView = sloganView;
    
    JFWeakSelf;
    // 动画
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    anim.toValue = @(sloganY);
    anim.springSpeed = JFSpringFactor;
    anim.springBounciness = JFSpringFactor;
    // CACurrentMediaTime()获得的是当前时间
    anim.beginTime = CACurrentMediaTime() + [self.times.lastObject doubleValue];
    [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        // 开始交互
        weakSelf.view.userInteractionEnabled = YES;
    }];
    [sloganView.layer pop_addAnimation:anim forKey:nil];
}

2015-12 - 29 上海

源码在下一个开源项目中贴出来,有不足之处欢迎指出来。我会非常感谢

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,595评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • web系统测试分为6个部分: 功能测试 性能测试(包括负载/压力测试) 用户界面测试 兼容性测试 安全测试 接口测...
    繼續hug阅读 1,245评论 0 7
  • 前期准备: 使用前请先导入CoreLocation.framework、Security.framework、Co...
    流水点点阅读 1,631评论 0 1
  • 现在正坐在咖啡厅的走廊,面向落日余晖。 一晃又是几个月没有文字记录。且随着思绪记录,权当自我梳理。 下午来这边准备...
    捍道阅读 999评论 0 0