将CAGradientLayer用作maskView的遮罩图层

简介:


 

说明

CAGradientLayer可以用作alpha遮罩,供maskView使用,这两种东西都是非常偏门的东西,但是,偏门不代表功能不强大,我们可以用CAGradientLayer制作出非常牛逼的动画效果,此博文,本人抛砖引玉简易介绍怎么将CAGradientLayer与maskView联系上。后续博文会陆续扩展并写出更好的控件。

只有完美的UI交互效果才能够为功能强大的app锦上添花,本人致力于研究分析苹果开发中那些巨牛逼但却无人问津的冷门特效。

 

效果图

 

源码


//
//  CAGradientView.h
//  AlphaView
//
//  Created by YouXianMing on 15/5/4.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CAGradientView : UIView

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

- (void)alphaType;

@end


//
//  CAGradientView.m
//  AlphaView
//
//  Created by YouXianMing on 15/5/4.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "CAGradientView.h"

@interface CAGradientView () {
    CAGradientLayer   *_gradientLayer;
}

@end

@implementation CAGradientView

/**
 *  修改当前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


//
//  ViewController.m
//  AlphaView
//
//  Created by YouXianMing on 15/5/4.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "CAGradientView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    // 加了mask的view
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 200, 200)];
    imageView.image        = [UIImage imageNamed:@"base"];
    [self.view addSubview:imageView];
    
    CAGradientView *gradientView = [[CAGradientView alloc] initWithFrame:imageView.bounds];
    [gradientView alphaType];
    imageView.maskView = gradientView;
    
    
    // 对照组
    UIImageView *baseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40 + 200 + 40, 200, 200)];
    baseImageView.image        = [UIImage imageNamed:@"base"];
    [self.view addSubview:baseImageView];
}

@end

简易分析


目录
相关文章
|
JavaScript Linux
linux安装pm2全过程
你要在linux上安装pm2有很多方法,但我是用node的工具npm来完成安装的,所以在安装pm2之前需要先安装node
4688 1
|
文件存储 Android开发 iOS开发
DIY nas 之照片备份工具
Immich是一个开源解决方案,主要用于直接从iPhone、Android手机上备份照片与视频。用户可以将Immich部署到自己的电脑、NAS或服务器中,并通过App进行备份
777 2
|
NoSQL Java Linux
Java Spring Data Redis实战与配置参数详解 application.properties
Java Spring Data Redis实战与配置参数详解 application.properties。Java Spring Boot 2.0连接Redis服务器,可以在配置文件 application.properties中修改控制参数,包括安全、连接池、超时等参数。
11985 0
|
3月前
|
人工智能 API 定位技术
MCP全方位扫盲
MCP(Model Context Protocol)是由Anthropic提出的协议,旨在标准化大模型与外部数据源和工具的通信方式。其核心架构包括MCP Client(客户端)和MCP Server(服务端),通过标准化接口实现解耦,支持不同LLM无缝调用工具。相比传统方法,MCP简化了Prompt工程,减少定制代码,提升复用性。实际场景中,如天气查询或支付处理,MCP可智能调用对应工具,优化用户体验。MCP的核心价值在于标准化通信、统一工具描述及动态兼容性,成为大模型与外部服务的智能桥梁。
|
5月前
|
固态存储 IDE 开发工具
手把手教你安装Keil MDK5:官方网盘资源+芯片支持包配置详解(附调试实战)
Keil是一款专为嵌入式系统开发设计的集成开发环境(IDE),由德国Keil Software公司开发,后被ARM收购整合为MDK-ARM工具链的一部分。本文详细介绍Keil MDK541的安装步骤、系统要求、运行环境配置及首次使用指南,包括许可证管理、芯片支持包安装和工程模板设置等。同时提供新建STM32工程、编写测试代码的具体操作,并解答常见问题,如缺少DLL文件、语言设置及编译错误处理。附延伸学习资源与版权声明,帮助用户高效上手Keil开发环境。
2710 8
|
8月前
|
人工智能 自然语言处理 搜索推荐
【潜意识Java】了解并详细分析Java与AIGC的结合应用和使用方式
本文介绍了如何将Java与AIGC(人工智能生成内容)技术结合,实现智能文本生成。
594 5
|
安全 Docker 容器
dockercompose如何配置特权启动
dockercompose如何配置特权启动
|
JSON 前端开发 算法
vite打包优化vite-plugin-compression的使用
【8月更文挑战第4天】vite打包优化vite-plugin-compression的使用
1362 4
|
存储 前端开发 算法
常见的前端加密方式有哪些?运用场景有哪些?
【4月更文挑战第12天】前端加密技术包括对称加密(如AES、DES)、非对称加密(如RSA)和Hash算法(如MD5、SHA-1)。对称加密用于本地数据加密、HTTPS通信,非对称加密常用于用户登录认证,Hash算法适用于数据完整性校验和密码存储。应用场景包括用户登录认证、敏感数据传输、文件加密和支付安全。加密技术结合访问控制、安全审计等措施,能提升数据和用户信息安全。
1431 9
|
网络协议 Linux 网络架构
如何在Linux c/c++ 进行多播(组播)编程
如何在Linux c/c++ 进行多播(组播)编程
530 0