iOS時鐘開發(fā)案例分享
本文實例為大家介紹了iOS時鐘開發(fā)過程,供大家參考,具體內(nèi)容如下
思路就是利用CALayer的隱式動畫來實現(xiàn)。因為UIView的非根層也就是手動創(chuàng)建的layer在其屬性發(fā)生變化時會默認會產(chǎn)生動畫效果,這些屬性也叫作可動畫屬性。比如bounds、backgroundColor、position。
時鐘里面表盤就是一個UIView,而三根針就是三個手動創(chuàng)建的layer。
先在storyboard上弄一個UIImageView,設(shè)置表盤圖片
然后在viewDidLoad中初始化三根針,并設(shè)置定時器,獲取當(dāng)前時間,將當(dāng)前時間對應(yīng)的時針分針秒針分別指向?qū)?yīng)的角度。
// // ViewController.m // 時鐘效果 // // Created by Daniel on 16/4/7. // Copyright © 2016年 Daniel. All rights reserved. // #define kClockWH _clockView.bounds.size.width //一秒鐘秒針轉(zhuǎn)多少度 #define preSecondA 6 //一分鐘分針轉(zhuǎn)多少度 #define preMinuteA 6 //一小時時針轉(zhuǎn)多少度 #define preHourA 30 //每分鐘時針轉(zhuǎn)多少度 #define preHourMinute 0.5 //每秒鐘分針轉(zhuǎn)多少度 #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 - (void)viewDidLoad { [super viewDidLoad]; //添加時針 [self setUpHourLayer]; //添加分針 [self setUpMinuteLayer]; //添加秒針 [self setUpSecondLayer]; //添加定時器 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES]; //開始運行時就獲取時間,這樣在啟動時就不會有停頓的感覺 [self timeChange]; } - (void)timeChange { //獲取當(dāng)前系統(tǒng)時間 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *cmp = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]]; //獲取秒 NSInteger second = cmp.second; //獲取分 NSInteger minute = cmp.minute; //獲取小時 NSInteger hour = cmp.hour; //計算秒針轉(zhuǎn)多少度 CGFloat secondA = second * preSecondA; //計算分針轉(zhuǎn)多少度 CGFloat minuteA = minute * preMinuteA + second * preMinuteSecond; //計算時針轉(zhuǎn)多少度 CGFloat hourA = hour * preHourA + minute * preHourMinute; //旋轉(zhuǎn)秒針 _secondL.transform = CATransform3DMakeRotation(angle2raditon(secondA), 0, 0, 1); //旋轉(zhuǎn)分針 _minuteL.transform = CATransform3DMakeRotation(angle2raditon(minuteA), 0, 0, 1); //旋轉(zhuǎn)時針 _hourL.transform = CATransform3DMakeRotation(angle2raditon(hourA), 0, 0, 1); } #pragma mark - 初始化時針 - (void)setUpHourLayer { CALayer *hourL = [CALayer layer]; //設(shè)置秒針背景色 hourL.backgroundColor = [UIColor blackColor].CGColor; //設(shè)置秒針錨點 hourL.anchorPoint = CGPointMake(0.5, 1); //設(shè)置秒針錨點在父控件的位置 hourL.position = CGPointMake(kClockWH * 0.5, kClockWH * 0.5); hourL.cornerRadius = 4; //設(shè)置秒針bounds hourL.bounds = CGRectMake(0, 0, 4, kClockWH * 0.5 - 40); //把秒針添加到clockView圖層上 [_clockView.layer addSublayer:hourL]; _hourL = hourL; } #pragma mark - 初始化分針 - (void)setUpMinuteLayer { CALayer *minuteL = [CALayer layer]; //設(shè)置秒針背景色 minuteL.backgroundColor = [UIColor blackColor].CGColor; //設(shè)置秒針錨點 minuteL.anchorPoint = CGPointMake(0.5, 1); //設(shè)置秒針錨點在父控件的位置 minuteL.position = CGPointMake(kClockWH * 0.5, kClockWH * 0.5); minuteL.cornerRadius = 4; //設(shè)置秒針bounds minuteL.bounds = CGRectMake(0, 0, 4, kClockWH * 0.5 - 20); //把秒針添加到clockView圖層上 [_clockView.layer addSublayer:minuteL]; _minuteL = minuteL; } #pragma mark - 初始化秒針 - (void)setUpSecondLayer { CALayer *secondL = [CALayer layer]; //設(shè)置秒針背景色 secondL.backgroundColor = [UIColor redColor].CGColor; //設(shè)置秒針錨點 secondL.anchorPoint = CGPointMake(0.5, 1); //設(shè)置秒針錨點在父控件的位置 secondL.position = CGPointMake(kClockWH * 0.5, kClockWH * 0.5); //設(shè)置秒針bounds secondL.bounds = CGRectMake(0, 0, 1.5, kClockWH * 0.5 - 20); //把秒針添加到clockView圖層上 [_clockView.layer addSublayer:secondL]; _secondL = secondL; } @end
效果圖:
以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)IOS程序設(shè)計有所幫助。
相關(guān)文章
iOS開發(fā)中ViewController的頁面跳轉(zhuǎn)和彈出模態(tài)
這篇文章主要介紹了iOS開發(fā)中ViewController的頁面跳轉(zhuǎn)和彈出模態(tài),ViewController是MVC開發(fā)模式中一個重要的類,需要的朋友可以參考下2015-10-10IOS 開發(fā)APP之關(guān)于時間處理詳細介紹
這篇文章主要介紹了IOS 開發(fā)APP之關(guān)于時間處理詳細介紹的相關(guān)資料,開發(fā)APP 不僅需要對API的調(diào)用還需要對時間相關(guān)的各種API之間的差別,再因場景而異去設(shè)計相應(yīng)的機制,需要的朋友可以參考下2016-12-12ios的collection控件的自定義布局實現(xiàn)與設(shè)計
這篇文章主要介紹了mac、iOS端支持自定義布局的collection控件的實現(xiàn)與設(shè)計,需要的朋友學(xué)習(xí)參考下吧。2017-12-12簡介Objective-C解析XML與JSON數(shù)據(jù)格式的方法
這篇文章主要介紹了Objective-C解析XML與JSON數(shù)據(jù)格式的方法,文中解析JSON包括拼接JSON字符串用到了SBJson這個解析器,需要的朋友可以參考下2016-01-01iOS開發(fā)之?dāng)r截URL轉(zhuǎn)換成本地路由模塊URLRewrite詳解
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)之?dāng)r截URL轉(zhuǎn)換成本地路由模塊URLRewrite的相關(guān)資料,這是最近在工作中遇到的一個需求,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起看看吧。2017-08-08