OC - 28.模拟时钟

效果图


实现思路


  • 该示例通过隐式动画实现
  • 表盘通过显示在imageView中的一张图片来实现
  • 在表盘上绘制(时分秒)三条直线,分别位于不同的图层,且时针位于最下层,秒针位于最上层

    • 设置直线为圆角
    • 直线的一段位于表盘的圆心
    • 通过NSTimer定时器,使不同的图层定时执行对应的动画

实现步骤


  • 通过storyboard创建表盘,并拥有它

    @property (weak, nonatomic) IBOutlet UIImageView *clockView;
  • 使用三个成员变量来保存时分秒三根表针位于的不同图层

    @property (nonatomic, weak) CALayer *secondLayer;
    @property (nonatomic, weak) CALayer *minuteLayer; @property (nonatomic, weak) CALayer *hourLayer;
  • 初始化所用到的常量

    //将旋转角度转换为弧度制
    #define angleToRadion(angle) ((angle) / 180.0 * M_PI)
    
    //秒针每秒钟转过的角度
    #define perSecondAngle 6 //分针每分钟转过的角度 #define perMinuteAngle 6 //时针每小时转过的角度 #define perHourAngle 30 //时针每分钟转过的角度 #define perMuniteHourAngle 0.5
  • 设置时分秒三根表针

    • 设置时针

      - (void)setUpHourLayer
      {
      	//创建图层
      	CALayer *layer = [CALayer layer];
      	layer.backgroundColor = [UIColor blackColor].CGColor; layer.cornerRadius = 8; 设置图层的锚点 layer.anchorPoint = CGPointMake(0.5, 1); //设置图层的位置和尺寸 layer.position = CGPointMake(kClockWith * 0.5, kClockWith * 0.5); layer.bounds = CGRectMake(0, 0, 5, kClockWith * 0.5 - 44); //将图层添加到父图层中 [self.clockView.layer addSublayer:layer]; self.hourLayer = layer; }
    • 设置分针

      - (void)setUpMinuteLayer
      {
          CALayer *layer = [CALayer layer];
          layer.backgroundColor = [UIColor blackColor].CGColor; layer.cornerRadius = 4; //设置锚点 layer.anchorPoint = CGPointMake(0.5, 1); //设置位置和尺寸 layer.position = CGPointMake(kClockWith * 0.5, kClockWith * 0.5); layer.bounds = CGRectMake(0, 0, 3, kClockWith * 0.5 - 34); //将图层添加到父图层中 [self.clockView.layer addSublayer:layer]; self.minuteLayer = layer; }
    • 设置秒针

      - (void)setUpSecondLayer
      {
          CALayer *layer = [CALayer layer];
          layer.backgroundColor = [UIColor redColor].CGColor; //设置锚点 layer.anchorPoint = CGPointMake(0.5, 1); //设置位置和尺寸 layer.position = CGPointMake(kClockWith * 0.5, kClockWith * 0.5); layer.bounds = CGRectMake(0, 0, 1, kClockWith * 0.5 - 24); //将图层添加到父图层中 [self.clockView.layer addSublayer:layer]; self.secondLayer = layer; }
  • 设置定时器,定时执行动画

    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
  • 设置定时器触发时调用的方式,添加动画代码

    - (void)timeChange
    {
        //获取日历对象
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        //获取日期组件 NSDateComponents *components = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]]; //获取当前的时分秒值 NSInteger second = components.second; NSInteger munite = components.minute; NSInteger hour = components.hour; //计算当前时分秒表针转过的角度(弧度制) CGFloat secondAngle = angleToRadion(second * perSecondAngle); CGFloat muniteAngle = angleToRadion(munite * perMinuteAngle); CGFloat hourAngle = angleToRadion(hour *perHourAngle + munite * perMuniteHourAngle); //修改时分秒表针位于的图层的transform属性,执行隐式动画 self.secondLayer.transform = CATransform3DMakeRotation(secondAngle, 0, 0, 1); self.minuteLayer.transform = CATransform3DMakeRotation(muniteAngle, 0, 0, 1); self.hourLayer.transform = CATransform3DMakeRotation(hourAngle, 0, 0, 1); }

     

 
 

转载于:https://www.cnblogs.com/funny11/p/4970377.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值