博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CAShapeLayer的使用[1]
阅读量:7197 次
发布时间:2019-06-29

本文共 2953 字,大约阅读时间需要 9 分钟。

CAShapeLayer的使用[1]

 

使用CoreAnimation绘制动画带来的系统开销非常的小,CoreAnimation通常都是使用GPU的.

CAShapeLayer属于CoreAnimation中很重要的一种layer,无论是作为mask还是作为进度条显示都非常的好用,我用CAShapeLayer实现了如下复杂的效果.

////  RootViewController.m////  Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"#import "UIImage+ImageEffects.h"@interface RootViewController ()@property (nonatomic, strong) UIView        *showView;@property (nonatomic, strong) CAShapeLayer  *oneReferenceLayer;@property (nonatomic, strong) CAShapeLayer  *maskLayer;@end#define   DEGREES(degrees)  ((M_PI * (degrees))/ 180.f)@implementation RootViewController- (void)handlePan:(UIPanGestureRecognizer *)recognizer{    // 拖拽    CGPoint translation = [recognizer translationInView:self.view];    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,                                         recognizer.view.center.y + translation.y);    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];    // 关闭CoreAnimation实时动画绘制(核心)    [CATransaction setDisableActions:YES];    _maskLayer.position = recognizer.view.center;}- (void)viewDidLoad{    [super viewDidLoad];        /* ====== 背景View ====== */    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];    imageView.image = [UIImage imageNamed:@"back"];    [self.view addSubview:imageView];        /* ====== 作为mask的View ====== */    _maskLayer = [CAShapeLayer layer];        // 贝塞尔曲线(创建一个圆)    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0)                                                        radius:100                                                    startAngle:DEGREES(0)                                                      endAngle:DEGREES(360)                                                     clockwise:YES];    // 获取path    _maskLayer.path     = path.CGPath;    _maskLayer.position = CGPointMake(_showView.bounds.size.width/2.f,                                      _showView.bounds.size.height/2.f);    // 设置填充颜色为透明    _maskLayer.fillColor = [UIColor blackColor].CGColor;    _maskLayer.position = self.view.center;        UIView *blurView = [[UIView alloc] initWithFrame:self.view.bounds];    blurView.backgroundColor = [UIColor blackColor];    [self.view addSubview:blurView];    blurView.layer.mask = _maskLayer;    blurView.layer.contents = (__bridge id)([[UIImage imageNamed:@"back"] blurImage].CGImage);        /* ====== 透明的View,用于maskView中的ShapeLayer的参考View(用于拖拽) ====== */    _showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];    _showView.backgroundColor = [UIColor clearColor];    _showView.center = self.view.center;    [self.view addSubview:_showView];        UIPanGestureRecognizer *recognizer = \    [[UIPanGestureRecognizer alloc] initWithTarget:self                                            action:@selector(handlePan:)];    [_showView addGestureRecognizer:recognizer];}@end

这个地方很关键哦,没有关闭的话会非常的卡顿的.

 

shapeLayer作为mask的一些技巧.

 

转载地址:http://zqkum.baihongyu.com/

你可能感兴趣的文章
【转】王钿《浅谈逻辑设计的学习》
查看>>
js闭包
查看>>
几何画板如何通过迭代法绘图
查看>>
BarTender表单的人性化设计—分组框
查看>>
在 CSS 中,width 和 height 指的是内容区域的宽度和高度
查看>>
河马SQLServer注入工具v1.1
查看>>
Linux基础知识随笔记
查看>>
Oracle:ORA-01791: 不是 SELECTed 表达式
查看>>
SpringMVC快速入门
查看>>
2019-05-20 Java学习日记之String类型
查看>>
.net core Jenkins持续集成Linux、Docker、K8S
查看>>
python入门
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
Cisco ASA5512 双线
查看>>
杨元庆:税收影响联想电脑国内售价
查看>>
Linux虚拟机下lvm扩大根目录磁盘空间
查看>>
tomcat应用实践(虚拟主机以及站点优化)
查看>>
使用VB.NET重构简单知识简述
查看>>
访问网络共享
查看>>