zoukankan      html  css  js  c++  java
  • 通过cagradientLayer类封装uiimageview动画色度差

    #import <UIKit/UIKit.h>
    typedef NS_ENUM(NSInteger, EcolorDirectionType)
    {
        EcolorDirectionUp,    //
        EcolorDirectionDown,  //
        EcolorDirectionRight, //
        EcolorDirectionLeft,  //
    };
    @interface ColorImageView : UIImageView
    
    /**
     *  @brief 确定方向
     */
    @property(nonatomic,assign) EcolorDirectionType direction;
    
    /**
     *  @brief  颜色
     */
    @property(nonatomic,strong) UIColor *color;
    
    /**
     *  @brief  百分比
     */
    @property(nonatomic,assign) CGFloat percent;
    
    @end
    #import "ColorImageView.h"
    @interface ColorImageView ()
    {
        CAGradientLayer *_gradientLayer;
    }
    @end
    @implementation ColorImageView
    @synthesize color     = _color;
    @synthesize percent   = _percent;
    @synthesize direction = _direction;
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self)
        {
            _gradientLayer = [CAGradientLayer layer];
            _gradientLayer.frame = self.bounds;
            _gradientLayer.borderWidth = 1.0f;
            _gradientLayer.colors = @[ (__bridge id)[UIColor clearColor].CGColor,
                                       (__bridge id)[UIColor redColor].CGColor
                                      ];
            _gradientLayer.locations = @[@(0.1),@(1)];
            
            [self.layer addSublayer:_gradientLayer];
        }
        return self;
    }
    /**
     *  @brief  设置颜色
     *  @param color 重写setter方法
     */
    - (void)setColor:(UIColor *)color
    {
        _color = color;
        _gradientLayer.colors = @[ (__bridge id)[UIColor clearColor].CGColor,
                                   (__bridge id)color.CGColor
                                   ];
    }
    - (UIColor *)color
    {
        return _color;
    }
    /**
     *  @brief  设置颜色分割点
     *  @param percent 重写setter方法
     */
    - (void)setPercent:(CGFloat)percent
    {
        _percent = percent;
        _gradientLayer.locations= @[@(percent),@(1)];
    }
    
    -(CGFloat)percent
    {
        return _percent;
    }
    
    /**
     *  @brief  设置颜色渐变方向
     *  @param direction 重写setter方法
     */
    -(void)setDirection:(EcolorDirectionType)direction
    {
        _direction=direction;
        switch (direction) {
            case EcolorDirectionUp:
            {
                _gradientLayer.startPoint = CGPointMake(0, 0);
                _gradientLayer.endPoint   = CGPointMake(0, 1);
            }
                break;
            case EcolorDirectionDown:
            {
                _gradientLayer.startPoint = CGPointMake(0, 1);
                _gradientLayer.endPoint   = CGPointMake(0, 0);
    
            }
                break;
            case EcolorDirectionLeft:
            {
                _gradientLayer.startPoint = CGPointMake(0, 0);
                _gradientLayer.endPoint   = CGPointMake(1, 0);
    
            }
                break;
            case EcolorDirectionRight:
            {
                _gradientLayer.startPoint = CGPointMake(1, 0);
                _gradientLayer.endPoint   = CGPointMake(0, 0);
            }
                break;
            default:
            {
                _gradientLayer.startPoint = CGPointMake(0, 0);
                _gradientLayer.endPoint   = CGPointMake(0, 1);
            }
                break;
        }
    }
    -(EcolorDirectionType)direction
    {
        return _direction;
    }
    @end
    #import "ViewController.h"
    #import "ColorImageView.h"
    @interface ViewController ()
    {
    
        ColorImageView *_imvColor;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        _imvColor=[[ColorImageView alloc]initWithFrame:CGRectMake(0, 0, 220, 185)];
        _imvColor.image = [UIImage imageNamed:@"1"];
        _imvColor.center = self.view.center;
        
        [self.view addSubview:_imvColor];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(event) userInfo:nil repeats:YES];
       // [self performSelector:@selector(event) withObject:nil afterDelay:2.0f];
    
    }
    - (void)event
    {
        _imvColor.direction=EcolorDirectionUp;
        _imvColor.color=[UIColor cyanColor];
        _imvColor.percent=arc4random()%100/100.f;
    }
    @end

  • 相关阅读:
    TortoiseSVN 使用详细步骤(三):安装
    TortoiseSVN使用详细步骤(二)
    TortoiseSVN使用详细步骤(一)
    IIS7下访问ashx页面,显示404
    Learning Python 008 正则表达式-003 search()方法
    Learning Python 008 正则表达式-002 findall()方法
    Learning Python 008 正则表达式-001
    Learning Python 007 基本语句
    Learning Python 006 list(列表) 和 tuple(元组)
    Learning Python 005 字符串和编码
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4384763.html
Copyright © 2011-2022 走看看