IOS动画学习(一)
(1)基础动画CABasicAnimation
//基础动画
CABasicAnimation *base = [CABasicAnimation animationWithKeyPath:@"position"];
base.duration = 1.0f;
base.toValue = [NSValue valueWithCGPoint:CGPointMake(30, 40)];
base.delegate = self;
[self.view.layer addAnimation:base forKey:@"basean"];
//委托函数
-(void)animationDidStart:(CAAnimation *)anim {
NSLog(@"动画开始");
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
NSLog(@"动画结束");
}
(2)关键帧动画CAKeyframeAnimation
//关键帧动画(path的优先级比values得高,两个都设置优先执行path)
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyFrame.duration = 2.0f;
keyFrame.repeatCount = 10;
keyFrame.values = @[
[NSValue valueWithCGPoint:CGPointMake(80, 220)],
[NSValue valueWithCGPoint:CGPointMake(90, 230)],
[NSValue valueWithCGPoint:CGPointMake(100, 240)],
[NSValue valueWithCGPoint:CGPointMake(110, 250)]
];
keyFrame.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; //淡出淡入
// CGMutablePathRef path = CGPathCreateMutable();
// CGPathMoveToPoint(path, NULL, self.view.layer.position.x, self.view.layer.position.y);
//
// CGPathAddCurveToPoint(path, NULL, 80, 220, 90, 230, 100, 240);//贝塞尔曲线(三点确定一条曲线)
//
// keyFrame.path = path;
//
// CGPathRelease(path);
keyFrame.autoreverses = YES;
[self.view.layer addAnimation:keyFrame forKey:@"keyFrame"];
(3)转场动画CATransition
//转换动画
CATransition *transi = [CATransition animation];
transi.type = kCATransitionMoveIn; //新视图覆盖旧试图上
transi.subtype = kCATransitionFromLeft; //过渡动画,从左侧
transi.duration = 1.0f;
[self.view.layer addAnimation:transi forKey:@"transi"];
(4)组动画CAAnimationGroup
//绕z轴旋转
CABasicAnimation *ratate = [CABasicAnimation animationWithKeyPath:@"transform.ratation.z"];
ratate.toValue = [NSNumber numberWithFloat:M_PI * 3];
ratate.removedOnCompletion = NO;
ratate.autoreverses = YES;
ratate.duration = 1.0f;
//移动动画
CAKeyframeAnimation *positionKey = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGPoint endPoint = CGPointMake(50, 300);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, self.view.layer.position.x, self.view.layer.position.y);
CGPathAddCurveToPoint(path, NULL, 160, 280, -30, 300, endPoint.x, endPoint.y);
positionKey.path = path;
positionKey.autoreverses = YES;
positionKey.removedOnCompletion = NO;
//组动画
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[ratate,positionKey];
group.duration = 8.0f;
group.removedOnCompletion = NO;
group.beginTime = CACurrentMediaTime() + 2;
[self.view.layer addAnimation:group forKey:@"group"];
(5)UIView封装的block动画
//block方式
/*开始动画,UIView的动画方法执行完后动画会停留在重点位置,而不需要进行任何特殊处理
duration:执行时间
delay:延迟时间
options:动画设置,例如自动恢复、匀速运动等
completion:动画完成回调方法
*/
//UIView封装的动画
//(1)关键帧
[UIView animateKeyframesWithDuration:8.0f delay:0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5 animations:^{
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.25 animations:^{
}];
[UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{
}];
} completion:^(BOOL finished) {
}];
/*创建弹性动画
damping:阻尼,范围0-1,阻尼越接近于0,弹性效果越明显
velocity:弹性复位的速度
*/
[UIView animateWithDuration:5.0f delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
//这里写执行的动画
} completion:^(BOOL finished) {
//动画完成后的操作
}];
//转场的block动画
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[UIView transitionWithView:imageView duration:4.0f options:UIViewAnimationOptionCurveLinear | UIViewAnimationTransitionFlipFromLeft animations:^{
//动画内容
} completion:^(BOOL finished) {
//动画完成的操作
}];