今天在點電腦桌面通知中心的時候看到了這個東西犀被,突發(fā)奇想自己能不能寫一個呢琢唾?
然后就做了一個效果這樣的模擬時鐘楞件,還可以吧
Demo地址:https://github.com/JerryLMJ/LMJSimulationClock
其實原理簡單的不要不要的兴蒸,就是利用定時器驅(qū)動指針做旋轉(zhuǎn)视粮,唯一需要注意的就是指針視圖的layer的anchorPoint(錨點)屬性的設置,這個可能要量一下計算一下橙凳,我這里也只是計算了個大概不是很精確蕾殴。
貼一下實現(xiàn)的代碼:
// 圖片資源來源于網(wǎng)絡,版權(quán)歸原作者所有痕惋,僅供學習交流使用
#define ClockBgImage @"clockBg.jpg"
#define HourPointerImage @"hourPointer"
#define MinutePointerImage @"minutePointer"
#define SecondPointerImage @"secondPointer"
// 圖片資源來源于網(wǎng)絡区宇,版權(quán)歸原作者所有,僅供學習交流使用
@implementation LMJSimulationClock
{
NSTimer * _timer;
UIImageView * _clockBg;
UIImageView * _hourPointer;
UIImageView * _minutePointer;
UIImageView * _secondPointer;
}
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self initViews];
[self clockAction];
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(clockAction) userInfo:nil repeats:YES];
}
return self;
}
- (void)initViews{
_clockBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
_clockBg.image = [UIImage imageNamed:ClockBgImage];
_clockBg.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
[self addSubview:_clockBg];
_hourPointer = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 93)];
_hourPointer.image = [UIImage imageNamed:HourPointerImage];
_hourPointer.center = CGPointMake(_clockBg.frame.size.width/2, _clockBg.frame.size.height/2);
_hourPointer.layer.anchorPoint = CGPointMake(0.5, 0.92);
[_clockBg addSubview:_hourPointer];
_minutePointer = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 120)];
_minutePointer.image = [UIImage imageNamed:MinutePointerImage];
_minutePointer.center = CGPointMake(_clockBg.frame.size.width/2, _clockBg.frame.size.height/2);
_minutePointer.layer.anchorPoint = CGPointMake(0.5, 0.945);
[_clockBg addSubview:_minutePointer];
_secondPointer = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 220)];
_secondPointer.image = [UIImage imageNamed:SecondPointerImage];
_secondPointer.center = CGPointMake(_clockBg.frame.size.width/2, _clockBg.frame.size.height/2);
_secondPointer.layer.anchorPoint = CGPointMake(0.5, 0.65);
[_clockBg addSubview:_secondPointer];
}
- (void)clockAction{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents * components = [calendar components:units fromDate:[NSDate date]];
CGFloat hourAngle = (components.hour *3600 + components.minute*60 +components.second) / (12*3600.f) * 2*M_PI;
CGFloat minuteAngle = (components.minute*60 +components.second) / 3600.f * 2*M_PI;
CGFloat secondAngle = (components.second) / 60.f * 2*M_PI;
_hourPointer.transform = CGAffineTransformMakeRotation(hourAngle);
_minutePointer.transform = CGAffineTransformMakeRotation(minuteAngle);
_secondPointer.transform = CGAffineTransformMakeRotation(secondAngle);
}
- (void)dealloc{
[_timer invalidate];
_timer = nil;
}
@end
Demo地址:https://github.com/JerryLMJ/LMJSimulationClock
版權(quán)聲明:出自MajorLMJ技術(shù)博客的原創(chuàng)作品 值戳,轉(zhuǎn)載時必須注明出處及相應鏈接议谷!