博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[控件] LineAnimationView
阅读量:5933 次
发布时间:2019-06-19

本文共 4197 字,大约阅读时间需要 13 分钟。

LineAnimationView

 

效果

 

说明

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

 

源码

////  LineAnimationView.h//  AnimationLine////  Created by YouXianMing on 15/7/4.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import 
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

 

细节

 

转载地址:http://qpctx.baihongyu.com/

你可能感兴趣的文章
vagrant box各种命令汇总
查看>>
界面只能输入数字和小数
查看>>
js 自定义事件 包含 添加、激活、销毁
查看>>
Redhat6.5——解决yum功能不能正常使用
查看>>
PowerDesigner逆向生成MYSQL数据库表结构总结
查看>>
[转]Angular4---部署---将Angular项目部署到IIS上
查看>>
The type groovy.lang.GroovyObject cannot be resolved
查看>>
Oracle 如何将“26-9月 -17 06.46.00.000000000 下午”字符串转换成标准日期格式
查看>>
Java 虚拟机类加载器
查看>>
利用shell脚本批量提交网站404死链给百度
查看>>
为什么我们做分布式使用Redis?
查看>>
elasticsearch 入门
查看>>
局域网内的机器不能ping通虚拟机,但是虚拟机可以ping通局域网的机器
查看>>
django中将model转换为dict的方法
查看>>
最完美解决Nginx部署ThinkPHP项目的办法
查看>>
容器网络——从CNI到Calico
查看>>
《性能测试二三谈》系列
查看>>
在Winform开发框架中使用DevExpress的内置图标资源
查看>>
点评cat系列-应用集成
查看>>
.netcore使用SocketAsyncEventArgs Pool需要注意!
查看>>