[控件] LineAnimationView

LineAnimationView

效果

说明

水平循环无间隔播放动画效果,用于loading的界面

源码

https://github.com/YouXianMing/LineAnimationView

//
//  LineAnimationView.h
//  AnimationLine
//
//  Created by YouXianMing on 15/7/4.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef enum : NSUInteger {

    /**
     *  从左往右(默认)
     */
    LEFT_TO_RIGHT,

    /**
     *  从右往左
     */
    RIGHT_TO_LEFT,

} ELineAnimationType;

@interface LineAnimationView : UIView

/**
 *  动画时间间隔(默认时间为 1 秒)
 */
@property (nonatomic) NSTimeInterval      duration;

/**
 *  动画类型(默认为从左到右)
 */
@property (nonatomic) ELineAnimationType  animationType;

/**
 *  素材图片
 */
@property (nonatomic, strong) UIImage    *sourceImage;

/**
 *  开始执行动画
 */
- (void)startAnimation;

@end
//
//  LineAnimationView.m
//  AnimationLine
//
//  Created by YouXianMing on 15/7/4.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "LineAnimationView.h"

@interface LineAnimationView ()

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UIImageView *rightImageView;

@end

@implementation LineAnimationView

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame {

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

        [self defaultConfig];

        [self setup];
    }

    return self;
}

- (void)defaultConfig {

    self.layer.masksToBounds = YES;
}

- (void)setup {

    CGFloat width       = self.frame.size.width;
    CGFloat height      = self.frame.size.height;

    self.contentView    = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width * 2, height)];
    [self addSubview:self.contentView];

    self.leftImageView  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    [self.contentView addSubview:self.leftImageView];

    self.rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(width, 0, width, height)];
    [self.contentView addSubview:self.rightImageView];

    _animationType = LEFT_TO_RIGHT;
    _duration      = 1.f;
}

#pragma mark - 开始动画
- (void)startAnimation {

    if (_animationType == LEFT_TO_RIGHT) {

        CGFloat width          = self.frame.size.width;
        CGFloat height         = self.frame.size.height;

        CGRect startRect       = CGRectMake(0, 0, width * 2, height);
        CGRect endRect         = CGRectMake(-width, 0, width * 2, height);

        _contentView.frame     = startRect;

        CABasicAnimation *line = [CABasicAnimation animationWithKeyPath:@"bounds"];
        line.fromValue         = [NSValue valueWithCGRect:startRect];
        line.toValue           = [NSValue valueWithCGRect:endRect];
        line.timingFunction    = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        line.duration          = _duration;
        line.delegate          = self;

        _contentView.frame     = endRect;
        [_contentView.layer addAnimation:line forKey:nil];

    } else if (_animationType == RIGHT_TO_LEFT) {

        CGFloat width          = self.frame.size.width;
        CGFloat height         = self.frame.size.height;

        CGRect startRect       = CGRectMake(- width, 0, width * 2, height);
        CGRect endRect         = CGRectMake(0, 0, width * 2, height);

        _contentView.frame     = startRect;

        CABasicAnimation *line = [CABasicAnimation animationWithKeyPath:@"bounds"];
        line.fromValue         = [NSValue valueWithCGRect:startRect];
        line.toValue           = [NSValue valueWithCGRect:endRect];
        line.timingFunction    = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        line.duration          = _duration;
        line.delegate          = self;

        _contentView.frame     = startRect;
        [_contentView.layer addAnimation:line forKey:nil];

    }
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {

    [self startAnimation];
}

#pragma mark - 重写setter,getter方法
@synthesize sourceImage = _sourceImage;
- (void)setSourceImage:(UIImage *)sourceImage {

    _sourceImage          = sourceImage;
    _leftImageView.image  = sourceImage;
    _rightImageView.image = sourceImage;
}

- (UIImage *)sourceImage {

    return _sourceImage;
}

@end

细节

时间: 2024-08-27 03:34:02

[控件] LineAnimationView的相关文章

在DataGridView控件中实现冻结列分界线

我们在使用Office Excel的时候,有很多时候需要冻结行或者列.这时,Excel会在冻结的行列和非冻结的区域之间绘制上一条明显的黑线.如下图: (图1) WinForm下的DataGridView控件也能实现类似的冻结行或者列的功能(参见:http://msdn.microsoft.com/zh-cn/library/28e9w2e1(VS.85).aspx) ,但是呢,DataGridView控件默认不会在冻结列或者行的分界处绘制一个明显的分界线,这样的话,最终用户很难注意到当前有列或者

摆脱Login控件,自己定义登录操作

protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { //在登录过程中,程序自动使用login.aspx进行拦截 //验证用户通过后,自动返回拦截的位置 if (Membership.ValidateUser(this.TextBox1.Text, this.TextBox2.Text)) { this.Session["xh"] = this.TextBox1.Text; FormsAuth

python selenium 处理时间日期控件(十五)

测试过程中经常遇到时间控件,需要我们来选择日期,一般处理时间控件通过层级定位来操作或者通过调用js来实现. 1.首先我们看一下如何通过层级定位来操作时间控件. 通过示例图可以看到,日期控件是无法输入日期,点击后弹出日期列表供我们选择日期,自己找了一个日期控制演示一下,通过两次定位,选择了日期 #-*- coding:utf-8 -*- import time from selenium import webdriver driver = webdriver.Chrome() driver.get

Delphi XE10 dxLayoutControl 控件应用指南

http://www.cnblogs.com/Bonny.Wong/p/7440288.html DevExpress VCL套件是一套非常强大的界面控件,可惜关于Delphi开发方面的说明太少,有些控件使用起来一头雾水,不知从何下手.本节详细介绍在Delphi Xe10 Seattle中如何利用dxLayoutControl 控件来做界面布局. 1.  首先从工具箱面板中将dxLayoutControl放在Form上,设置2个关键属性如下: 属性 属性值 说明 Align alClient 一

Android自己定义控件之轮播图控件

背景 近期要做一个轮播图的效果.网上看了几篇文章.基本上都能找到实现,效果还挺不错,可是在写的时候感觉每次都要单独去又一次在Activity里写一堆代码.于是自己封装了一下.这里仅仅是做了下封装成一个控件,不必每次反复写代码了. 效果图 实现分析 轮播图的功能就是实现左右滑动的广告.图片信息展示,那我们就用ViewPager来实现,由于考虑到用户体验,我们还须要在以下加一个指示器来标示滑动到了第几张轮播图.指示器我们能够用一个线性布局来依据要展示的轮播图设置显示的View,我们要做这种一个控件没

[ ObjectListView ] - ListView的增强控件 - 前言 (翻译)

********************************************************************************** 原  标 题: A Much Easier to Use ListView 原文地址: https://www.codeproject.com/Articles/16009/A-Much-Easier-to-Use-ListView 翻       译: 于国栋 http://www.shannon.net.cn *********

MFC 加入背景图片并让控件背景透明

/*加入背景图片*/ BOOL CTOOLDlg::OnEraseBkgnd(CDC* pDC) { // TODO: 在此加入消息处理程序代码和/或调用默认值 CDialog::OnEraseBkgnd(pDC); HBITMAP   m_hBitmap; HDC           m_hBkDC; m_hBitmap   =   ::LoadBitmap(::GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP2)); m_hBkDC     =

[转] 基于C#的波形显示控件的实现

转自 基于C#的波形显示控件的实现[附完整源码下载] 编者记: 09年暑假正好在学院实验室呆了一段时间,做了个完整的上位机软件(具体实现:根据下位机的指令,实现通过串口来操纵下位机进行实验,并将采集的数据进行处理和保存,并以图形的方式显示),整个项目边学C# WinForm边设计,这个波形显示控件就是项目中的一部分,也花了自己绝大多数时间.此外,顺便将该波形显示控件当作自己毕业设计的内容,下文实际上是节选自自己的本科毕业论文,希望对大家能有所帮助.代码以及文章有疏漏.错误.不妥之处在所难免,欢迎

ASP.NET页面生命周期与控件生命周期

ASP.NET页面生命周期 (1)PreInit 预初始化(2)Init 初始化(3)InitComplete 初始化完成(4)PreLoad 预加载(5)Load 加载(6)LoadComplete 加载完成(7)PreRender 预输出(8)PreRenderComplete 预输出完成(9)Unload 卸载 ASP.NET控件生命周期 -- 实例化(Instantiate) 控件被页面或另一个控件通过调用它的构造器所实例化.这个步骤之后所列出的阶段,仅当控件加入控件树中才会发生. --