修改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的更多相关文章

  1. 修改UIView的默认Layer后,修改View的值会动态修改Layer的值

    修改UIView的默认Layer后,修改View的值会动态修改Layer的值 效果图: 如上图所示,当我们修改了一个UIView的子类中的Layer内置类型时(如上图中我们将CALayer直接替换成了 ...

  2. 绘制虚线的UIView

    绘制虚线的UIView CAShapeLayer配合贝塞尔曲线可以绘制曲线,笔者继承了一个UIView的子类,并将该子类的backedLayer替换为CAShapeLayer,以此来实现绘制虚线的效果 ...

  3. iOS UIView 快速修改 frame,

    在iOS开发布局修改 frame 时需要繁琐的代码实现,今天偶尔看到一播客说到快速修改的 frame 的方法,自己动手写了一遍实现代码. 快速实现主要通过 添加类目的方式,对UIView 控件添加了一 ...

  4. 详解CALayer 和 UIView的区别和联系

    详解CALayer 和 UIView的区别和联系   前言 前面发了一篇iOS 面试的文章,在说到 UIView 和 CALayer 的区别和联系的时候,被喵神指出没有切中要点,所以这里就 CALay ...

  5. UIView和CALayer区别

    (1)首先UIView可以响应用户的触摸事件,Layer不可以. (2)View中frame getter方法,bounds和center,UIView并没有做什么工作:它只是简单的各自调用它底层的C ...

  6. iOS:UIView的CALayer基本演练

    UIView的CALayer基本演练的属性和注意事项: 在UIView中创建一个按钮UIButton,然后设置UIButton的Layer属性 –圆角.边框.阴影及3D形变属性 注意: 1.在UIVi ...

  7. ios开发核心动画七:核心动画与UIView动画的区别

    /** UIView与核心动画区别?(掌握) 1.核心动画只作用在layer. 2.核心动画看到的都是假像,它并没有去修改UIView的真实位置. 什么时候使用核心动画? 1.当不需要与用户进行交互, ...

  8. UIView与CALayer 区别

    在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个 ...

  9. 详解 CALayer 和 UIView 的区别和联系

    http://www.cocoachina.com/ios/20150828/13244.html 作者:@武蕴牛x 授权本站转载. 前言 前面发了一篇iOS 面试的文章,在说到 UIView 和 C ...

随机推荐

  1. 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...

  2. python-Event事件线程同步和互斥

    #!/usr/bin/python #coding=utf-8 #用于线程间通信,通过事件标识控制 import threading from time import sleep,ctime def ...

  3. STL 排序(转载)

    这篇文章关于STL中的排序写的虽不深入,但是还是挺好的. 1.sort sort有两种形式,第一种形式有两个迭代器参数,构成一个前开后闭的区间,按照元素的 less 关系排序:第二种形式多加一个指定排 ...

  4. POJ 3481 Double Queue(set实现)

    Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...

  5. 饶军:Apache Kafka的过去,现在,和未来

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文首发在云+社区,未经许可,不得转载. 大家好,我大概简单的介绍一下,我叫饶军,我是硅谷的初创公司Confluent的联合创始人之一,我们公 ...

  6. 解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function

    Vue的项目中,如果项目简单, 父子组件之间的数据传递可以使用  props 或者 $emit 等方式 进行传递 但是如果是大中型项目中,很多时候都需要在不相关的平行组件之间传递数据,并且很多数据需要 ...

  7. 玩转树莓派《二》——用python实现动画与多媒体

    环境:树莓派,系统raspbian,系统自带两个版本的python以及pygame. 1.画板 程序如下: !/home/pi/game_1.py import pygame width = 640 ...

  8. python——高级特性(2)

    迭代 在python中迭代是通过for ....in...完成的,只要是可迭代对象都可以迭代 #!usr/bin/python #-*- coding:UTF-8 -*- #tuple迭代 t=[(1 ...

  9. UEditor图片焦点错位,火狐document.body.scrollTop不管用的问题

    转自 http://liyunpeng.iteye.com/blog/2068751 关于 document.body.scrollTop 在火狐浏览器中不管用的问题 看网上有人写通过判断docume ...

  10. 陆续放给大家一些拿钱买的收费的模板,今天先给一个在某销售网站上排行第一的管理端模板 Ace Responsive Admin Template

    下载地址: http://pan.baidu.com/s/1B2xjC 提取密码:le1p 原文地址:http://zhangrou.net/post/2014/06/23/陆续放给大家一些拿钱买的收 ...