这个动画采用了CABasicAnimation和CAAnimationGroup来实现的,改变位置、改变大小、按钮圆角数合在一起形成一个组合动画。
代码部分:
自定义按钮
@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