/ Published in: Objective C
Simple calculation for a clock
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
@property (weak, nonatomic) IBOutlet UIImageView *hourHands; @property (weak, nonatomic) IBOutlet UIImageView *minuteHands; @property (weak, nonatomic) IBOutlet UIImageView *secondHands; - (void)viewDidLoad { [super viewDidLoad]; self.secondHands.layer.anchorPoint = CGPointMake(0.5f, 0.9f); self.minuteHands.layer.anchorPoint = CGPointMake(0.5f, 0.9f); self.hourHands.layer.anchorPoint = CGPointMake(0.5f, 0.9f); self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateClockHands) userInfo:nil repeats:YES]; // Make sure user does not see uncalibrated hands at 0,0 position. [self updateClockHands]; } -(void) updateClockHands { NSUInteger units = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; CGFloat hoursAngle = (components.hour /12.0) * M_PI * 2.0; CGFloat minsAngle = (components.minute / 60.0) * M_PI * 2.0; CGFloat secsAngle = (components.second / 60.0) * M_PI * 2.0; // Now you can adjust the hands of a clock // Assuming hourHands, minuteHands, secondHands are UIImageview's and are setup as properties in .m self.hourHands.transform = CGAffineTransformMakeRotation(hoursAngle); self.minuteHands.transform = CGAffineTransformMakeRotation(minsAngle); self.secondHands.transform = CGAffineTransformMakeRotation(secsAngle); }