实现一个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];
    });
}
相关推荐
HarderCoder3 小时前
iOS 知识积累第一弹:从 struct 到 APP 生命周期的全景复盘
ios
叽哥14 小时前
Flutter Riverpod上手指南
android·flutter·ios
用户092 天前
SwiftUI Charts 函数绘图完全指南
ios·swiftui·swift
YungFan2 天前
iOS26适配指南之UIColor
ios·swift
权咚2 天前
阿权的开发经验小集
git·ios·xcode
用户092 天前
TipKit与CloudKit同步完全指南
ios·swift
法的空间3 天前
Flutter JsonToDart 支持 JsonSchema
android·flutter·ios
2501_915918413 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张3 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
Magnetic_h3 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa