iOS中实现弹簧动画

作者: 朱凯奇 | 来源:发表于2016-04-07 15:52 被阅读1577次

    我们都知道iOS9 CASpringAnimation 弹簧动画,操作简单,容易实现,目前使用也比较广泛,下面我给大家介绍一种pop的弹簧效果,跟CASpringAnimation 弹簧动画 很相似
    CocaPods加载 pod 'pop', '~> 1.0.9'

    首先我们先看下动画的效果


    2.gif

    具体实现如下

    #import "CSPlusVC.h"
    #import "CSVerticalButton.h" //自定义封装的button
    #import <POP.h>
    
    @implementation CSPlusVC
    
    - (void)viewDidLoad {
        [super viewDidLoad];
      
        [self addVerticalButtons];
      
    }
    - (void)addVerticalButtons{
     //点击之前 关闭交互
        self.view.userInteractionEnabled = NO;
    //创建两个数组 分别存button上image的名字和title的名字
        NSArray *imagesArray = @[@"renwu",@"shuju",@"shudu",@"xianhua",@"yanlun",@"quantou"];
        NSArray *titleArray = @[@"人物",@"数据",@"数读",@"闲话",@"言论",@"拳头"];
       // kWidth为整屏宽,kHeight为整屏高
        CGFloat buttonW = kWidth*0.2;
        CGFloat buttonH = buttonW + 0.04*kHeight;
        CGFloat buttonStartY = (kHeight - 2*buttonW)*0.5;//buttonY的开始位置
        CGFloat buttonStartX = 20;
        int maxCols = 3;//每行最多三个
        
        CGFloat xMargin = (kWidth - 2*buttonStartX - maxCols *buttonW)/(maxCols-1); 
        
        for (int i=0;i<6;i++) {
      //添加6个button
            CSVerticalButton *button = [[CSVerticalButton alloc] init];
    //设置button的相关属性
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            [button setTitle:titleArray[i] forState:(UIControlStateNormal)];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:imagesArray[i]] forState:(UIControlStateNormal)];
            button.tag = i; //原始方法 给button一个tag值 用与后面点击方法
            [button addTarget:self action:@selector(clickButtonAciton:) forControlEvents:(UIControlEventTouchUpInside)];
            
            int row = i/maxCols; //行
            int col = i%maxCols;  //列
            CGFloat buttonX = buttonStartX+col*(buttonW+xMargin);
            CGFloat buttonEndY = buttonStartY +row*(buttonH+10);
            CGFloat buttonBeginY = buttonEndY -kHeight;
            [self.view addSubview:button];
            
    //创建POPSpringAnimation动画 对象  动画结束位置就是button最终frame的位置 所以不需要一开始设置button在window中的位置 这也是跟CASpringAnimation的区别
            POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame]; //改变frame
            anim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonBeginY, buttonW, buttonH)];//动画开始位置
            anim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonEndY, buttonW, buttonH)];//动画结束位置
            anim.springBounciness = 15;//弹簧反弹弹度[4,20]
            anim.springSpeed  = 15;//弹簧速度[4,20]
            anim.beginTime = CACurrentMediaTime()+0.1*i;//动画开始时间
            //动画结束后 打开用户交互界面
            [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
                self.view.userInteractionEnabled = YES;
            }];
            [button pop_addAnimation:anim forKey:nil];
            
        }
    
        // 添加标语
        UIImageView *sloganView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"app_slogan"]];
        [self.view addSubview:sloganView];
        
        // 标语动画 
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter]; //改变center位置
        CGFloat centerX = kWidth * 0.5;
        CGFloat centerEndY = kHeight * 0.2;
        CGFloat centerBeginY = centerEndY - kHeight;
        anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerBeginY)];
        anim.toValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerEndY)];
        
        anim.springBounciness = 15;
        anim.springSpeed = 15;
        anim.beginTime = CACurrentMediaTime() + imagesArray.count * 0.1;
        [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
            // 标语动画执行完毕, 恢复点击事件
            self.view.userInteractionEnabled = YES;
        }];
        [sloganView pop_addAnimation:anim forKey:nil];
    }
    //取消按钮的方法
    - (IBAction)cancelAction:(id)sender {
        
        [self cancelWithCompletionBlock:nil];
        
    }
    
    //取消的block方法
    - (void)cancelWithCompletionBlock:(void(^)())completionBlock
    {
     //关闭交互
        self.view.userInteractionEnabled = NO;
        int index = 0;
    //遍历父视图   用于设置页面消失时的动画
        for (UIButton *button in self.view.subviews) {
            if ([button isKindOfClass:[CSVerticalButton class]]) {
      //给button设置下坠动画 用最普通的POPBasicAnimation就可以
                POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
                CGFloat centerY = button.center.y + kHeight;
                anim.toValue = [NSValue valueWithCGPoint:CGPointMake(button.center.x, centerY)];
                anim.beginTime = CACurrentMediaTime()+ index*0.1;
                [button pop_addAnimation:anim forKey:nil];
                index++;
                if (index==5) { //在最后一个动画执行完毕后 ,页面退出 ,然后调用block ,block里执行要点击的事件
                    [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
                        [self dismissViewControllerAnimated:NO completion:nil];
                        if (completionBlock) {
                            completionBlock();
                        }
                    }];
                }
            }
        }
    
    }
    

    相信我写的已经很详细了,如有问提,请留言

    相关文章

      网友评论

        本文标题:iOS中实现弹簧动画

        本文链接:https://www.haomeiwen.com/subject/hkomlttx.html