您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)簡(jiǎn)易鐘表的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
注意:表盤是一個(gè)UIImageView控件,設(shè)置image為表盤圖片
核心代碼:
// // ViewController.m // 時(shí)鐘 // // Created by llkj on 2017/8/29. // Copyright © 2017年 LayneCheung. All rights reserved. // #import "ViewController.h" //每一秒旋轉(zhuǎn)多少度 #define perSecA 6 //每一分旋轉(zhuǎn)多少度 #define perMinA 6 //每一小時(shí)旋轉(zhuǎn)多少度 #define perHourA 30 //每一分時(shí)針旋轉(zhuǎn)的度數(shù) #define perMinHour 0.5 //角度轉(zhuǎn)弧度 #define angle2Rad(angle) ((angle) / 180.0 * M_PI) @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *clockView; @property (nonatomic, weak) CALayer *secL; @property (nonatomic, weak) CALayer *minL; @property (nonatomic, weak) CALayer *hourL; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self setHour]; [self setMin]; [self setSec]; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES]; [self timeChange]; } - (void)timeChange{ //獲取當(dāng)前秒 NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *cmp = [cal components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]]; NSInteger curSec = cmp.second + 1; NSInteger curMin = cmp.minute; NSInteger curHour = cmp.hour; //秒針開始旋轉(zhuǎn) //計(jì)算秒針當(dāng)前旋轉(zhuǎn)的角度 // angle = 當(dāng)前多少秒 * 每一秒旋轉(zhuǎn)多少度 CGFloat secA = curSec * perSecA; //旋轉(zhuǎn)方向是Z軸 self.secL.transform = CATransform3DMakeRotation(angle2Rad(secA), 0, 0, 1); //分針開始旋轉(zhuǎn) //計(jì)算分針當(dāng)前旋轉(zhuǎn)的角度 // angle = 當(dāng)前多少分 * 每一分旋轉(zhuǎn)多少度 CGFloat minA = curMin * perMinA; self.minL.transform = CATransform3DMakeRotation(angle2Rad(minA), 0, 0, 1); //時(shí)針開始旋轉(zhuǎn) //計(jì)算時(shí)針當(dāng)前旋轉(zhuǎn)的角度 // angle = 當(dāng)前多少時(shí) * 每一小時(shí)旋轉(zhuǎn)多少度 CGFloat hourA = curHour * perHourA + curMin * perMinHour; self.hourL.transform = CATransform3DMakeRotation(angle2Rad(hourA), 0, 0, 1); } //添加秒針 - (void)setSec{ CALayer *secL = [CALayer layer]; secL.bounds = CGRectMake(0, 0, 1, 80); secL.backgroundColor = [UIColor redColor].CGColor; //繞著錨點(diǎn)旋轉(zhuǎn) secL.anchorPoint = CGPointMake(0.5, 1); secL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5); [self.clockView.layer addSublayer:secL]; self.secL = secL; } //添加分針 - (void)setMin{ CALayer *minL = [CALayer layer]; minL.bounds = CGRectMake(0, 0, 3, 70); minL.cornerRadius = 1.5; minL.backgroundColor = [UIColor blackColor].CGColor; minL.anchorPoint = CGPointMake(0.5, 1); minL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5); [self.clockView.layer addSublayer:minL]; self.minL = minL; } //添加時(shí)針 - (void)setHour{ CALayer *hourL = [CALayer layer]; hourL.bounds = CGRectMake(0, 0, 3, 60); hourL.backgroundColor = [UIColor blackColor].CGColor; hourL.anchorPoint = CGPointMake(0.5, 1); hourL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5); [self.clockView.layer addSublayer:hourL]; self.hourL = hourL; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。