修改UIView的默认Layer后,修改View的值会动态修改Layer的值

简介:

修改UIView的默认Layer后,修改View的值会动态修改Layer的值

效果图:

如上图所示,当我们修改了一个UIView的子类中的Layer内置类型时(如上图中我们将CALayer直接替换成了CAGradientLayer类),会直接作用到其内置的Layer当中.

我们可以用这个特性将Layer封装到View当中,然后直接修改view就能达到我们想要实现的目的.

源码:

//
//  AlphaView.h
//  YXMWeather
//
//  Created by XianMingYou on 15/2/20.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface AlphaView : UIView

@property (nonatomic, strong) NSArray *colors;
@property (nonatomic, strong) NSArray *locations;
@property (nonatomic)         CGPoint  startPoint;
@property (nonatomic)         CGPoint  endPoint;

- (void)alphaType;

@end


//
//  AlphaView.m
//  YXMWeather
//
//  Created by XianMingYou on 15/2/20.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import "AlphaView.h"

@interface AlphaView ()

{
    CAGradientLayer   *_gradientLayer;
}

@end

@implementation AlphaView

/**
 *  修改当前view的backupLayer为CAGradientLayer
 *
 *  @return CAGradientLayer类名字
 */
+ (Class)layerClass {
    return [CAGradientLayer class];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _gradientLayer = (CAGradientLayer *)self.layer;
    }
    return self;
}

- (void)alphaType {
    self.colors     = @[[UIColor clearColor], [UIColor blackColor], [UIColor clearColor]];
    self.locations  = @[@(0.25), @(0.5), @(0.75)];
    self.startPoint = CGPointMake(0, 0);
    self.endPoint   = CGPointMake(1, 0);
}

/**
 *  重写setter,getter方法
 */
@synthesize colors = _colors;
- (void)setColors:(NSArray *)colors {
    _colors = colors;
    
    // 将color转换成CGColor
    NSMutableArray *cgColors = [NSMutableArray array];
    for (UIColor *tmp in colors) {
        id cgColor = (__bridge id)tmp.CGColor;
        [cgColors addObject:cgColor];
    }
    
    // 设置Colors
    _gradientLayer.colors = cgColors;
}
- (NSArray *)colors {
    return _colors;
}

@synthesize locations = _locations;
- (void)setLocations:(NSArray *)locations {
    _locations = locations;
    _gradientLayer.locations = _locations;
}
- (NSArray *)locations {
    return _locations;
}

@synthesize startPoint = _startPoint;
- (void)setStartPoint:(CGPoint)startPoint {
    _startPoint = startPoint;
    _gradientLayer.startPoint = startPoint;
}
- (CGPoint)startPoint {
    return _startPoint;
}

@synthesize endPoint = _endPoint;
- (void)setEndPoint:(CGPoint)endPoint {
    _endPoint = endPoint;
    _gradientLayer.endPoint = endPoint;
}
- (CGPoint)endPoint {
    return _endPoint;
}

@end

目录
相关文章
element-ui table排序sortable三种状态,怎么去掉默认状态
在 element-ui 中,也定义了 sort-orders 有三种状态: ascending、descending、null,这三种状态形成一个循环切换。
2180 0
|
3月前
|
XML Java 数据格式
面试题:怎样把所有的组件的lazy-init值都设置为默认true?
面试题:怎样把所有的组件的lazy-init值都设置为默认true?
19 0
|
8月前
UE4 使用Animation Data Modifiers修改动画片段
UE4 使用Animation Data Modifiers修改动画片段
59 0
UE4 使用Animation Data Modifiers修改动画片段
|
存储 Swift
Swift - Cell自适应+代码约束(SnapKit)横竖屏支持平铺+根据URL获取图片size
Swift - Cell自适应+代码约束(SnapKit)横竖屏支持平铺+根据URL获取图片size
179 0
SwiftUI—Text视图的填充属性
SwiftUI—Text视图的填充属性
285 0
SwiftUI—Text视图的填充属性
|
Swift
Swift :SnapKit 更新约束 & label宽度自适应
Swift :SnapKit 更新约束 & label宽度自适应
1160 0
|
异构计算
UIView 的 alpha,hidden,opaque 属性之间的关系影响 图层混用
前言 在看性能优化文章里, 看到不少提到要把相应控件设置成不透明,特别是在滚动页面上,但是没有看到深入说明为什么要这样去做, 如果控件是透明的将会带来什么影响,还有当中涉及到的知识点都没有提出来.
1236 0
YII2 save 和 insert 循环保存数据,出现间隔性丢失值 (这些值是共有相同的值) 解决: // 每次保存需要用 clone 不能使用直接用一个 实例化的model.
YII2 save 和 insert  循环保存数据,出现间隔性丢失值 (这些值是共有相同的值) 解决:  // 每次保存需要用 clone 不能使用直接用一个 实例化的model. $_model = clone $timeTask;$_model->setAttributes($save);if ( ! $_model->save()) {参考: http://www.
1709 0