iOS 动画
2016-10-24 00:00
531 查看
####UIView 之animation
s = vt; 这个是我对动画的理解, 任何动画都可以用这个来解释分析。 我们设置一个动画 往往是限定了 运动变化(轨迹)和时间,最常见的是
或者
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion
(void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion
@interface CAAnimation : NSObject <NSCoding, NSCopying, CAMediaTiming, CAAction>
(void)showPlay{
UIColor *stroke = [UIColor redColor];
CGRect pathFrame = CGRectMake(120, 120, 60, 60);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:pathFrame cornerRadius:30];
CAShapeLayer *circleShape = [CAShapeLayer layer];
circleShape.path = path.CGPath;
circleShape.position = CGPointMake(0, 0);
circleShape.fillColor = [UIColor clearColor].CGColor;
circleShape.opacity = 0;
circleShape.strokeColor = stroke.CGColor;
circleShape.lineWidth = 3;
[self.layer addSublayer:circleShape];
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(3, 1, 1)];//x,y,z放大缩小倍数
CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
alphaAnimation.fromValue = @1;
alphaAnimation.toValue = @0;
CAAnimationGroup *animation = [CAAnimationGroup animation];
animation.animations = @[scaleAnimation, alphaAnimation];
animation.duration = 7.5f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.repeatCount = 4;
[circleShape addAnimation:animation forKey:nil];
}
(void)rotateWheel:(UIView*)view
{
float totalRotation = 6M_PI;
NSArray keyTimes = @[@0.0, @0.2, @0.8, @1.0]; //0-0.2-0.8-1.0 3段动画
CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"]; //z轴选择
animation.duration = 5;
animation.keyTimes = keyTimes;
animation.values = @[@(0totalRotation), @(0.33totalRotation), @(0.67totalRotation), @(1totalRotation)];
animation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[view.layer addAnimation:animation forKey:@"rotateZ"];
}
#pragma - mark - loadingImageAnimation
(void)startMoveLoadingImage
{
self.moveDisplaylink = [CADisplayLink displayLinkWithTarget:self selector:@selector(bgLoadingImageMove:)];
self.moveDisplaylink.frameInterval = 2;//每秒30次
[self.moveDisplaylink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
(void)bgLoadingImageMove:(CADisplayLink *)displayLink
{
self.linkTimeCount ++;
int perMaxCount = 360; //每一次循环滑动调用的最大次数
[self setLoadingImage:perMaxCount];
}
(void)setLoadingImage:(int)perMaxCount
{
int moveCount = self.linkTimeCount%perMaxCount;//余数
// CCLog(@"moveCount %d",moveCount);
CGRect frame = _img1Loading.frame;
CGFloat imageWidth = frame.size.width - 44; //441
frame.origin.x = -imageWidth/perMaxCount*moveCount;
if (self.img1Loading.frame.origin.x > self.img2Loading.frame.origin.x) {
self.img2Loading.frame = frame;
frame.origin.x += imageWidth;
self.img1Loading.frame = frame;
}
else
{
self.img1Loading.frame = frame;
frame.origin.x += imageWidth;
self.img2Loading.frame = frame;
}
if (moveCount+1 == perMaxCount)
{
self.isNeedChange = YES;
}
if (self.isNeedChange) {
frame.origin.x = imageWidth;
if (self.img1Loading.frame.origin.x > self.img2Loading.frame.origin.x) {
self.img2Loading.frame = frame;
}
else
{
self.img1Loading.frame = frame;
}
self.isNeedChange = false;
}
}
(void)stopAnimation
{
if (_moveDisplaylink)
{
[self.moveDisplaylink invalidate];
self.moveDisplaylink = nil;
self.linkTimeCount = 0;
}
}
s = vt; 这个是我对动画的理解, 任何动画都可以用这个来解释分析。 我们设置一个动画 往往是限定了 运动变化(轨迹)和时间,最常见的是
[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.30];//时间 [UIView setAnimationDelegate:self]; self.transform = rotationTransform;//视图改变 [UIView commitAnimations];
或者
[UIView animateWithDuration:0.3 animations:^{ }]; ``` UIView animateWithDuration:… 这个方法 apple也做了很多的扩展,对一些简单的单一需求也能满足。 这边对下面这个带有阻尼效果的动画做个简单分析
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion
usingSpringWithDamping(阻尼系数) 的范围为0.0f 到1.0f ,数值越小「弹簧」的振动效果越明显。initialSpringVelocity(初始运动速度),数值越大一开始移动速度越快。 UIView还有一种在实际开发过程中比较常见的转场动画:
(void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion
如自定义拍照或录制视频功能时,在点击前后摄像头图标 视图内的画面旋转自然切换,图片集浏览。 UIViewAnimationOptions: 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, ####UILayer之CAAnimation
@interface CAAnimation : NSObject <NSCoding, NSCopying, CAMediaTiming, CAAction>
从上看出CAAnimation 使用了CAMediaTiming协议,可以设置动画的开始时间、持续时间、速度、重复次数等,使用了CAAction协议,可以设置动画的方式(路径)来显示动画。 CAAnimation 设置动画原理和UIView差不多,CAAnimation是作用于Layer层上的,而UIView animation 是作用于View上的。 CAAnimation的一些派生类: CATransition 提供渐变效果:(推拉push效果,消退fade效果,揭开reveal效果) CAAnimationGroup 允许多个动画同时播放 CABasicAnimation 提供了对单一动画的实现 CAKeyframeAnimation 关键桢动画,可以定义行动路线 比较常用的是CABasicAnimation、CAKeyframeAnimation和CAAnimationGroup; CABasicAnimation动画也需要设置变化和变化的时间,通过-setFromValue 和-setToValue 来指定一个开始值和结束值,duration是动画持续时间。 下面是两个例子:
(void)showPlay{
UIColor *stroke = [UIColor redColor];
CGRect pathFrame = CGRectMake(120, 120, 60, 60);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:pathFrame cornerRadius:30];
CAShapeLayer *circleShape = [CAShapeLayer layer];
circleShape.path = path.CGPath;
circleShape.position = CGPointMake(0, 0);
circleShape.fillColor = [UIColor clearColor].CGColor;
circleShape.opacity = 0;
circleShape.strokeColor = stroke.CGColor;
circleShape.lineWidth = 3;
[self.layer addSublayer:circleShape];
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(3, 1, 1)];//x,y,z放大缩小倍数
CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
alphaAnimation.fromValue = @1;
alphaAnimation.toValue = @0;
CAAnimationGroup *animation = [CAAnimationGroup animation];
animation.animations = @[scaleAnimation, alphaAnimation];
animation.duration = 7.5f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.repeatCount = 4;
[circleShape addAnimation:animation forKey:nil];
}
(void)rotateWheel:(UIView*)view
{
float totalRotation = 6M_PI;
NSArray keyTimes = @[@0.0, @0.2, @0.8, @1.0]; //0-0.2-0.8-1.0 3段动画
CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"]; //z轴选择
animation.duration = 5;
animation.keyTimes = keyTimes;
animation.values = @[@(0totalRotation), @(0.33totalRotation), @(0.67totalRotation), @(1totalRotation)];
animation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[view.layer addAnimation:animation forKey:@"rotateZ"];
}
###### CADisplayLink CADisplayLink 其实和CADAnimation一样 都是QuartzCore.framework里的类,但这个又是有些特殊的地方,一般都把它于NSTimer放在一起比较,因为这个拥有计时器的功能,精度能达到每秒触发60次。它和timer一样是把对象加到runloop中,触发的时间到了,runloop向对象指定的target发送一次selector消息。 在做一些精细点的动画,不受其他动画或线程干扰,需要指定runloop的模式,就像srollView在滚动时,如果是默认的runloop模式,timer的计时时间会延迟。 如下面一段代码:为了实现img1Loading和img2Loading 无缝隙循环移动
#pragma - mark - loadingImageAnimation
(void)startMoveLoadingImage
{
self.moveDisplaylink = [CADisplayLink displayLinkWithTarget:self selector:@selector(bgLoadingImageMove:)];
self.moveDisplaylink.frameInterval = 2;//每秒30次
[self.moveDisplaylink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
(void)bgLoadingImageMove:(CADisplayLink *)displayLink
{
self.linkTimeCount ++;
int perMaxCount = 360; //每一次循环滑动调用的最大次数
[self setLoadingImage:perMaxCount];
}
(void)setLoadingImage:(int)perMaxCount
{
int moveCount = self.linkTimeCount%perMaxCount;//余数
// CCLog(@"moveCount %d",moveCount);
CGRect frame = _img1Loading.frame;
CGFloat imageWidth = frame.size.width - 44; //441
frame.origin.x = -imageWidth/perMaxCount*moveCount;
if (self.img1Loading.frame.origin.x > self.img2Loading.frame.origin.x) {
self.img2Loading.frame = frame;
frame.origin.x += imageWidth;
self.img1Loading.frame = frame;
}
else
{
self.img1Loading.frame = frame;
frame.origin.x += imageWidth;
self.img2Loading.frame = frame;
}
if (moveCount+1 == perMaxCount)
{
self.isNeedChange = YES;
}
if (self.isNeedChange) {
frame.origin.x = imageWidth;
if (self.img1Loading.frame.origin.x > self.img2Loading.frame.origin.x) {
self.img2Loading.frame = frame;
}
else
{
self.img1Loading.frame = frame;
}
self.isNeedChange = false;
}
}
(void)stopAnimation
{
if (_moveDisplaylink)
{
[self.moveDisplaylink invalidate];
self.moveDisplaylink = nil;
self.linkTimeCount = 0;
}
}
相关文章推荐
- 【iOS-Cocos2d游戏开发之七】在cocos2d中添加/删除系统组件,并解决View设置透明会影响View中的其他组件的问题!【11月28日更新解决添加组件Cocos2d动画停止播放的BUG】
- iOS-实现简单的动画效果
- iOS-实现简单的动画效果
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)! .
- iOS动画:Core Animation概述
- ios中view的动画效果
- 【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧(指定开始帧)以及扩展Cocos2d源码的CCAnimation简化动画创建!
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现动画进行时慢动作\设置游戏加减速进行
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)! .
- ios 视图切换动画效果
- IOS中如何模拟UINavigationController的Slide动画
- iphone ios 动画
- 【iOS-Cocos2d游戏开发之十八】解决滚屏背景/拼接地图有黑边(缝隙)/动画播放出现毛边以及禁止游戏中自动锁屏问题!【2011年12月18日补充】
- ios 视图切换动画效果
- ios中view的动画效果
- [iOS]实现了一套自定义动画库
- RAD Studio XE2新特性概览:多平台支持、原生iOS与Android、HD与3D动画及云
- 【iOS-Cocos2d游戏开发之二十一 】自定义精灵类并为你的精灵设置攻击帧以及动画创建!【一】
- ios 视图切换动画效果
- 如何在ios中运用core animation暂停和继续动画