iOS 动画 —— 活动加载器

平常我们在加载数据的时候通常用UIActivityIndicatorView, 但是有时产品想改改样式,在此刚好看到几款动画兴许可以当做活动加载器,特此学习下。

学习来源:DTIActivityIndicatorView-Swift,在此用Objective-C模仿一下。

一、圈圈的跳动
pulse
#import "PulseView.h"

@interface PulseView ()

@property (nonatomic, strong) UIView *pulseView;

@end

@implementation PulseView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        CGSize pulseSize = CGRectInset(frame, 2.0, 2.0).size;
        _pulseView = [[UIView alloc] init];
        _pulseView.backgroundColor = [UIColor whiteColor];
        _pulseView.frame = CGRectMake(0.0, 0.0, pulseSize.width, pulseSize.height);
        _pulseView.layer.cornerRadius  = MIN(pulseSize.width,pulseSize.height)/2.0;
        [self addSubview:_pulseView];
        
    }
    return self;
}

- (void)startAnimating {
    
    CAKeyframeAnimation *scaleAnimation =  [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.values = @[@0,@1];
    
    CAKeyframeAnimation *opacityAnimation =  [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.values = @[@1,@0];
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.duration = 1.0;
    group.repeatCount = MAXFLOAT;
    group.removedOnCompletion = NO;
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    group.animations = @[scaleAnimation, opacityAnimation];
    [self.pulseView.layer addAnimation:group forKey:@"Animation_Pulse"];
    
}

@end
二、变换的跳动
doublw
#import "DoubleBounceView.h"

@interface DoubleBounceView ()

@property (nonatomic, strong) UIView *bounceOutView;
@property (nonatomic, strong) UIView *bounceInView;

@end

@implementation DoubleBounceView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        CGSize size = CGRectInset(frame, 2.0, 2.0).size;
        _bounceInView = [self makeBounceViewWithSize:size];
        _bounceOutView = [self makeBounceViewWithSize:size];
        [self addSubview:_bounceOutView];
        [self addSubview:_bounceInView];
    }
    return self;
}

- (UIView *)makeBounceViewWithSize:(CGSize)size {
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, size.width, size.height);
    view.backgroundColor = [UIColor whiteColor];
    view.layer.cornerRadius = MIN(size.width,size.height)/2.0;
    view.layer.opacity = 0.6;
    return view;
}

- (void)startAnimating {
    
    CAKeyframeAnimation *scaleInAnimation =  [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    scaleInAnimation.values = @[@0, @1, @0];
    scaleInAnimation.removedOnCompletion = NO;
    scaleInAnimation.repeatCount = CGFLOAT_MAX;
    scaleInAnimation.duration = 2;
    scaleInAnimation.timingFunctions = @[
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                  ];

    
    CAKeyframeAnimation *scaleOutAnimation =  [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    scaleOutAnimation.values = @[@1, @0, @1];
    scaleOutAnimation.removedOnCompletion = NO;
    scaleOutAnimation.repeatCount = MAXFLOAT;
    scaleOutAnimation.duration = 2;
    scaleOutAnimation.timingFunctions = @[
                                         [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                         [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                         [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
           
                                         ];
    [self.bounceOutView.layer addAnimation:scaleOutAnimation forKey:@"scaleOutAnimation"];
    [self.bounceInView.layer addAnimation:scaleInAnimation forKey:@"scaleInAnimation"];
 
}


@end

三、琴块的变化
wave.gif
#import "WaveView.h"

static const NSInteger kRectCount = 5;
static const CGFloat kSpaceBetweenRect = 3.0;

@interface WaveView ()

@property (nonatomic, strong) UIView *rectView;

@end

@implementation WaveView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    
        _rectView = [[UIView alloc] initWithFrame: frame];
        [self addSubview:_rectView];
        
        CGFloat rectWidth = (frame.size.width - kSpaceBetweenRect *(kRectCount-1)) / kRectCount;
        for (NSInteger i = 0; i < kRectCount; ++i) {
            CALayer *layer = [CALayer layer];
            layer.transform =  CATransform3DMakeScale(1.0, 0.4, 0.0);
            layer.backgroundColor = [UIColor whiteColor].CGColor;
            layer.frame = CGRectMake(i * (rectWidth + kSpaceBetweenRect), 0, rectWidth, frame.size.height);
            [_rectView.layer addSublayer:layer];
        }
        
    }
    return self;
}

- (void)startAnimating {
    
    for (NSInteger i = 0; i < kRectCount; ++i) {
        
        CALayer *layer = self.rectView.layer.sublayers[i];
        
        CAKeyframeAnimation *animation =  [CAKeyframeAnimation animationWithKeyPath:@"transform"];
        animation.removedOnCompletion = NO;
        animation.repeatCount = MAXFLOAT;
        animation.duration = 1.2;
        
        animation.beginTime = CACurrentMediaTime() + i * 0.1;
        animation.keyTimes = @[@0.0,@0.2, @0.4,@1.0];
        animation.timingFunctions = @[
                                      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                      ];
        animation.values = @[
                             [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 0.4, 0.0)],
                             [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 0.0)],
                             [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 0.4, 0.0)],
                             [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 0.4, 0.0)]
                             ];
    
        [layer addAnimation:animation forKey:[NSString stringWithFormat:@"wave_animation_%lu",i]];
        
    }
   
}

@end

四、方块的旋转
Rota.gif
#import "RotatingView.h"

@interface RotatingView ()

@property (nonatomic, strong) UIView *planeView;

@end

@implementation RotatingView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
 
        CGSize size = CGRectInset(frame, 2.0, 2.0).size;
        _planeView = [[UIView alloc] init];
        _planeView.backgroundColor = [UIColor whiteColor];
        _planeView.frame = CGRectMake(0.0, 0.0, size.width, size.height);
        [self addSubview:_planeView];
    }
    return self;
}


CATransform3D transformPlane(CGFloat perspective, CGFloat angle,
                             CGFloat x, CGFloat y, CGFloat z) {
    CATransform3D translation = CATransform3DIdentity;
    translation.m34 = perspective;
    return CATransform3DRotate(translation, angle, x, y, z);
    
}

- (void)startAnimating {
    
    CAKeyframeAnimation *animation =  [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.removedOnCompletion = NO;
    animation.repeatCount = MAXFLOAT;
    animation.duration = 2;
    animation.keyTimes = @[@0.0,@0.45,@0.95];
    animation.timingFunctions = @[
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                  ];
    animation.values = @[
                         [NSValue valueWithCATransform3D:transformPlane(1.0/120.0, 0.0, 0.0, 0.0, 0.0)],
                          [NSValue valueWithCATransform3D:transformPlane(1.0/120.0, (CGFloat)M_PI, 0.0, 1.0, 0.0)],
                          [NSValue valueWithCATransform3D:transformPlane(1.0/120.0, (CGFloat)M_PI, 0.0, 0.0, 1.0)]
                         ];
  
    [self.planeView.layer addAnimation:animation forKey:@"Animation_Plane"];
}

@end

五、二圆旋转飞
Chasing.gif
#import "ChasingView.h"

@interface ChasingView ()

@property (nonatomic, strong) UIView *dotOneView;
@property (nonatomic, strong) UIView *dotTwoView;

@end

@implementation ChasingView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.layer.shouldRasterize = YES;
        self.backgroundColor = [UIColor lightGrayColor];
        CGFloat sizeWidth = frame.size.width * 3/5;
        _dotOneView = [self makeDotViewWithSize:CGRectMake((frame.size.width - sizeWidth)/2.0, 1.0, sizeWidth, sizeWidth)];
        _dotTwoView = [self makeDotViewWithSize:CGRectMake((frame.size.width - sizeWidth)/2.0, frame.size.height = sizeWidth, sizeWidth, sizeWidth)];
        [self addSubview:_dotOneView];
        [self addSubview:_dotTwoView];
        
    }
    return self;
}

- (UIView *)makeDotViewWithSize:(CGRect)rect {
    UIView *view = [[UIView alloc] init];
    view.frame = rect;
    view.backgroundColor = [UIColor whiteColor];
    view.layer.cornerRadius = MIN(rect.size.width,rect.size.height)/2.0;
    return view;
}

- (void)startAnimating {
    
    CABasicAnimation *ratotionAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    ratotionAnimation.fromValue = @0;
    ratotionAnimation.toValue = @(2 * M_PI);
    ratotionAnimation.removedOnCompletion = NO;
    ratotionAnimation.repeatCount = MAXFLOAT;
    ratotionAnimation.duration = 2.0;
    ratotionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [self.layer addAnimation:ratotionAnimation forKey:@"chasing_rotation"];
    
    
    CAKeyframeAnimation *scaleOneAnimation =  [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    scaleOneAnimation.values = @[@1, @0, @1];
    scaleOneAnimation.removedOnCompletion = NO;
    scaleOneAnimation.repeatCount = CGFLOAT_MAX;
    scaleOneAnimation.duration = 2;
    scaleOneAnimation.timingFunctions = @[
                                          [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                          [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                          [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                          
                                          ];
    
    CAKeyframeAnimation *scaleTwoAnimation =  [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    scaleTwoAnimation.values = @[@0, @1, @0];
    scaleTwoAnimation.removedOnCompletion = NO;
    scaleTwoAnimation.repeatCount = CGFLOAT_MAX;
    scaleTwoAnimation.duration = 2;
    scaleTwoAnimation.timingFunctions = @[
                                          [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                          [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                          [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                          
                                          ];
    [self.dotOneView.layer addAnimation:scaleOneAnimation forKey:@"chasing_one_scale"];
    [self.dotTwoView.layer addAnimation:scaleTwoAnimation forKey:@"chasing_two_scale"];
    
}

@end
六、三个点
threePoint.gif
#import "SpotifyView.h"

static const NSInteger kCircleCount = 3;

@interface SpotifyView ()

@property (nonatomic, strong) UIView *circleView;

@end

@implementation SpotifyView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        _circleView = [[UIView alloc] initWithFrame: frame];
        [self addSubview:_circleView];
        
        CGFloat circleWidth = frame.size.width / (kCircleCount*2 + 1);
        CGFloat posY = (frame.size.height - circleWidth)/2;
        for (NSInteger i = 0; i < kCircleCount; ++i) {
            CALayer *layer = [CALayer layer];
            layer.backgroundColor = [UIColor whiteColor].CGColor;
            layer.cornerRadius = circleWidth/2;
            layer.frame = CGRectMake(circleWidth + i*(circleWidth*2), posY, circleWidth, circleWidth);
            [_circleView.layer addSublayer:layer];
        }
        
    }
    return self;
}

- (void)startAnimating {
    
    for (NSInteger i = 0; i < kCircleCount; ++i) {
        
        CALayer *layer = self.circleView.layer.sublayers[i];

        CAKeyframeAnimation *animation =  [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        animation.removedOnCompletion = NO;
        animation.repeatCount = MAXFLOAT;
        animation.duration = 1.2;
        animation.values = @[@1.0, @1.7, @1.0, @1.0];
        animation.beginTime = CACurrentMediaTime() + i * 0.2;
        animation.keyTimes = @[@0.0,@0.2, @0.4,@1.0];
        animation.timingFunctions = @[
                                      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                      ];
        
        [layer addAnimation:animation forKey:[NSString stringWithFormat:@"spotify_animation_%lu",i]];
        
    }
    
}

@end

七、网易刷新圈圈(额外需要图片的)
网易刷新圈圈
#import "ShowView.h"

static const CGFloat kCicleEdge  = 7.0f;

@interface ShowView ()

@property (nonatomic, strong) CALayer *ballLayer;
@property (nonatomic, strong) CALayer *circleLayer;

@end

@implementation ShowView

- (instancetype)initWithFrame:(CGRect)frame {
    if ([super initWithFrame:frame]) {
        
        UIImage *ballImage = [UIImage imageNamed:@"ball"]; // size == 30 * 30
        UIImage *circleImage =  [UIImage imageNamed:@"circle"]; // size == 100 * 30
        
        _ballLayer = [CALayer layer];
        _ballLayer.frame = CGRectMake(0, 0, ballImage.size.width - kCicleEdge, ballImage.size.height - kCicleEdge);
        _ballLayer.position = CGPointMake(self.center.x, _ballLayer.position.y);
        _ballLayer.contents = (id)ballImage.CGImage;
        
        _circleLayer = [CALayer layer];
        _circleLayer.frame = CGRectMake(0, 0, circleImage.size.width, circleImage.size.height);
        _circleLayer.position = CGPointMake(self.center.x, _ballLayer.position.y - kCicleEdge);
        _circleLayer.contents = (id)circleImage.CGImage;

        [self.layer addSublayer:_ballLayer];
        [self.layer insertSublayer:_circleLayer below:_ballLayer];
    }
    return self;
}

// 增加动画
- (void) startAnimating  {
    // 变大
    CABasicAnimation *enlarge = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    enlarge.duration = 0.5f;
    enlarge.repeatCount = 1.0f;
    enlarge.removedOnCompletion = NO;
    enlarge.fromValue = @(0.5f);
    enlarge.toValue = @(1.0f);
    
    // 变小
    CABasicAnimation *decrease = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    decrease.duration = 0.5f;
    decrease.repeatCount = 1.0f;
    decrease.removedOnCompletion = NO;
    decrease.beginTime = 0.5f;
    decrease.fromValue = @(1.0f);
    decrease.toValue = @(0.5f);
    
    // 位置
    CABasicAnimation *position = [CABasicAnimation animationWithKeyPath:@"position"];
    position.duration = 1.0f;
    position.repeatCount = MAXFLOAT;
    position.fromValue =[NSValue valueWithCGPoint:self.circleLayer.position];
    position.toValue = [NSValue valueWithCGPoint:CGPointMake(self.circleLayer.position.x, self.circleLayer.position.y + self.circleLayer.frame.size.height)];
    
    // 将三组动画组合成
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.duration = 1;
    group.repeatCount = MAXFLOAT;
    group.removedOnCompletion = NO;
    group.autoreverses = YES;
    group.animations = @[enlarge, decrease, position];
    [self.circleLayer addAnimation:group forKey:@"Animation"];
}

// 移除动画
- (void)removeAnimation {
     [self.circleLayer removeAnimationForKey:@"Animation"];
}

@end

PS:上述尝试实现,都是直接用 frame 的!用自动布局尝试直接修改里面的chu

重点是看startAnimating 中的实现!

PS: 另外同时学习 Swift ,直接去看 DTIActivityIndicatorView-Swift 里面的实现是很有必要,虽说好久没更新啦。

同时通过上面几组的学习尝试,更加了体会到了一般动画的构成。

  • CALayer === > Who 事件是谁做 ;
  • CAAnimation ===> How 事件怎么做;
  • AddAnimation ===> Add 将谁做和做怎么结合起来。

虽说上述几组动画都是一些基本的简单动画,但是任何复杂的动画都是由简单动画构成,所以再次提醒自己动画并没有想象中那么难!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容

  • 清,河水悠悠小桨行,扬帆过,微漾细纹惊 缘,三世齐眉戏子怜,何须唱,扇面画桑田 幽,山野清风鹤鸟啾,狼嚎尽,人坐醉...
    易清幽阅读 3,002评论 0 3
  • 导语:在职场上没有所谓的公平与不公平,所以吃亏是难免的。有的时候领导或同事做错的事情却要你来承担,这种黑锅当然人人...
    浮生乐事阅读 1,097评论 0 0
  • 婚前,快餐店是我解决吃饭的地方。 婚后,家里的厨房设备齐全,我便很少外食。 可是,我懒散惯了,只会吃不会做。老公见...
    玄玄被用了阅读 473评论 7 4