通过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

时间: 2024-11-08 20:39:48

通过cagradientLayer类封装uiimageview动画色度差的相关文章

android ScaleAnimation类:尺寸变化动画类

Android JDK为我们提供了4种动画效果,分别是: AlphaAnimation,RotateAnimation, ScaleAnimation, TranslateAnimation.今天我想讲解的是TranslateAnimation这个动画效果.也是本人在做一个移动图片的动画效果的项目时,遇到了一些问题.在网上查了很多资料,搞了好几天.终于明白怎么使用这个TranslateAnimation,在本文中记录下来,以便以后忘记了可以查阅. http://gundumw100.iteye.

UIView封装的动画

////  MJViewController.m//  10-UIView封装的动画////  Created by apple on 14-4-21.//  Copyright (c) 2014年 itcast. All rights reserved.// #import "MJViewController.h" @interface MJViewController ()@property (weak, nonatomic) IBOutlet UIView *myview;@pr

QT 操作excel 类封装

1 # pro file 2 [plain] view plaincopy 3 CONFIG += qaxcontainer 4 5 QT += core 6 7 QT -= gui 8 9 TARGET = QExcel 10 CONFIG += console 11 CONFIG -= app_bundle 12 13 TEMPLATE = app 14 15 16 SOURCES += main.cpp \ 17 qexcel.cpp 18 19 HEADERS += \ 20 qexce

php---数据库类封装

为了节省以后的时间,今天封装了操作sql语句的一个类,在此保存起来,方面以后使用. 这个类的文件名:SqlTool.class.php 主要有dql和dml两个函数 看下面的源码" <?php class SqlTool{ private $conn; private $username="root"; private $password="1234"; private $host="127.0.0.1"; private $db

背水一战 Windows 10 (42) - 控件(导航类): Frame 动画

原文:背水一战 Windows 10 (42) - 控件(导航类): Frame 动画 [源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) Frame 动画 示例Animation/NavigationTransitionInfo/Demo.xaml <Page x:Class="Windows10.Animation.NavigationTransitionInfo.Demo" xmlns="http://schemas.mic

ScaleAnimation类:尺寸变化动画类

9.4  ScaleAnimation类:尺寸变化动画类 ScaleAnimation类是Android系统中的尺寸变化动画类,用于控制View对象的尺寸变化,该类继承于Animation类.ScaleAnimation类中的非常多方法都与Animation类一致,该类中最经常使用的方法便是ScaleAnimation构造方法. [基本的语法]public ScaleAnimation (float fromX, float toX, float fromY, float toY, int pi

操作类封装

/*操作类封装 */ /*调用方法 如下: * var str= new IStrManipulation();//实例化字符串处理接口 * console.log(str.StrManipulation('StrManipulation',"111sss23123").getLength()); * var convert =new IConvert();//实例化类型转换接口 * console.log(convert.Convert('Convert',"1112312

MFC如和将类封装到DLL以及调用

MFC如和将类封装到DLL以及调用 分类: C++技术2012-06-27 17:40 1028人阅读 评论(0) 收藏 举报 dllfunmfcnullexe *1.先用mfc向导生成静态dll文件.*2.编辑增加类:*3.生成dll文件和lib文件:*4.将生成的dll和lib,和类的头文件复制到需要引用的文*件exe下:*///导出dll的头文件myClass.h#define DLLimport __declspec(dllimport)#define DLLexprot __decls

RUBY的类封装,继承,多态简单演示

class Person def initialize(name,age=18) @name=name @age=age @motherland="China" end def talk puts "my name is " [email protected]+",age is "+@age.to_s if @motherland == "China" puts "I am a China." else p