码迷,mamicode.com
首页 > 其他好文 > 详细

产生渐变色的view

时间:2015-12-15 22:46:32      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

产生渐变色的view

技术分享

 

效果

技术分享

 

源码

https://github.com/YouXianMing/GradientColorView

//
//  GradientColorView.h
//  GradientColorView
//
//  Created by YouXianMing on 15/12/15.
//  Copyright © 2015年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface GradientColorView : UIView

/**
 *  CGColor‘s array.
 */
@property (nonatomic, strong) NSArray   *colors;

/**
 *  CGColor‘s location.
 */
@property (nonatomic, strong) NSArray   *locations;

/**
 *  Start point.
 */
@property (nonatomic) CGPoint startPoint;

/**
 *  End point.
 */
@property (nonatomic) CGPoint endPoint;

/**
 *  After you have set all the properties, you should run this method to make effective.
 */
- (void)becomeEffective;

@end
//
//  GradientColorView.m
//  GradientColorView
//
//  Created by YouXianMing on 15/12/15.
//  Copyright © 2015年 YouXianMing. All rights reserved.
//

#import "GradientColorView.h"

@interface GradientColorView ()

@property (nonatomic, strong) CAGradientLayer  *gradientLayer;

@end

@implementation GradientColorView

+ (Class)layerClass {
    
    return [CAGradientLayer class];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        _gradientLayer  = (CAGradientLayer *)self.layer;
        self.startPoint = CGPointMake(0, 0);
        self.endPoint   = CGPointMake(1, 0);
        self.locations  = @[@(0.25), @(0.5), @(0.75)];
        self.colors     = @[(__bridge id)[UIColor redColor].CGColor,
                            (__bridge id)[UIColor greenColor].CGColor,
                            (__bridge id)[UIColor blueColor].CGColor];
    }
    
    return self;
}

- (void)becomeEffective {
    
    self.gradientLayer.startPoint = self.startPoint;
    self.gradientLayer.endPoint   = self.endPoint;
    self.gradientLayer.colors     = self.colors;
    self.gradientLayer.locations  = self.locations;
}

@end

 

细节

技术分享

 

产生渐变色的view

标签:

原文地址:http://www.cnblogs.com/YouXianMing/p/5049702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!