使用CABasicAnimation实现的动画效果(一)
来源:互联网 发布:蚁群算法优化问题 编辑:程序博客网 时间:2024/05/29 19:59
这个动画采用了CABasicAnimation和CAAnimationGroup来实现的,改变位置、改变大小、按钮圆角数合在一起形成一个组合动画。
代码部分:
自定义按钮
@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
- 使用CABasicAnimation实现的动画效果(一)
- 用CABasicAnimation实现简单的动画效果
- CABasicAnimation动画的使用
- iOS --用CABasicAnimation实现简单的动画效果
- CABasicAnimation 几个常用的动画效果
- CABasicAnimation 几个常用的动画效果
- CABasicAnimation 几个常用的动画效果
- CABasicAnimation 热门动画效果
- CABasicAnimation 动画效果
- 实现 圆形加载view 动画效果(用CALayer,CABasicAnimation实现)
- iOSCoreAnimation动画系列教程(一):CABasicAnimation
- iOSCoreAnimation动画系列教程(一):CABasicAnimation
- iOSCoreAnimation动画系列教程(一):CABasicAnimation
- CABasicAnimation(基础属性动画)的简单使用
- iOS动画:UIView动画和CALayer动画(CABasicAnimation、CAKeyframeAnimation的使用)
- 关于 CABasicAnimation 实现效果
- CABasicAnimation学习Demo 包括了一些常用的动画效果
- CABasicAnimation学习Demo 包括了一些常用的动画效果
- 使用拦截器实现通用的登陆验证和日志记录
- Js实现360浏览器判断刷新或关闭
- LeetCode 21. Merge Two Sorted Lists
- 导入(import)那点事
- poj3614 Sunscreen (优先队列+贪心)
- 使用CABasicAnimation实现的动画效果(一)
- hbase 介绍
- 蓝桥杯B六----牌型种数----动态规划问题
- HashMap原理分析
- 数字在排序数组中出现的次数
- 布局之merge的使用
- mysql 高可用集群实验性搭建
- Java finally语句到底是在return之前还是之后执行?
- web开发与IC卡读卡器