
iOS 中圖層預設繞中心點旋轉,因為 anchorPoint 預設在圖層中心。要讓指針繞著底部中心轉,必須改錨點位置,再依錨點反推 position,使其落在時鐘的中點。
旋轉角度的計算:
時間資料透過 NSCalendar 取得 NSDateComponents 中的各個組成成分,再用 NSTimer 每秒更新一次。最後把指針頭尾設圓角,視覺上更精緻。
#import "ViewController.h"
// 取得當前的各個時間成分
#define CURRENTSEC [[NSCalendar currentCalendar] component:NSCalendarUnitSecond fromDate:[NSDate date]]
#define CURRENTMIN [[NSCalendar currentCalendar] component:NSCalendarUnitMinute fromDate:[NSDate date]]
#define CURRENTHOUR [[NSCalendar currentCalendar] component:NSCalendarUnitHour fromDate:[NSDate date]]
// 角度轉弧度
#define ANGEL(x) (x / 180.0 * M_PI)
#define kPerSecondA ANGEL(6) // 每秒 6 度
#define kPerMinuteA ANGEL(6) // 每分鐘 6 度
#define kPerHourA ANGEL(30) // 每小時 30 度
#define kPerHourMinuteA ANGEL(0.5) // 時針每分鐘 0.5 度
@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageClock;
@property (nonatomic, strong) CALayer *layerSec;
@property (nonatomic, strong) CALayer *layerMin;
@property (nonatomic, strong) CALayer *layerHour;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.imageClock];
[self.imageClock.layer addSublayer:self.layerSec];
[self.imageClock.layer addSublayer:self.layerMin];
[self.imageClock.layer addSublayer:self.layerHour];
[self timeChange];
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timeChange)
userInfo:nil
repeats:YES];
}
#pragma mark - 更新指針角度
- (void)timeChange {
self.layerSec.transform =
CATransform3DMakeRotation(CURRENTSEC * kPerSecondA, 0, 0, 1);
self.layerMin.transform =
CATransform3DMakeRotation(CURRENTMIN * kPerMinuteA, 0, 0, 1);
// 時針 = 小時部分 + 分鐘推進的部分
self.layerHour.transform = CATransform3DMakeRotation(
CURRENTHOUR * kPerHourA + CURRENTMIN * kPerHourMinuteA, 0, 0, 1);
}
#pragma mark - 懶載入
- (UIImageView *)imageClock {
if (_imageClock == nil) {
_imageClock = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"鐘表"]];
_imageClock.frame = CGRectMake(100, 100, 200, 200);
}
return _imageClock;
}
- (CALayer *)layerSec {
if (_layerSec == nil) {
_layerSec = [CALayer layer];
_layerSec.bounds = CGRectMake(0, 0, 2, 80);
_layerSec.backgroundColor = [UIColor redColor].CGColor;
_layerSec.cornerRadius = 1;
_layerSec.anchorPoint = CGPointMake(0.5, 1); // 錨點在底部中央
_layerSec.position = CGPointMake(
self.imageClock.bounds.size.width / 2,
self.imageClock.bounds.size.height / 2);
}
return _layerSec;
}
- (CALayer *)layerMin {
if (_layerMin == nil) {
_layerMin = [CALayer layer];
_layerMin.bounds = CGRectMake(0, 0, 4, 60);
_layerMin.backgroundColor = [UIColor blackColor].CGColor;
_layerMin.cornerRadius = 2;
_layerMin.anchorPoint = CGPointMake(0.5, 1);
_layerMin.position = CGPointMake(
self.imageClock.bounds.size.width / 2,
self.imageClock.bounds.size.height / 2);
}
return _layerMin;
}
- (CALayer *)layerHour {
if (_layerHour == nil) {
_layerHour = [CALayer layer];
_layerHour.bounds = CGRectMake(0, 0, 6, 40);
_layerHour.backgroundColor = [UIColor blackColor].CGColor;
_layerHour.cornerRadius = 3;
_layerHour.anchorPoint = CGPointMake(0.5, 1);
_layerHour.position = CGPointMake(
self.imageClock.bounds.size.width / 2,
self.imageClock.bounds.size.height / 2);
}
return _layerHour;
}
@end
CABasicAnimation,把 byValue 設為單格角度、duration 設為 1 秒並開 repeatCount = MAXFLOAT。