Graphics Reference
In-Depth Information
repeats
:
YES
];
//set initial hand positions
[
self
tick
];
}
- (
void
)tick
{
//convert time to hours, minutes and seconds
NSCalendar
*calendar =
[[
NSCalendar
alloc
]
initWithCalendarIdentifier
:
NSGregorianCalendar
];
NSUInteger
units =
NSHourCalendarUnit
|
NSMinuteCalendarUnit
|
NSSecondCalendarUnit
;
NSDateComponents
*components = [calendar
components
:units
fromDate:
[
NSDate
date
]];
//calculate hour hand angle
CGFloat
hoursAngle = (components.
hour
/
12.0
) *
M_PI
*
2.0
;
//calculate minute hand angle
CGFloat
minsAngle = (components.
minute
/
60.0
) *
M_PI
*
2.0
;
//calculate second hand angle
CGFloat
secsAngle = (components.
second
/
60.0
) *
M_PI
*
2.0
;
//rotate hands
self
.hourHand.
transform
=
CGAffineTransformMakeRotation
(hoursAngle);
self
.minuteHand.
transform
=
CGAffineTransformMakeRotation
(minsAngle);
self
.secondHand.
transform
=
CGAffineTransformMakeRotation
(secsAngle);
}
@end
When we run the clock app, it looks a bit strange (see Figure 3.6). The reason for this is that
the hand images are rotating around the center of the image, which is not where we would
expect a clock's hand to pivot.
You might think that this could be fixed by adjusting the position of the hand images in
Interface Builder, but that won't work because the images will not rotate correctly if they
are not centered on the clock face.