产生渐变色的view
效果
源码
https://github.com/YouXianMing/UI-Component-Collection
//
// 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
细节
时间: 2024-09-10 12:05:05