Graphics Reference
In-Depth Information
}
- (
void
)tick
{
[
self
updateHandsAnimated
:
YES
];
}
- (
void
)updateHandsAnimated:(
BOOL
)animated
{
//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
hourAngle = (components.
hour
/
12.0
) *
M_PI
*
2.0
;
//calculate minute hand angle
CGFloat
minuteAngle = (components.
minute
/
60.0
) *
M_PI
*
2.0
;
//calculate second hand angle
CGFloat
secondAngle = (components.
second
/
60.0
) *
M_PI
*
2.0
;
//rotate hands
[
self
setAngle
:hourAngle
forHand
:
self
.hourHand
animated
:animated];
[
self
setAngle
:minuteAngle
forHand
:
self
.minuteHand
animated
:animated];
[
self
setAngle
:secondAngle
forHand
:
self
.secondHand
animated
:animated];
}
- (
void
)setAngle:(
CGFloat
)angle
forHand:(
UIView
*)handView
animated:(
BOOL
)animated
{
//generate transform
CATransform3D
transform =
CATransform3DMakeRotation
(angle,
0
,
0
,
1
);
if
(animated)
{
//create transform animation
CABasicAnimation
*animation = [
CABasicAnimation
animation
];