前言
一些简单的动画效果,会让你的界面看起来更加酷炫,这里我就把一些简单的实现动画效果的方法总结了一些.下面我就开始介绍下吧
UIView(1)
// 简单介绍这个API
// 参数1 : 是动画持续的时间.
// 参数2 : 延迟的时间.
// 参数3 : 阻尼.
// 参数4 : 是速率
// 参数5 : 选择动画的各种方式.是一个按位枚举
// 参数6 : 是一个block. 在block中完成,你需要的动画效果. 比如改变某个视图的位子.大小.
// 参数7 : 动画完成之后要做的事情
[UIView animateWithDuration:2 delay:0 usingSpringWithDamping:0.9 initialSpringVelocity:10 options:0 animations:^{
[UIView setAnimationRepeatCount:2];
self.viewOfGray.frame = CGRectMake(0, 200, 375, 120);
} completion:^(BOOL finished) {
}];
注释:
这是options的枚举
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
UIViewAnimationOptionLayoutSubviews = 1 << 0,
UIViewAnimationOptionAllowUserInteraction = 1 << 1, // turn on user interaction while animating
UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // start all views from current value, not initial value
UIViewAnimationOptionRepeat = 1 << 3, // repeat animation indefinitely
UIViewAnimationOptionAutoreverse = 1 << 4, // if repeat, run animation back and forth
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, // ignore nested duration
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, // ignore nested curve
UIViewAnimationOptionAllowAnimatedContent = 1 << 7, // animate contents (applies to transitions only)
UIViewAnimationOptionShowHideTransitionViews = 1 << 8, // flip to/from hidden state instead of adding/removing
UIViewAnimationOptionOverrideInheritedOptions = 1 << 9, // do not inherit any options or animation type
UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,
UIViewAnimationOptionTransitionNone = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
UIViewAnimationOptionTransitionCurlUp = 3 << 20,
UIViewAnimationOptionTransitionCurlDown = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,
UIView(2)
// 准备开始动画
[UIView beginAnimations:@"abc" context:nil];
// 设置动画参数
//执行的时间
[UIView setAnimationDuration:2];
// 动画执行的次数
[UIView setAnimationRepeatCount:CGFLOAT_MAX];
[UIView setAnimationRepeatAutoreverses:YES];
// 控件属性的更改
self.viewOF.backgroundColor = [UIColor blueColor];
self.viewOF.frame = CGRectMake(50, 50, 60, 50);
self.viewOfGray.frame = CGRectMake(0, 200, 375, 120);
self.viewOfGray.backgroundColor = [UIColor redColor];
// 提交动画.
[UIView commitAnimations];
CABasicAnimation
// 创建动画对象.
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
// 动画对象的API设置.
basic.fromValue = @1;
basic.toValue = @0.5;
basic.duration = 1;
basic.autoreverses = YES;
basic.repeatCount = CGFLOAT_MAX;
// layer层上添加动画.
[self.viewOfGray.layer addAnimation:basic forKey:@"basic"];
CAAnimationGroup动画组
// 创建动画组对象.
CAAnimationGroup *group = [CAAnimationGroup animation];
// 第一个动画
CABasicAnimation *basic1 = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
basic1.fromValue = @1;
basic1.toValue = @0.2;
// 第二个动画
CABasicAnimation *basic2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
basic2.fromValue = @2;
basic2.toValue = @M_PI;
// 设置动画组的一些属性
group.animations = @[basic1, basic2];
group.duration = 2;
group.autoreverses = YES;
group.repeatCount = CGFLOAT_MAX;
// layer层添加动画对象.
[self.viewOfGray.layer addAnimation:group forKey:@"group"];
CAKeyFrameAnimation
// 创建动画对象
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
// 设置动画的API
// 通过函数创建CGPathRef结构体.
CGMutablePathRef path = CGPathCreateMutable();
// 起始点
CGPathMoveToPoint(path, nil, self.viewOfRed.center.x, self.viewOfRed.center.y);
// 路径经过的点(直线移动).
// CGPathAddLineToPoint(path, nil, 200, 200);
// CGPathAddLineToPoint(path, nil, 200, 300);
// CGPathAddLineToPoint(path, nil, 300, 300);
// 路径经过的点(曲线移动).
CGPathAddCurveToPoint(path, nil, self.viewOfRed.center.x, self.viewOfRed.center.y, self.viewOfRed.center.x, self.viewOfRed.center.y + 100, self.viewOfRed.center.x + 100, self.viewOfRed.center.y + 100);
keyFrame.path = path;
keyFrame.duration = 2;
keyFrame.autoreverses = YES;
keyFrame.repeatCount = CGFLOAT_MAX;
// layer层添加动画.
[self.viewOfRed.layer addAnimation:keyFrame forKey:@"keyFrame"];
CATransition
// 创建动画对象.
CATransition *sition = [CATransition animation];
// 设置动画API
sition.type = @"cameraIrisHollowOpen";
sition.duration = 2;
sition.autoreverses = YES;
sition.repeatCount = CGFLOAT_MAX;
// layer上添加动画.
[self.viewOfGray.layer addAnimation:sition forKey:@"sition"];
总结
这就是小编总结的一些实现动画效果的方法,这里也是简单的介绍.实现的也是一些简单的动画, 如果需要,你们可以研究一些更酷炫的动画效果哦.
希望会对你们有些小的帮助.也希望多提一些宝贵的意见.