objc.io 学习 摘自原处修改

对层的属性详细了解可见这里

@interface ClockFace : CAShapeLayer
@property (nonatomic, strong) NSDate *time;
@end

@interface ClockFace ()
@property (nonatomic, strong) CAShapeLayer *hourHand;
@property (nonatomic, strong) CAShapeLayer *minuteHand;
@end

@implementation ClockFace
- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor grayColor].CGColor;
        self.bounds = CGRectMake(0, 0, 200, 200);
        self.position = CGPointMake(520, 150);
        self.path = [UIBezierPath bezierPathWithOvalInRect:self.bounds].CGPath;
//        self.anchorPoint = CGPointMake(0, 0);
        self.fillColor = [UIColor whiteColor].CGColor;
        self.strokeColor = [UIColor blackColor].CGColor;
        self.lineWidth = 4;
        
        self.hourHand = [CAShapeLayer layer];
        self.hourHand.bounds = CGRectMake(0, 0, 4, 70);//若未设置bounds,anchorPoint实际指向始终为{0,0}代表左上角;默认为{0.5,0.5}代表中心点随着position属性偏移
        self.hourHand.path = [UIBezierPath bezierPathWithRect:self.hourHand.bounds].CGPath;
        self.hourHand.lineWidth = .5f;
        self.hourHand.strokeColor = [UIColor blackColor].CGColor;
        self.hourHand.fillColor = [UIColor yellowColor].CGColor;
        self.hourHand.anchorPoint = CGPointMake(1, 1);
        self.hourHand.position = CGPointMake(102, 100);
        [self addSublayer:self.hourHand];
        
        self.minuteHand = [CAShapeLayer layer];
        self.minuteHand.bounds = CGRectMake(0, 0, 2, 90);
        self.minuteHand.path = [UIBezierPath bezierPathWithRect:self.minuteHand.bounds].CGPath;
        self.minuteHand.lineWidth = .5f;
        self.minuteHand.strokeColor = [UIColor blackColor].CGColor;
        self.minuteHand.fillColor = [UIColor yellowColor].CGColor;
        self.minuteHand.anchorPoint = CGPointMake(1, 1);
        self.minuteHand.position = CGPointMake(101, 100);
        [self addSublayer:self.minuteHand];
    }
    return self;
}

- (void)setTime:(NSDate *)time {
    _time = time;
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:time];
    self.hourHand.affineTransform = CGAffineTransformMakeRotation(components.hour / 12.0 * 2.0 * M_PI);
    self.minuteHand.affineTransform = CGAffineTransformMakeRotation(components.minute / 60.0 * 2.0 * M_PI);
}

CALayer anchorPoint 锚点始终为(0,0)的更多相关文章

  1. 网站开启https后加密协议始终是TLS1.0如何配置成TLS1.2?

    p { margin-bottom: 0.1in; line-height: 120% } 网站开启https后加密协议始终是TLS1.0如何配置成TLS1.2? 要在服务器上开启 TLSv1.,通常 ...

  2. 数据可视化-EChart2.0.0使用中遇到的2个问题

    之前项目中都是使用FusionChart和HighChart,基本都是没有购买商业许可.然后现在开发的系统需要交付给客户使用.所以现在图表控件不能直接使用FusionChart和HighChart,通 ...

  3. 解决ListView 跟ScroolView 共存 listItem.measure(0, 0) 空指针

    在网上找到ListView 和ScroolView 共存的方法无非是给他每个listview 重新增加高度,但是android 的设计者始终认为这并不是一种好的实现方法.但是有的时候有必须要用这种蛋疼 ...

  4. 解决ListView 和ScroolView 共存 listItem.measure(0, 0) 空指针

    在网上找到ListView 和ScroolView 共存的方法无非是给他每个listview 重新增加高度,但是android 的设计者始终认为这并不是一种好的实现方法.但是有的时候有必须要用这种蛋疼 ...

  5. Java 获取客户端ip返回127.0.0.1问题

    Java开发中使用 request.getRemoteAddr 获取客户端 ip ,返回结果始终为127.0.0.1.原因是服务器使用了nginx反向代理. 解决办法:在nginx配置文件nginx. ...

  6. Redis-5.0.0集群配置

    版本:redis-5.0.0 参考:http://redis.io/topics/cluster-tutorial. 集群部署交互式命令行工具:https://github.com/eyjian/re ...

  7. hadoop3.0.0测验

    下载地址: hadoop: http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-3.0.0/ 准备工作: 1.master节点与其他节点需要建 ...

  8. NPOI2.2.0.0实例详解(十一)—向EXCEL插入图片

    --------------------- 本文来自 天水宇 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/xxs77ch/article/details/50553 ...

  9. WPF 自定义 MessageBox (相对完善版 v1.0.0.6)

    基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当你不得不弹出一个消息框通知用户消息时(虽然很不建议在程序中频繁 ...

随机推荐

  1. 64位ubuntu下安装微博客户端的方法

    最近安装了12.04的ubuntu系统,在unbutu提供的软件中心找不到微博客户端的应用,但在新浪的http://sinatair.sinaapp.com/下找到了官方的客户端. 于是下载了linu ...

  2. 哈希表(Hash Table)

    参考: Hash table - Wiki Hash table_百度百科 从头到尾彻底解析Hash表算法 谈谈 Hash Table 我们身边的哈希,最常见的就是perl和python里面的字典了, ...

  3. MySQL wamp密码修改

    WAMP安装好后,mysql密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按回车 ...

  4. Python之路 day3 全局变量、局部变量

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa """ 全局与局部变量 在子程序中定义的变量称为局部变 ...

  5. js基本类型 引用类型

    参考 https://segmentfault.com/a/1190000005794070 http://blog.csdn.net/yummy_go/article/details/5050468 ...

  6. Timing Attack 周边感应sql

    直接上硬菜.盲注的一段语句: 1170 UNION SELECT IF(SUBSTRING(current,1,1)) = CHAR(119) , BENCHMARK(5000000,ENCODE(' ...

  7. Html5导航插件,支持水平/垂直展示

    /*========================= CSS STYLE=========================== */ .tabWrap {} .tabWrap ul { paddin ...

  8. mac OS.NE开发环境搭建

    合肥程序员群:49313181.    合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入,申请备注填写姓名+技术+工作年限) Q  Q:408365330     E-Mail:eg ...

  9. [HTML/CSS] ul元素居中处理

    CSS: <style type="text/css"> #nav_sub_page { text-align: center; width: 80%; } #nav_ ...

  10. winform项目打包成可安装程序(vs2015)

    1.新建安装和部署项目      如果是初始使用并且原来没有下载过,会被所引导一个下载界面http://learn.flexerasoftware.com/content/IS-EVAL-Instal ...