今天在点电脑桌面通知中心的时候看到了这个东西,突发奇想自己能不能写一个呢?
然后就做了一个效果这样的模拟时钟,还可以吧
Demo地址:https://github.com/JerryLMJ/LMJSimulationClock
其实原理简单的不要不要的,就是利用定时器驱动指针做旋转,唯一需要注意的就是指针视图的layer的anchorPoint(锚点)属性的设置,这个可能要量一下计算一下,我这里也只是计算了个大概不是很精确。
贴一下实现的代码:
// 图片资源来源于网络,版权归原作者所有,仅供学习交流使用
#define ClockBgImage @"clockBg.jpg"
#define HourPointerImage @"hourPointer"
#define MinutePointerImage @"minutePointer"
#define SecondPointerImage @"secondPointer"
// 图片资源来源于网络,版权归原作者所有,仅供学习交流使用
@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
版权声明:出自MajorLMJ技术博客的原创作品 ,转载时必须注明出处及相应链接!