iOS时钟开发案例分享时钟开发案例分享
本文实例为大家介绍了iOS时钟开发过程,供大家参考,具体内容如下
思路就是利用CALayer的隐式动画来实现。因为UIView的非根层也就是手动创建的layer在其属性发生变化时会默认会产生动
画效果,这些属性也叫作可动画属性。比如bounds、backgroundColor、position。
时钟里面表盘就是一个UIView,而三根针就是三个手动创建的layer。
先在storyboard上弄一个UIImageView,设置表盘图片
然后在viewDidLoad中初始化三根针,并设置定时器,获取当前时间,将当前时间对应的时针分针秒针分别指向对应的角度。
//
// ViewController.m
// 时钟效果
//
// Created by Daniel on 16/4/7.
// Copyright © 2016年 Daniel. All rights reserved.
//
#define kClockWH _clockView.bounds.size.width
//一秒钟秒针转多少度
#define preSecondA 6
//一分钟分针转多少度
#define preMinuteA 6
//一小时时针转多少度
#define preHourA 30
//每分钟时针转多少度
#define preHourMinute 0.5
//每秒钟分针转多少度
#define preMinuteSecond 0.1
#define angle2raditon(a) ((a) / 180.0 * M_PI)
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *clockView;
/** 秒针 */
@property(nonatomic, strong) CALayer *secondL;
/** 分针 */
@property(nonatomic, strong) CALayer *minuteL;
/** 时针 */
@property(nonatomic, strong) CALayer *hourL;
@end
@implementation ViewController