使用前
需引入QuartzCore.framework, 并在相关文件中加入 #import "QuartzCore/QuartzCore.h"
定义
shakeFeedbackOverlay为UIImageView
设置
self.shakeFeedbackOverlay.alpha = 0.0;
self.shakeFeedbackOverlay.layer.cornerRadius = 10.0; //设置圆角半径
1、图像左右抖动
CABasicAnimation* shake = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
shake.fromValue = [NSNumber numberWithFloat:-M_PI/32];
shake.toValue = [NSNumber numberWithFloat:+M_PI/32];
shake.duration = 0.1;
shake.autoreverses = YES; //是否重复
shake.repeatCount = 4;
[self.shakeFeedbackOverlay.layer addAnimation:shake forKey:@"shakeAnimation"];
self.shakeFeedbackOverlay.alpha = 1.0;
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction animations:^{ self.shakeFeedbackOverlay.alpha = 0.0; //透明度变0则消失 } completion:nil];
2、图像顺时针旋转
CABasicAnimation* shake = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
shake.fromValue = [NSNumber numberWithFloat:0];
shake.toValue = [NSNumber numberWithFloat:2*M_PI];
shake.duration = 0.8; shake.autoreverses = NO;
shake.repeatCount = 1;
[self.shakeFeedbackOverlay.layer addAnimation:shake forKey:@"shakeAnimation"];
self.shakeFeedbackOverlay.alpha = 1.0;
[UIView animateWithDuration:10.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction animations:^{ self.shakeFeedbackOverlay.alpha = 0.0; } completion:nil];
3、图像关键帧动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
CGMutablePathRef aPath = CGPathCreateMutable();
CGPathMoveToPoint(aPath, nil, 20, 20);
CGPathAddCurveToPoint(aPath, nil, 160, 30, 220, 220, 240, 420);
animation.path = aPath;
animation.autoreverses = YES;
animation.duration = 2;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
animation.rotationMode = @"auto";
[ballView.layer addAnimation:animation forKey:@"position"];
4、组合动画 CAAnimationGroup
CABasicAnimation *flip = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
flip.toValue = [NSNumbernumberWithDouble:-M_PI];
CABasicAnimation *scale= [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scale.toValue = [NSNumbernumberWithDouble:12];
scale.duration = 1.5;
scale.autoreverses = YES;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:flip, scale, nil];
group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
group.duration = 3;
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[ballView.layer addAnimation:group forKey:@"position"];
5、指定时间内旋转图片
//启动定时器旋转光圈
- (void)startRotate
{
self.rotateTimer = [NSTimer scheduledTimerWithTimeInterval:0.02
target:self
selector:@selector(rotateGraduation)
userInfo:nil
repeats:YES];
}
//关闭定时器
- (void)stopTimer
{
if ([self.rotateTimerisValid])
{
[self.rotateTimerinvalidate];
self.rotateTimer = nil;
}
}
//旋转动画
- (void)rotateGraduation
{
self.timeCount--;
if (self.timeCount == 0)
{
[self stopTimer];
// doSomeThing //旋转完毕 可以干点别的
self.timeCount = 25;
}
else
{
//计算角度 旋转
static CGFloat radian = 150 * (M_2_PI / 360);
CGAffineTransform transformTmp = self.lightImageView.transform;
transformTmp = CGAffineTransformRotate(transformTmp, radian);
self.lightImageView.transform = transformTmp;
};
}
调用方法
self.timeCount = 25; //动画执行25次
[self startRotate];
注:本文部分代码摘抄自《ios5核心框架》一书,原文有改动
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库