####UIView 之animation s = vt; 这个是我对动画的理解, 任何动画均可以用这个来解释分析。 咱们设置一个动画 每每是限定了 运动变化(轨迹)和时间,最多见的是 app
[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.30];//时间 [UIView setAnimationDelegate:self]; self.transform = rotationTransform;//视图改变 [UIView commitAnimations];
或者oop
[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还有一种在实际开发过程当中比较常见的转场动画:
如自定义拍照或录制视频功能时,在点击先后摄像头图标 视图内的画面旋转天然切换,图片集浏览。 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>url
从上看出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"]; }code
###### CADisplayLink CADisplayLink 其实和CADAnimation同样 都是QuartzCore.framework里的类,但这个又是有些特殊的地方,通常都把它于NSTimer放在一块儿比较,由于这个拥有计时器的功能,精度能达到每秒触发60次。它和timer同样是把对象加到runloop中,触发的时间到了,runloop向对象指定的target发送一次selector消息。 在作一些精细点的动画,不受其余动画或线程干扰,须要指定runloop的模式,就像srollView在滚动时,若是是默认的runloop模式,timer的计时时间会延迟。 以下面一段代码:为了实现img1Loading和img2Loading 无缝隙循环移动
#pragma - mark - loadingImageAnimationorm
}视频
(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; }
}