使用CABasicAnimation实现的动画效果(一)

这个动画采用了CABasicAnimation和CAAnimationGroup来实现的,改变位置、改变大小、按钮圆角数合在一起形成一个组合动画。

原来是准备在CABasicAnimation中通过begintime来设置延时的效果,但效果不理想。


代码部分:

自定义按钮


@interface MyButton : UIButton


@property (assign,nonatomic) BOOL isChange; /*!< 是否变化 */


@end


#import "MyButton.h"


@implementation MyButton


-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    

    if (self) {

        self.layer.cornerRadius = frame.size.width / 2;

        self.backgroundColor = [UIColor lightGrayColor];

        self.titleLabel.font = [UIFont systemFontOfSize:14];

    }

    

    return self;

}



@end



#define kSmallWidth 10

#define kBigWidth 30


@interface TwoViewController ()


@property (strong,nonatomic) CAShapeLayer *shape;


@property (strong,nonatomic) UIButton *runButton;

@property (strong,nonatomic) NSMutableArray *arrayM;


@end


@implementation TwoViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    for (NSInteger i = 0; i < self.arrayM.count; i++) {

        

        MyButton *btn = self.arrayM[i];

        

        [self.view addSubview:btn];

    }

    

    [self.view addSubview:self.runButton];

    

}


-(void)run{

    //偏移量

    CGFloat yOffSet = 50;

    //延时秒数

    NSTimeInterval delay = 0.1;

    

    for (NSInteger i = 0; i < self.arrayM.count; i++) {

        

        MyButton *btn = self.arrayM[i];

        

        [self runAnimation:btn yOffSet:yOffSet * (i + 1) delay:delay];

        

        delay += 0.1;

    }

}


/**

 *  按钮的动画处理

 *

 *  @param btn     按钮

 *  @param yOffSet 偏移量

 *  @param delay   延迟秒数(用了CAAnimationGroup中的begintime来做延时,但没有做到想要的效果,暂时就不用了)

 */

- (void)runAnimation:(MyButton *)btn yOffSet:(CGFloat)yOffSet delay:(NSTimeInterval)delay{

    

    btn.isChange = !btn.isChange;

    

    //位移设置

    CABasicAnimation *pAnimatiom = [CABasicAnimation animationWithKeyPath:@"position"];

    //大小改变设置

    CABasicAnimation *bAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];

    //按钮圆角数设置

    CABasicAnimation *cAnimation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];

    //组合动画

    CAAnimationGroup *group = [CAAnimationGroup animation];

    

    if (btn.isChange) {

        

        //位移设置

        pAnimatiom.fromValue = [NSValue valueWithCGPoint:btn.layer.position];

        pAnimatiom.toValue = [NSValue valueWithCGPoint:CGPointMake(btn.layer.position.x, btn.layer.position.y - yOffSet)];


        //大小改变设置

        bAnimation.fromValue = [NSValue valueWithCGRect:btn.bounds];

        bAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, kBigWidth, kBigWidth)];

        

        //按钮圆角数设置

        cAnimation.fromValue = [NSNumber numberWithFloat:btn.layer.cornerRadius];

        cAnimation.toValue = [NSNumber numberWithFloat:btn.layer.bounds.size.width / 2];


        //组合动画设置

        group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

        [group setAnimations:@[pAnimatiom,bAnimation,cAnimation]];

        group.duration = 0.1;

        group.removedOnCompletion = NO;

        

        //动画完成后,设置按钮

        btn.layer.cornerRadius = kBigWidth / 2;

        btn.layer.position = CGPointMake(btn.layer.position.x, btn.layer.position.y - yOffSet);

        btn.bounds = CGRectMake(0, 0, kBigWidth, kBigWidth);

    }

    else{

        //位移设置

        pAnimatiom.fromValue = [NSValue valueWithCGPoint:btn.layer.position];

        pAnimatiom.toValue = [NSValue valueWithCGPoint:self.view.center];

        

        //大小改变设置

        bAnimation.fromValue = [NSValue valueWithCGRect:btn.bounds];

        bAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, kSmallWidth, kSmallWidth)];


        //按钮圆角数设置

        cAnimation.fromValue = [NSNumber numberWithFloat:btn.layer.cornerRadius];

        cAnimation.toValue = [NSNumber numberWithFloat:btn.layer.bounds.size.width / 2];

        

        //组合动画设置

        group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

        [group setAnimations:@[pAnimatiom,bAnimation,cAnimation]];

        group.duration = 0.1;

        group.removedOnCompletion = NO;

        

        //动画完成后,设置按钮

        btn.layer.cornerRadius = kSmallWidth / 2;

        btn.layer.position = self.view.center;

        btn.bounds = CGRectMake(0, 0, kSmallWidth, kSmallWidth);


    }

    

    [btn.layer addAnimation:group forKey:nil];

}



-(UIButton *)runButton{

    if (!_runButton) {

        _runButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [_runButton setFrame:CGRectMake(0, 0,30, 30)];

        [_runButton setTitle:@"按钮" forState:UIControlStateNormal];

        [_runButton.titleLabel setFont:[UIFont systemFontOfSize:14]];

        [_runButton setBackgroundColor:[UIColor lightGrayColor]];

        

        _runButton.layer.cornerRadius = _runButton.bounds.size.width / 2;

        _runButton.layer.position = self.view.center;

        

        [_runButton addTarget:self action:@selector(run) forControlEvents:UIControlEventTouchUpInside];

    }

    

    return _runButton;

}


-(NSMutableArray *)arrayM{

    if (!_arrayM) {

        _arrayM = [NSMutableArray array];

        

        

        for (NSString *name in @[@"A",@"B",@"C",@"D",@"E"]) {

            

            MyButton *btn = [[MyButton alloc]initWithFrame:CGRectMake(0, 0, kSmallWidth, kSmallWidth)];

            

            [btn setTitle:name forState:UIControlStateNormal];

            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

            btn.layer.position = self.view.center;

            

            [_arrayM addObject:btn];

        }

    }

    

    return _arrayM;

}


@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值