zoukankan      html  css  js  c++  java
  • 修改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
  • 相关阅读:
    129. Sum Root to Leaf Numbers
    113. Path Sum II
    114. Flatten Binary Tree to Linked List
    112. Path Sum
    100. Same Tree
    300. Longest Increasing Subsequence
    72. Edit Distance
    自定义js标签库
    JS 实现Table相同行的单元格自动合并示例代码
    mysql 高版本only_full_group_by 错误
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4297301.html
Copyright © 2011-2022 走看看