修改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 ...
随机推荐
- JavaScript数据结构-6.优先队列
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Struts2 resulttype
本文章只介绍较为常用的三种result type 1.dispatcher 2.redirect 3.redirectAction 一.dispatcher 用于转向JSP页面,这个是默认的结果类型, ...
- [转]Repeat Page Header on each Page for reports SSRS
本文转自:https://stackoverflow.com/questions/3475144/i-want-to-repeat-page-header-on-each-page-for-repor ...
- js文件加载太慢,JavaScript文件加载加速
原文出自:https://blog.csdn.net/seesun2012 js脚本加载太慢,JavaScript脚本加载加速(亲测有效) 测试背景: JS文件大小:6.1kB 传统形式加载js文件: ...
- C# 文件上传 制作水印
其实C#的文件上传是非常简单的 前台代码 <asp:FileUpload ID="FileUpload1" accept=".jpg,.png,.jpeg" ...
- [javaSE] 标识符大小写
java中是严格区分大小写的. PHP中函数,类名称不区分大小写,变量和常量区分大小写 public class VariableDemo { public static void test(){ S ...
- 三、synchronized同步锁
一.简介 在Java多线程中,我们要实现同步串行最早接触的就是synchronized关键字. 基本语法如下: synchronized(锁) { // 代码块 } sychronized关键字的锁主 ...
- LeetCode刷题第一天
1 . 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用 ...
- JRE“瘦身”&桌面程序集成JRE
项目是一个桌面程序,程序文件不大,但运行jre有198 MB,因此需要"瘦身". jre包含bin.lib两部分,分别为93.6 MB.104 MB. 1.精简bin 运行桌面程序 ...
- 判断有几个checkbox被选中
//判断是否有选中的checkbox的值是否为空 var number = $("input[type='checkbox']:checked").length; if(numbe ...