iOS時鐘搭建

CALayer所有的旋轉(zhuǎn)和縮放都是圍繞錨點進(jìn)行的。UIView的center是相對于父控件而言的
#import "ViewController.h"

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

//每一秒旋轉(zhuǎn)6度
#define perSecA 6

//每一分旋轉(zhuǎn)6度
#define perMinA 6

//每一小時旋轉(zhuǎn) 30 度
#define perHourA 30

//每一分時針旋轉(zhuǎn)多少度 0.5
#define  perMinHourA 0.5

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *colockView;
@property (weak, nonatomic) CALayer *secL;
@property (weak, nonatomic) CALayer *minL;
@property (weak, nonatomic) CALayer *hourL;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //所有的旋轉(zhuǎn),縮放都是繞著錨點進(jìn)行的.
    //添加時針
    [self addHourL];
    
    //添加分針
    [self addMinL];
    
    //添加秒針
    [self addSecL];
   
    //添加定時器
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    
    [self timeChange];
}

- (void)timeChange {
    //讓秒針開始旋轉(zhuǎn)
    //獲取當(dāng)前的秒數(shù)
    //獲取當(dāng)前的日歷
    NSCalendar *cal = [NSCalendar currentCalendar];
    //從什么時間獲取日歷當(dāng)中的哪些組件(年,月,日,時,分,秒)
    NSDateComponents *cpt = [cal components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
    //當(dāng)前時間的秒數(shù)
    NSInteger curSec =  cpt.second;
    NSInteger curMin = cpt.minute;
    NSInteger curHour = cpt.hour;
    NSLog(@"%ld-%ld-%ld",curHour,curMin,curSec);
    
    //求角度 = 當(dāng)前的秒數(shù) * 每一秒旋轉(zhuǎn)多少度
    CGFloat secA = curSec * perSecA;
    self.secL.transform = CATransform3DMakeRotation(angleToRadio(secA), 0, 0, 1);
    
    //旋轉(zhuǎn)分針
    //求角度 = 當(dāng)前多少分 * 每一分旋轉(zhuǎn)多少度
    CGFloat minA = curMin * perMinA;
    self.minL.transform =  CATransform3DMakeRotation(angleToRadio(minA), 0, 0, 1);
    
    //旋轉(zhuǎn)時針
    //求角度 = 當(dāng)前多少小時 * 每一小時旋轉(zhuǎn)多少度
    CGFloat hourA = curHour * perHourA + curMin * perMinHourA;
    self.hourL.transform =  CATransform3DMakeRotation(angleToRadio(hourA), 0, 0, 1);
}

//所有的旋轉(zhuǎn),縮放都是繞著錨點進(jìn)行的.
- (void)addSecL {
    CALayer *secL = [CALayer layer];
    secL.backgroundColor = [UIColor redColor].CGColor;
    secL.bounds = CGRectMake(0, 0, 1, 80);
    
    secL.anchorPoint = CGPointMake(0.5, 1);
    secL.position = CGPointMake(self.colockView.bounds.size.width * 0.5, self.colockView.bounds.size.height * 0.5);
    
    [self.colockView.layer addSublayer:secL];
    self.secL = secL;
}

- (void)addMinL {
    CALayer *minL = [CALayer layer];
    minL.backgroundColor = [UIColor blackColor].CGColor;
    minL.bounds = CGRectMake(0, 0, 2, 70);
    minL.anchorPoint = CGPointMake(0.5, 1);
    minL.position = CGPointMake(self.colockView.bounds.size.width * 0.5, self.colockView.bounds.size.height * 0.5);
    
    [self.colockView.layer addSublayer:minL];
    self.minL = minL;
}

- (void)addHourL {
    CALayer *hourL = [CALayer layer];
    hourL.backgroundColor = [UIColor blackColor].CGColor;
    hourL.bounds = CGRectMake(0, 0, 3, 60);
    hourL.anchorPoint = CGPointMake(0.5, 1);
    hourL.position = CGPointMake(self.colockView.bounds.size.width * 0.5, self.colockView.bounds.size.height * 0.5);
    
    [self.colockView.layer addSublayer:hourL];
    self.hourL = hourL;
}

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

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

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