android sdio 时钟 ios->clock,iOS制作一个模拟时钟

这篇博客介绍了如何在iOS平台上利用定时器和Core Animation创建一个模拟时钟,通过设置图片资源和调整UIImageView的transform属性,使指针随时间旋转,展示了简单的iOS界面动态效果实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

d12b2c4d0423

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

d12b2c4d0423

其实原理简单的不要不要的,就是利用定时器驱动指针做旋转,唯一需要注意的就是指针视图的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

版权声明:出自MajorLMJ技术博客的原创作品 ,转载时必须注明出处及相应链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值