实现一个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];
    });
}
相关推荐
TouchWorld9 分钟前
iOS逆向-哔哩哔哩增加3倍速播放(2)-[横屏视频-半屏播放]增加3倍速播放
ios·swift
1024小神1 小时前
xcode 中配置AR Resource Group并设置图片宽度等
ios·swiftui·ar·xcode·swift
如此风景16 小时前
iOS SwiftUI开发所有修饰符使用详解
ios
mumuWorld16 小时前
KSCrash 实现机制深度分析
ios·源码阅读
AskHarries17 小时前
Google 登录问题排查指南
flutter·ios·app
崽崽长肉肉18 小时前
Swift中的知识点总结
ios·swift
2501_9160074719 小时前
苹果手机iOS应用管理全指南与隐藏功能详解
android·ios·智能手机·小程序·uni-app·iphone·webview
2501_9151063221 小时前
全面理解 iOS 帧率,构建从渲染到系统行为的多工具协同流畅度分析体系
android·ios·小程序·https·uni-app·iphone·webview
TouchWorld1 天前
iOS逆向-哔哩哔哩增加3倍速(1)-最大播放速度
ios·逆向
RollingPin1 天前
React Native与Flutter的对比
android·flutter·react native·ios·js·移动端·跨平台开发