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

来源:互联网 发布:蚁群算法优化问题 编辑:程序博客网 时间:2024/05/29 19:59

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

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


代码部分:

自定义按钮


@interface MyButton : UIButton


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


@end


#import "MyButton.h"


@implementation MyButton


-(instancetype)initWithFrame:(CGRect)frame{

    self = [superinitWithFrame:frame];

    

    if (self) {

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

        self.backgroundColor = [UIColorlightGrayColor];

        self.titleLabel.font = [UIFontsystemFontOfSize: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 {

    [superviewDidLoad];


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

        

        MyButton *btn = self.arrayM[i];

        

        [self.viewaddSubview:btn];

    }

    

    [self.viewaddSubview: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:btnyOffSet: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 = [CABasicAnimationanimationWithKeyPath:@"position"];

    //大小改变设置

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

    //按钮圆角数设置

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

    //组合动画

    CAAnimationGroup *group = [CAAnimationGroupanimation];

    

    if (btn.isChange) {

        

        //位移设置

        pAnimatiom.fromValue = [NSValuevalueWithCGPoint:btn.layer.position];

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


        //大小改变设置

        bAnimation.fromValue = [NSValuevalueWithCGRect:btn.bounds];

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

        

        //按钮圆角数设置

        cAnimation.fromValue = [NSNumbernumberWithFloat:btn.layer.cornerRadius];

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


        //组合动画设置

        group.timingFunction = [CAMediaTimingFunctionfunctionWithName: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 = [NSValuevalueWithCGPoint:btn.layer.position];

        pAnimatiom.toValue = [NSValuevalueWithCGPoint:self.view.center];

        

        //大小改变设置

        bAnimation.fromValue = [NSValuevalueWithCGRect:btn.bounds];

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


        //按钮圆角数设置

        cAnimation.fromValue = [NSNumbernumberWithFloat:btn.layer.cornerRadius];

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

        

        //组合动画设置

        group.timingFunction = [CAMediaTimingFunctionfunctionWithName: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 = [UIButtonbuttonWithType:UIButtonTypeCustom];

        [_runButtonsetFrame:CGRectMake(0,0,30,30)];

        [_runButtonsetTitle:@"按钮"forState:UIControlStateNormal];

        [_runButton.titleLabelsetFont:[UIFontsystemFontOfSize:14]];

        [_runButtonsetBackgroundColor:[UIColorlightGrayColor]];

        

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

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

        

        [_runButtonaddTarget:selfaction:@selector(run)forControlEvents:UIControlEventTouchUpInside];

    }

    

    return_runButton;

}


-(NSMutableArray *)arrayM{

    if (!_arrayM) {

        _arrayM = [NSMutableArrayarray];

        

        

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

            

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

            

            [btn setTitle:nameforState:UIControlStateNormal];

            [btn setTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];

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

            

            [_arrayM addObject:btn];

        }

    }

    

    return_arrayM;

}


@end




0 0