• 修改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
  • 相关阅读:
    一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——收流篇:(一)简介
    常用perl脚本工具
    一步步学习操作系统(2)——在STM32上实现一个可动态加载kernel的"my-boot"
    一步步学习操作系统(1)——参照ucos,在STM32上实现一个简单的多任务(“啰里啰嗦版”)
    一步步学习操作系统(1)——参照ucos,在STM32上实现一个简单的多任务(“精简版”)
    perl的正则表达式
    vim小技巧
    一些方便的bash命令
    Linux磁盘及文件系统管理
    Linux权限扩展
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4297301.html
Copyright © 2020-2023  润新知