iOS制作一个模拟时钟

今天在点电脑桌面通知中心的时候看到了这个东西,突发奇想自己能不能写一个呢?



然后就做了一个效果这样的模拟时钟,还可以吧


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技术博客的原创作品 ,转载时必须注明出处及相应链接!

推荐阅读更多精彩内容

  • 思路: 构建一个新的数组res存放结果; for循环中每次从原数组中取出一个元素,判断这个元素在res中的索引值是...
    凌Linny阅读 101评论 0 0
  • 今天看着室友的大细腿,突然想明白了为什么我上个学期整个人那么颓废了。 以前刘老师讲过一句话:“你在大学谈不谈恋爱,...
    冋三金阅读 20评论 0 0
  • 在春熙路有两个乞丐,都是男性。 早上上班的时候会见到一个, 晚上下班的时候会见到另外一个。 早上的乞丐,衣着除了脏...
    菊大侠说阅读 87评论 0 1
  • 有时候周瑜从高处望去,会想起少年时立马远眺,身边的挚友伸手丈量,脚下那些壮丽江山尽在掌间,他回头朗笑说公瑾你看,这...
    洞庭府君阅读 336评论 0 9