1. 程式人生 > >使用CABasicAnimation實現的動畫效果(一)

使用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 = [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.view addSubview: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: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 = [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 = [NSValue valueWithCGRect:btn.bounds];

        bAnimation.toValue = [NSValue valueWithCGRect: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 = [NSValue valueWithCGRect:btn.bounds];

        bAnimation.toValue = [NSValue valueWithCGRect: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 = [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:[UIColorwhiteColor] forState:UIControlStateNormal];

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

            [_arrayM addObject:btn];

        }

    }

return_arrayM;

}


@end