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
  • 相关阅读:
    ElasticSearch(ES)学习笔记
    Lucene學習日志
    velocity代码生成器的使用
    springboot学习笔记
    springmvc json 类型转换错误
    在做del业务时,传递参数,和接口中入参注释
    做add添加业务时,字符集乱码,form标签库,button的href 问题,添加后页面跳转,forward,redirect 。定制错误输出
    mybatis中联合查询的返回结果集
    mybatis分页,绝对路径的2种写法
    maven导入项目时报错,配置应用程序监听器[org.springframework.web.context.ContextLoaderListener]错误
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4297301.html
Copyright © 2011-2022 走看看