UIView 之animation
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 = 6*M_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 = @[@(0*totalRotation), @(0.33*totalRotation), @(0.67*totalRotation), @(1*totalRotation)];
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;
}
}
网友评论