下面教大家用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è)置到表盤(pán)的中心點(diǎn)赎败,而此時(shí)的anchorPoint還是(0。5蠢甲,0.5)僵刮,這是默認(rèn)的,如果需要設(shè)置成從表盤(pán)中心原點(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ù)悟民,就是秒針在表盤(pán)上的旋轉(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ò)去就可以用了贡茅。