ios-簡(jiǎn)易時(shí)鐘

下面教大家用CALayer做時(shí)鐘,不啰嗦了,上圖上代碼。

#import"ViewController.h"

#define perSec6

#define perMin6

#define perHour30

#define angleToRadious(angle) ((angle)/180.0* M_PI)

@interfaceViewController()

@property(nonatomic,strong)CALayer*secLay;

@property(nonatomic,strong)CALayer*minLay;

@property(nonatomic,strong)CALayer*HourLay;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

UIImageView*img = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"timg.jpeg"]];

img.frame=CGRectMake(0,0,200,200);

img.center=self.view.center;

[self.viewaddSubview:img];

//分針

self.minLay= [[CALayeralloc]init];

self.minLay.frame=CGRectMake(0,0,2,80);

//position這個(gè)坐標(biāo)是相對(duì)父視圖的

self.minLay.position=CGPointMake(img.bounds.size.width/2, img.bounds.size.height/2);

//anchorPoint:這個(gè)坐標(biāo)是本View的中心坐標(biāo),anchorPoint跟position是永遠(yuǎn)重合的,所以想要anchorPoint和position相同,則需要把a(bǔ)nchorPoint設(shè)置為(0,0),但該案例需求不同,因?yàn)槲覀兿M衙脶樤O(shè)置到表盤的中心點(diǎn),而此時(shí)的anchorPoint還是(0。5,0.5),這是默認(rèn)的,如果需要設(shè)置成從表盤中心原點(diǎn)向外延伸的一條直線,則需要把a(bǔ)nchorPoint設(shè)置為(0.5,1),0.5是為了讓該線處于原點(diǎn)中心,而1呢,是為了讓它與position重合。

self.minLay.anchorPoint=CGPointMake(0.5,1);

self.minLay.backgroundColor= [UIColorblackColor].CGColor;

[img.layeraddSublayer:self.minLay];

//時(shí)針

self.HourLay= [[CALayeralloc]init];

self.HourLay.frame=CGRectMake(0,0,2,60);

self.HourLay.position=CGPointMake(img.bounds.size.width/2, img.bounds.size.height/2);

self.HourLay.anchorPoint=CGPointMake(0.5,1);

self.HourLay.backgroundColor= [UIColorblackColor].CGColor;

[img.layeraddSublayer:self.HourLay];

//秒針

self.secLay= [[CALayeralloc]init];

self.secLay.frame=CGRectMake(0,0,1,80);

self.secLay.position=CGPointMake(img.bounds.size.width/2, img.bounds.size.height/2);

self.secLay.anchorPoint=CGPointMake(0.5,1);

self.secLay.backgroundColor= [UIColorredColor].CGColor;

[img.layeraddSublayer:self.secLay];

//數(shù)字Label

UILabel*timelb = [[UILabelalloc]init];

timelb.frame=CGRectMake(0,120,self.view.bounds.size.width,100);

timelb.font= [UIFontsystemFontOfSize:50];

timelb.textAlignment=NSTextAlignmentCenter;

[self.viewaddSubview:timelb];

__weakViewController*weakSelf =self;

[NSTimerscheduledTimerWithTimeInterval:1repeats:YESblock:^(NSTimer*_Nonnulltimer) {

//獲取當(dāng)前日歷

NSCalendar*cal = [NSCalendarcurrentCalendar];

//根據(jù)需要獲取對(duì)應(yīng)的日歷參數(shù),比如時(shí)分秒:NSCalendarUnitHour、NSCalendarUnitMinute、NSCalendarUnitSecond

NSDateComponents*cmp = [calcomponents:NSCalendarUnitSecond|NSCalendarUnitMinute|NSCalendarUnitHourfromDate:[NSDatedate]];

NSIntegersec = cmp.second;

NSIntegermin = cmp.minute;

NSIntegerhour = cmp.hour;

//秒針一分鐘走360度,每一秒是6度,根據(jù)當(dāng)前秒數(shù)乘以度數(shù),就是秒針在表盤上的旋轉(zhuǎn)度數(shù)了,同理分針

CGFloatcurrSec = sec *perSec;

CGFloatcruuMin = min *perMin;

//時(shí)針比較特殊,它的幅度還有分針的幅度有關(guān),那么需要知道一小時(shí)占六十分鐘的百分比是多少?就是30/60 = 0.5度。舉個(gè)例子:當(dāng)時(shí)間在1小時(shí)45分的時(shí)候,如果用hour * perHour,那么得出的時(shí)針只是指到1點(diǎn)而已,而實(shí)際中,這個(gè)幅度是已經(jīng)快兩點(diǎn)了,那么是不符合要求的,所以需要知道分針當(dāng)前的時(shí)間乘上0.5度再加上hour * perHour,這才是正確的刻度。

CGFloatcurrhour = hour *perHour+ min *0.5;

/*

*CATransform3DMakeRotation(<#CGFloat angle#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat z#>)

*在這個(gè)API中,第一個(gè)參數(shù)表示旋轉(zhuǎn)的度數(shù),而在上面中,我們已經(jīng)知道了時(shí)分秒該旋轉(zhuǎn)多少度,但還不是真正的度數(shù),需要進(jìn)行轉(zhuǎn)換

* #define angleToRadious(angle) ((angle)/180.0 * M_PI)通過(guò)這個(gè)宏,轉(zhuǎn)換成度數(shù)

*后面的X,Y,Z的數(shù)值取值都是在[0,1]區(qū)間的,在這個(gè)案例中,因?yàn)槲覀兊臅r(shí)分秒針都圍繞Z軸旋轉(zhuǎn)的,所以X,Y取零便可。

*/

weakSelf.secLay.transform=CATransform3DMakeRotation(angleToRadious(currSec),0,0,1);

weakSelf.minLay.transform=CATransform3DMakeRotation(angleToRadious(cruuMin),0,0,1);

weakSelf.HourLay.transform=CATransform3DMakeRotation(angleToRadious(currhour),0,0,1);

timelb.text= [NSStringstringWithFormat:@"%zd : %zd : %zd",hour,min,sec];

}];

}

@end


圖一

效果如圖一所示,直接新建View或者ViewController,代碼原封不動(dòng)搬過(guò)去就可以用了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • #import "ViewController.h" //每一秒旋轉(zhuǎn)多少度 #define perSecA6 //...
    會(huì)寫bug的程序媛閱讀 1,731評(píng)論 4 1
  • Transform field value key paths! position.x position.y 1....
    youngZhou閱讀 403評(píng)論 0 1
  • CALayer 概念 在iOS中,你能看得見摸得著的東西基本上都是UIView,比如一個(gè)按鈕、一個(gè)文本標(biāo)簽、一個(gè)文...
    iOS_Cqlee閱讀 1,102評(píng)論 8 2
  • 動(dòng)畫只是Core Animation特性的冰山一角 Core Animation功能 Core Animation...
    封樓閱讀 425評(píng)論 0 0
  • 晚上下班回來(lái)經(jīng)常盯著手機(jī)屏幕發(fā)呆,就想寫點(diǎn)什么,打發(fā)下漫長(zhǎng)的夜晚,并記錄下工作中的點(diǎn)點(diǎn)滴滴。 1.tablevie...
    so_what閱讀 1,911評(píng)論 3 5

友情鏈接更多精彩內(nèi)容