修改UIView的backedlayer为CAShapeLayer
修改UIView的backedlayer为CAShapeLayer

什么叫backedlayer呢?backedlayer指的是一个View在创建好的时候就已经帮你创建好了一个与View大小一致的CALayer了,而且,这个CALayer的所有属性值的改变都不会引起动画反应,除非写在+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations这个方法里面.认识到这一点是很关键的.
效果:

源码:
ContentView.h + ContentView.m + RootViewController.m
//
// ContentView.h
// Progress
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface ContentView : UIView @property (nonatomic, assign) CGFloat progress; @end
//
// ContentView.m
// Progress
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "ContentView.h" @interface ContentView () {
CAShapeLayer *_shapeLayer;
} @end @implementation ContentView - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
_shapeLayer = (CAShapeLayer *)self.layer;
_shapeLayer.strokeEnd = .f;
_shapeLayer.fillColor = [UIColor clearColor].CGColor;
_shapeLayer.strokeColor = [UIColor redColor].CGColor;
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
_shapeLayer.path = path.CGPath;
}
return self;
} + (Class)layerClass
{
return [CAShapeLayer class];
} @synthesize progress = _progress; - (CGFloat)progress
{
return _progress;
} - (void)setProgress:(CGFloat)progress
{
if (_progress != progress)
{
CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
ani.fromValue = [NSNumber numberWithFloat:_progress];
ani.toValue = [NSNumber numberWithFloat:progress];
ani.duration = 0.2f;
_shapeLayer.strokeEnd = progress;
[_shapeLayer addAnimation:ani forKey:nil];
_progress = progress;
}
} @end
//
// RootViewController.m
// Progress
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import "ContentView.h"
#import "YXGCD.h" @interface RootViewController () @property (nonatomic, strong) GCDTimer *timer; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; ContentView *layerView = [[ContentView alloc] initWithFrame:CGRectMake(, , , )];
layerView.center = self.view.center;
[self.view addSubview:layerView]; _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
[_timer event:^{
layerView.progress = arc4random()% / .f;
} timeInterval:NSEC_PER_SEC];
[_timer start]; } @end
一些需要注意的细节:


修改UIView的backedlayer为CAShapeLayer的更多相关文章
- 修改UIView的默认Layer后,修改View的值会动态修改Layer的值
修改UIView的默认Layer后,修改View的值会动态修改Layer的值 效果图: 如上图所示,当我们修改了一个UIView的子类中的Layer内置类型时(如上图中我们将CALayer直接替换成了 ...
- 绘制虚线的UIView
绘制虚线的UIView CAShapeLayer配合贝塞尔曲线可以绘制曲线,笔者继承了一个UIView的子类,并将该子类的backedLayer替换为CAShapeLayer,以此来实现绘制虚线的效果 ...
- iOS UIView 快速修改 frame,
在iOS开发布局修改 frame 时需要繁琐的代码实现,今天偶尔看到一播客说到快速修改的 frame 的方法,自己动手写了一遍实现代码. 快速实现主要通过 添加类目的方式,对UIView 控件添加了一 ...
- 详解CALayer 和 UIView的区别和联系
详解CALayer 和 UIView的区别和联系 前言 前面发了一篇iOS 面试的文章,在说到 UIView 和 CALayer 的区别和联系的时候,被喵神指出没有切中要点,所以这里就 CALay ...
- UIView和CALayer区别
(1)首先UIView可以响应用户的触摸事件,Layer不可以. (2)View中frame getter方法,bounds和center,UIView并没有做什么工作:它只是简单的各自调用它底层的C ...
- iOS:UIView的CALayer基本演练
UIView的CALayer基本演练的属性和注意事项: 在UIView中创建一个按钮UIButton,然后设置UIButton的Layer属性 –圆角.边框.阴影及3D形变属性 注意: 1.在UIVi ...
- ios开发核心动画七:核心动画与UIView动画的区别
/** UIView与核心动画区别?(掌握) 1.核心动画只作用在layer. 2.核心动画看到的都是假像,它并没有去修改UIView的真实位置. 什么时候使用核心动画? 1.当不需要与用户进行交互, ...
- UIView与CALayer 区别
在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个 ...
- 详解 CALayer 和 UIView 的区别和联系
http://www.cocoachina.com/ios/20150828/13244.html 作者:@武蕴牛x 授权本站转载. 前言 前面发了一篇iOS 面试的文章,在说到 UIView 和 C ...
随机推荐
- res/raw与assets目录的区别
1.相同点: 两者都会原封不动的保存在apk包中,不会被编译成二进制码. 2.不同点: raw目录下只能存放文件,不能存放下一级的文件夹,而assets可以存放下一级的文件夹. raw目录下的资源会映 ...
- FS及CacheFS类解读
Javac中有FSInfo与CacheFSInfo两个类,CacheFSInfo继承了FSInfo类,这两个类的主要功能就是通过map缓存Jar文件,核心代码如下: private Map<Fi ...
- 使用github oauth 出现 OpenSSL::SSL::SSLError - SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 解决
A top level initializer is highly recommended to use: conf/initializer/tls_settings.rb OpenSSL::SSL: ...
- svn 被锁,清理恢复过程
svn 被锁,清理恢复过程 http://stackoverflow.com/questions/18000363/tortoisesvn-wont-allow-me-to-add-any-files ...
- Java线程池及其底层源码实现分析
1.相关类 Executors ExecutorService Callable ThreadPool Future 2.相关接口 Executor Executor接口的使用: p ...
- 最新 IntelliJ Idea 2017 激活方法(转)
转载地址:http://www.cnblogs.com/suiyueqiannian/p/6754091.html 1. 到网站 http://idea.lanyus.com/ 获取注册码. 2.填入 ...
- Django 中文文档地址
http://djangobook.py3k.cn/2.0/ MK一下
- Markdown 语法整理大集合2017
简明教程:https://ouweiya.gitbooks.io/markdown/ 1.标题 代码 注:# 后面保持空格 # h1 ## h2 ### h3 #### h4 ##### h5 ### ...
- [转]WxEmojiView
本文转自:https://github.com/icindy/WxEmojiView 来源信息 author: Di (微信小程序开发工程师) organization: WeAppDev(微信小程序 ...
- php中数组和字符串的相互转换
数组转字符串: implode('!', $arr);//将一维数组以!分隔组合成一个字符串,参数一可以为"" 字符串转数组: explode('!', $str);//将字符串以 ...