线性重复动画

线性重复动画

 

效果

 

说明

线性重复的动画可以用在以下的一些场景:

1)线性加载效果(如上图)

2)下载箭头的循环出现

 

源码

https://github.com/YouXianMing/Animations

//
//  ReplicatorLineAnimationView.h
//  Animations
//
//  Created by YouXianMing on 16/4/12.
//  Copyright  2016年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef enum : NSUInteger {

    kReplicatorLeft,
    kReplicatorRight,
    kReplicatorUp,
    kReplicatorDown

} EReplicatorLineDirection;

@interface ReplicatorLineAnimationView : UIView

/**
 *  Animation's direction.
 */
@property (nonatomic) EReplicatorLineDirection  direction;

/**
 *  Animation's speed.
 */
@property (nonatomic) CGFloat           speed;

/**
 *  Animation's image.
 */
@property (nonatomic, strong) UIImage  *image;

/**
 *  Start animation.
 */
- (void)startAnimation;

@end
//
//  ReplicatorLineAnimationView.m
//  Animations
//
//  Created by YouXianMing on 16/4/12.
//  Copyright  2016年 YouXianMing. All rights reserved.
//

#import "ReplicatorLineAnimationView.h"

@interface ReplicatorLineAnimationView () {

    CAReplicatorLayer *_replicatorLayer;
    CALayer           *_animationLayer;
    NSString          *_animationKeyPath;
    CGFloat            _animationToValue;
    CGFloat            _offsetX;
    CGFloat            _offsetY;
    CATransform3D      _instanceTransform;
    BOOL               _startAnimation;
}

@end

@implementation ReplicatorLineAnimationView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

        self.speed             = 2.f;
        _replicatorLayer       = [CAReplicatorLayer layer];
        _replicatorLayer.frame = self.bounds;
        [self.layer addSublayer:_replicatorLayer];

        _animationLayer       = [CALayer layer];
        _animationLayer.frame = self.bounds;
        [_replicatorLayer addSublayer:_animationLayer];

        self.layer.masksToBounds = YES;

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(eventDidBecomeActive:)
                                                     name:UIApplicationDidBecomeActiveNotification object:nil];
    }

    return self;
}

- (void)startAnimation {

    _startAnimation = YES;

    if (_animationKeyPath.length) {

        [_animationLayer removeAnimationForKey:_animationKeyPath];
    }

    [self dealWithTheEReplicatorLineDirection];

    _replicatorLayer.instanceCount      = 2;
    _replicatorLayer.instanceTransform  = _instanceTransform;
    _animationLayer.contents            = (__bridge id _Nullable)(self.image.CGImage);

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:_animationKeyPath];
    animation.toValue           = @(_animationToValue);
    animation.duration          = 1.f / self.speed;
    animation.timingFunction    = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.repeatCount       = HUGE_VALF;
    [_animationLayer addAnimation:animation forKey:_animationKeyPath];
}

- (void)dealWithTheEReplicatorLineDirection {

    if (_direction == kReplicatorLeft || _direction == kReplicatorRight) {

        _animationKeyPath  = @"position.x";
        _offsetX           = _direction == kReplicatorLeft ? self.frame.size.width : -self.frame.size.width;
        _offsetY           = 0;
        _animationToValue  = _animationLayer.position.x - _offsetX;
        _instanceTransform = CATransform3DMakeTranslation(_offsetX, 0.0, 0.0);

    } else if (_direction == kReplicatorUp || _direction == kReplicatorDown) {

        _animationKeyPath  = @"position.y";
        _offsetX           = 0;
        _offsetY           = _direction == kReplicatorUp ? self.frame.size.height : -self.frame.size.height;
        _animationToValue  = _animationLayer.position.y - _offsetY;
        _instanceTransform = CATransform3DMakeTranslation(0.0, _offsetY, 0.0);
    }
}

- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)eventDidBecomeActive:(id)obj {

    NSNotification *fication = obj;

    if ([fication.name isEqualToString:UIApplicationDidBecomeActiveNotification]) {

        if (_startAnimation == YES) {

            [self startAnimation];
        }
    }
}

@end

细节

线性重复动画是有着方向性的,他有4个方向可供你使用:

你需要设置方向值、速度值以及一张可以循环显示的图片,对图片也是有要求的,图片的话需要保证平移的时候可以无缝衔接:

CALayer的相关动画会在进入后台的时候自动移除掉了,所以,从后台进入前台的时候需要手动开启动画:

以下是核心所在:

时间: 2024-08-03 15:56:51

线性重复动画的相关文章

JQuery中解决重复动画的方法_jquery

下面我们就来总结一下几个JQuery中解决重复动画的办法 ,这里有几个常见的动画效果,它们分别是:图片滚动轮播,回到页面顶部,联级菜单滑出,手风琴. 图片滚动: $("#banner-min-img1").mouseover(function (){ $("#banner-img").animate({left:"0px"},300)} $("#banner-min-img2").mouseover(function (){

Silverlight:使用 XAML 和 Expression Blend 创建动画

原文使用 XAML 和 Expression Blend 创建动画                                        Lawrence Moroney                                                                                                           本文将介绍以下内容:                                            

Android动画(一)Interpolator

目录[+] 一:简介 interpolator可以翻译成插值器. Android中interpolator最底层的接口如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 packageandroid.animation;   /**  * 时间插值器定义了一个动画的变化率.  * 这让动画让非线性的移动轨迹,例如加速和减速.  * <hr/>  * A time interpolato

Android开发艺术探索——第七章:Android动画深入分析

Android开发艺术探索--第七章:Android动画深入分析 Android的动画可以分成三种,view动画,帧动画,还有属性动画,其实帧动画也是属于view动画的一种,,只不过他和传统的平移之类的动画不太一样的是表现形式上有点不一样,view动画是通过对场景的不断图像交换而产生的动画效果,而帧动画就是播放一大段图片,很显然,图片多了会OOM,属性动画通过动态的改变对象的属性达到动画效果,也是api11的新特性,在低版本无法使用属性动画,但是我们依旧有一些兼容库,OK,我们还是继续来看下详细

SVG Animation动画

SVG动画示例 下面是一个简单的SVG动画的例子: <svg width="500" height="100"> <rect x="10" y="10" height="110" width="110" style="stroke:#ff0000; fill: #0000ff"> <animateTransform attributeNa

巧用PPT2010中的合并及动画刷做大量简报

本文介绍如何巧妙利用PowerPoint2010中的合并以及动画刷,在短时间内快速制作大量内容基本上重复的PPT演示文档. 年终将至,为了向公司员工和各级领导做年终汇报,总需要把各种报告.图表.表格数据制作成幻灯片同步演示以加强汇报效果.通常这类总结演示都存在大量相同或相似的内容.格式或动画要求,因此在制作时总要大量重复相同操作,这要浪费不少时间.有没有什么办法能减少重复操作,让我们更快速地完成总结演示的制作呢?在PPT 2010中确实有几种方法可以有效减少重复操作. 一.重用相同幻灯片 打开P

模拟自然动画的精髓——TimeInterpolator与TypeEvaluator

模拟自然动画的精髓--TimeInterpolator与TypeEvaluator 在今天的文章开始之前,有个忙想请大家帮一下,希望在京东.淘宝.当当.亚马逊购买了我的书<Android群英传:神兵利器>的朋友们,帮忙去网店上给个简短的评价,举手之劳,还是多谢大家啦~~ 本文绘图软件 https://www.desmos.com/calculator 通过属性动画,我们可以模拟各种属性的动画效果,但对于这些属性来说,动画变化的速率和范围,是实现一个更加『真实.自然』的动画的基础,这两件事情,就

iOS UIView动画详解(Objective-C)

    我在之前的一篇博客中<iOS UIView动画详解(Swift)>讲解了使用Swift来实现UIView类下面提供的多种动画效果,如位置动画.旋转动画.缩放动画.颜色动画.透明度动画等等.为了这个题目的完整性,今天我使用Objective-C来完全重写以上的所有的动画.项目案例已经上传至:https://github.com/chenyufeng1991/iOS-UIView-Animation  中的Animation-OC文件夹下,另一个目录下则是Swift实现的动画. (1)位置

Android Frame动画

//主Activity如下 package cn.com; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; //Frame Animation步骤 //1