zoukankan      html  css  js  c++  java
  • 第四十八篇、呼吸灯动画

    #import "XXTwinkleView.h"
    static CGFloat const twinkleWidth = 30;
    static CGFloat const centerWidth = 8;
    @interface XXTwinkleView ()
    /** 闪动的 view */
    @property(nonatomic, strong) UIView *twinkleView;
    /** 占位的 view */
    @property(nonatomic, strong) UIView *centerView;
    /** 动画组 */
    @property(nonatomic, strong) CAAnimationGroup * groups;
    /** 闪烁的点颜色 */
    @property(nonatomic, strong) UIColor *twinkleColor;
    @end
    @implementation XXTwinkleView
    
    - (instancetype)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            self.backgroundColor = [UIColor clearColor];
        }
        return self;
    }
    
    - (instancetype)initWithColor:(UIColor *)color {
        if (self = [super init]) {
            self.twinkleColor = color;
        }
        return self;
    }
    
    - (void)didMoveToSuperview {
        [super didMoveToSuperview];
        [self startFlashAnimation];
    }
    
    - (UIView *)twinkleView {
        if (!_twinkleView) {
            _twinkleView =  [[UIView alloc] init];
            [self addSubview:_twinkleView];
            _twinkleView.backgroundColor = self.twinkleColor? self.twinkleColor : [UIColor whiteColor];
            _twinkleView.layer.cornerRadius = twinkleWidth * 0.5;
            _twinkleView.alpha = 0;
        }
        return _twinkleView;
    }
    
    - (UIView *)centerView {
        if (!_centerView) {
            _centerView =  [[UIView alloc] init];
            [self addSubview:_centerView];
            _centerView.backgroundColor = self.twinkleColor? self.twinkleColor : [UIColor whiteColor];
            _centerView.layer.cornerRadius = centerWidth * 0.5;
        }
        return _centerView;
    }
    
    - (CAAnimationGroup *)groups {
        if (!_groups) {
            // 缩放动画
            CABasicAnimation * scaleAnim = [CABasicAnimation animation];
            scaleAnim.keyPath = @"transform.scale";
            scaleAnim.fromValue = @0.1;
            scaleAnim.toValue = @1;
            scaleAnim.duration = 2;
            // 透明度动画
            CABasicAnimation *opacityAnim=[CABasicAnimation animationWithKeyPath:@"opacity"];
            opacityAnim.fromValue= @1;
            opacityAnim.toValue= @0.1;
            opacityAnim.duration= 2;
            // 创建动画组
            _groups =[CAAnimationGroup animation];
            _groups.animations = @[scaleAnim,opacityAnim];
            _groups.removedOnCompletion = NO;
            _groups.fillMode = kCAFillModeForwards;
            _groups.duration = 2;
            _groups.repeatCount = FLT_MAX;
        }
        return _groups;
    }
    
    - (void)layoutSubviews {
        [super layoutSubviews];
        CGFloat width = self.bounds.size.width;
        CGFloat height = self.bounds.size.height;
        self.centerView.frame = CGRectMake((width - centerWidth) * 0.5, (height - centerWidth) * 0.5, centerWidth, centerWidth);
        self.twinkleView.frame = CGRectMake((width - twinkleWidth) * 0.5, (height - twinkleWidth) * 0.5, twinkleWidth, twinkleWidth);
    }
    
    /**
     开始呼吸动画
     */
    - (void)startFlashAnimation {
         [self.twinkleView.layer addAnimation:self.groups forKey:@"groups"];
    }
    
    /**
     结束呼吸动画
     */
    - (void)stopFlashAnimation {
        [self.twinkleView.layer removeAllAnimations];
    }
    @end
  • 相关阅读:
    Asp.net_解决vs运行报在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题的bug方法
    WebConfig常用节点
    编译器错误消息:必须添加对程序集“System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的引用
    使用Log4net调试NHibernate
    Visual Studio 2012 如何打开MVC1.0 MVC2.0的项目
    MVC中IQueryable与IList的区别?
    vs2010 打开 vs2012 的解决方案
    LINQ分页和排序,skip和Take 用法
    未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService
    在Sql server数据库中,关于触发器的创建、调用及删除
  • 原文地址:https://www.cnblogs.com/HJQ2016/p/5952615.html
Copyright © 2011-2022 走看看