实现一个iOS晃动动画

有时候在开发中, 需要我们实现一个晃动动画,

达到一个提示的效果,如下图所示

思路, 我们要实现的本质上是一个旋转动画,然后

设置一个旋转角度,以底部中间为中心旋转,

左右各有一个旋转的角度,并且旋转角度逐渐变小,

动画速度逐渐变快,即时间间隔逐渐减小

代码

复制代码
#define radian(angle) ((angle) / 180.0 * M_PI)
- (void)setUpUI
{
    [self configCorners:UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomRight radius:6.5];
    self.layer.backgroundColor = [UIColor redColor].CGColor;
    self.textColor = [UIColor whiteColor];
    self.font = [UIFont systemFontOfSize:9];
    self.textAlignment = NSTextAlignmentCenter;
    self.layer.anchorPoint = CGPointMake(0.5, 1);
    self.layer.masksToBounds = YES;
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    self.layer.position = CGPointMake(width / 2.f - 15, height/2.f + 5);
    [self addMoveAnimation:self];
   
}
    
- (void)addMoveAnimation:(UIView *)view
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.delegate = self;
    animation.keyPath = @"transform.rotation";
    animation.values = @[@(radian(0)), @(radian(-25)), @(radian(0)), @(radian(25)),@(radian(0)),@(radian(-20)), @(radian(0)), @(radian(20)),@(radian(0)),@(radian(-10)), @(radian(0)), @(radian(10)),@(radian(0)),@(radian(-5)), @(radian(0)), @(radian(5)),@(radian(0)),];
    animation.keyTimes = @[@0, @0.15, @0.3 , @0.45, @0.6, @0.7,@0.74, @0.78, @0.82, @0.86, @0.89, @0.92, @0.94, @0.96, @0.98,@0.99, @1];
    animation.duration = 1.2;
    // 动画的重复执行次数
    animation. repeatCount = 1;
    // 保持动画执行完毕后的状态
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeRemoved;
    [view.layer addAnimation:animation forKey:@"shake_animation"];
}

#pragma mark - 动画代理

- (void)animationDidStop: (CAAnimation *)animation finished:(BOOL)flag
{
  dispatshafter_ 2, NSOperationQueuePriorityHigh, ^{
        [self addMoveAnimation:self];
    });
}
相关推荐
Mr -老鬼39 分钟前
移动端跨平台适配技术框架:从发展到展望
android·ios·小程序·uni-app
tiantian_cool17 小时前
Claude Code 四大核心技能使用指南
ios
冰淇淋真好吃21 小时前
iOS实现 WKWebView 长截图的优雅方案
ios
前端不太难1 天前
Flutter / RN / iOS,在长期维护下的性能差异本质
flutter·ios
搜狐技术产品小编20232 天前
精通 UITableViewDiffableDataSource——从入门到重构的现代 iOS 列表开发指南
ios·重构
tangweiguo030519872 天前
SwiftUI 状态管理完全指南:从 @State 到 @EnvironmentObject
ios
Digitally2 天前
如何轻松地将文件从 PC 传输到 iPhone
ios·iphone
iosTiov2 天前
当IPA遇见信任:解密ios生态中“签名”的真正力量
ios·团队开发·苹果签名·稳定
游戏开发爱好者82 天前
如何使用 AppUploader 提交上传 iOS 应用
android·ios·小程序·https·uni-app·iphone·webview
和沐阳学逆向2 天前
iOS 18 越狱教程:palera1n + 巨魔安装全流程
ios·巨魔商店·ios越狱·ios18越狱