如何旋转24小时制的时钟指针? [英] How to rotate the hands of clock for the 24 hour clock?
问题描述
我正在做一个时钟项目,我知道如何实现12小时制的指针(时,分,秒)旋转.我使用的代码如下
I am working on a clock project, i know how to implement hands (Hour, Minute, Seconds) rotation of 12 hour clock. The code i have used is follows
CGFloat secAngle,minAngle,hourAngle;
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
NSInteger seconds = [dateComponents second];
NSInteger minutes = [dateComponents minute];
NSInteger hours = [dateComponents hour];
if (timeFormat == 12)//12 hour clock
{
if (hours > 12) hours -=12; //PM
secAngle = Degrees2Radians(seconds/60.0*360);
minAngle = Degrees2Radians(minutes/60.0*360);
hourAngle = Degrees2Radians(hours/12.0*360) + minAngle/12.0;
//secHand.transform = CATransform3DMakeRotation (secAngle+M_PI, 0, 0, 1);
minHand.transform = CATransform3DMakeRotation (minAngle+M_PI, 0, 0, 1);
hourHand.transform = CATransform3DMakeRotation (hourAngle+M_PI, 0, 0, 1);
}
else //24 hour clock
{
secAngle = Degrees2Radians(seconds/60.0*360);
minAngle = Degrees2Radians(minutes/60.0*360);
hourAngle = (30 * hours + minutes / 2) /2;
//secHand.transform = CATransform3DMakeRotation(secAngle * M_PI / 180,0,0,1);
minHand.transform = CATransform3DMakeRotation(minAngle * M_PI ,0,0,1);
hourHand.transform = CATransform3DMakeRotation(hourAngle * M_PI,0,0,1);
}
但是当我尝试实施24小时制时.它不起作用.每当我运行此项目时,时钟都会显示错误的时间.
But when i try to implement the 24 hour clock. It is not working. Each time when i run this project the clock shows wrong timings.
谁能告诉我我做错了什么.提前非常感谢
Can anyone tell me what wrong do i did. Many thanks in advance
推荐答案
这是我的工作方式-我使用脸部中心和角度来计算手部端点的x,y位置,从那里开始绘制很简单.这是完整的表盘类.它是UIView的子类.每秒设置它的时间属性,它就像一个魅力...
Here's how I do it - I use the face center and the angle to compute the x,y position of the endpoints of the hands, drawing is simple from there. Here's a complete clock face class. It's a subclass of UIView. Set it's time property every second and it works like a charm...
//
// ClockFace.h
//
#import <UIKit/UIKit.h>
@interface ClockFace : UIView
@property (strong,nonatomic) NSDate *time;
@end
// ClockFace.m
#import "ClockFace.h"
@interface ClockFace ()
@property(assign,nonatomic) CGPoint boundsCenter;
@property(assign,nonatomic) NSInteger seconds;
@property(assign,nonatomic) NSInteger minutes;
@property(assign,nonatomic) NSInteger hours;
@end
@implementation ClockFace
@synthesize time=_time;
@synthesize boundsCenter=_boundsCenter;
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
_boundsCenter = CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0);
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_boundsCenter = CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0);
}
return self;
}
- (CGPoint)secondsHandPosition {
float secondsAsRadians = (float)self.seconds / 60.0 * 2.0 * M_PI - M_PI_2;
float handRadius = self.frame.size.width / 2.4;
return CGPointMake(handRadius*cosf(secondsAsRadians)+self.boundsCenter.x, handRadius*sinf(secondsAsRadians)+self.boundsCenter.y);
}
- (CGPoint)minutesHandPosition {
float minutesAsRadians = (float)self.minutes / 60.0 * 2.0 * M_PI - M_PI_2;
float handRadius = self.frame.size.width / 2.2;
return CGPointMake(handRadius*cosf(minutesAsRadians)+self.boundsCenter.x, handRadius*sinf(minutesAsRadians)+self.boundsCenter.y);
}
- (CGPoint)hoursHandPosition {
float hoursAsRadians = (float)self.hours / 12.0 * 2.0 * M_PI - M_PI_2;
float handRadius = self.frame.size.width / 3.8;
return CGPointMake(handRadius*cosf(hoursAsRadians)+self.boundsCenter.x, handRadius*sinf(hoursAsRadians)+self.boundsCenter.y);
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat zClear[4] = {1.0,1.0,1.0,0.0};
CGContextSetFillColor(context, zClear);
CGContextFillRect(context, rect);
CGRect face = CGRectInset(rect, 1.0, 1.0);
CGContextSetLineWidth(context, 1.0);
CGContextAddEllipseInRect(context, face);
CGContextStrokePath(context);
CGRect center = CGRectMake(self.boundsCenter.x-2.0, self.boundsCenter.y-2.0, 4.0, 4.0);
CGContextAddEllipseInRect(context, center);
CGContextStrokePath(context);
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGPoint secondsHandPosition = [self secondsHandPosition];
CGContextSetStrokeColor(context, red);
CGContextBeginPath(context);
CGContextMoveToPoint(context, self.boundsCenter.x, self.boundsCenter.y);
CGContextAddLineToPoint(context, secondsHandPosition.x, secondsHandPosition.y);
CGContextStrokePath(context);
CGFloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
CGPoint minutesHandPosition = [self minutesHandPosition];
CGContextSetStrokeColor(context, black);
CGContextBeginPath(context);
CGContextMoveToPoint(context, self.boundsCenter.x, self.boundsCenter.y);
CGContextAddLineToPoint(context, minutesHandPosition.x, minutesHandPosition.y);
CGContextStrokePath(context);
CGPoint hoursHandPosition = [self hoursHandPosition];
CGContextSetStrokeColor(context, black);
CGContextBeginPath(context);
CGContextMoveToPoint(context, self.boundsCenter.x, self.boundsCenter.y);
CGContextAddLineToPoint(context, hoursHandPosition.x, hoursHandPosition.y);
CGContextStrokePath(context);
}
- (void)setTime:(NSDate *)time {
_time = time;
static NSCalendar *gregorian;
if (!gregorian) gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
[gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:time];
self.hours = [weekdayComponents hour];
if (self.hours > 12) self.hours -= 12;
self.minutes = [weekdayComponents minute];
self.seconds = [weekdayComponents second];
[self setNeedsDisplay];
}
@end
要在视图控制器中使用此属性,请创建一个属性:
To use this in a view controller, create a property:
#import "ClockFace.h"
// ...
@property(strong, non atomic) ClockFace *clockFace;
确实加载了视图(或者如果您知道如何添加自定义视图,则在情节提要中):
In view did load (or in storyboard if you know how to add custom views):
self.clockFace = [[ClockFace alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
只要您希望时钟运行,请设置一个计时器:
Whenever you want the clock to run, setup a timer:
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:NO];
当计时器启动时,告诉时钟现在几点:
When the timer fires, tell the clock what time it is:
- (void)timerFired:(NSTimer *)timer {
self.clockFace.time = [NSDate date];
}
这篇关于如何旋转24小时制的时钟指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!