Graphics Reference
In-Depth Information
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//add the ship
self
.shipLayer = [
CALayer
layer
];
self
.shipLayer.
frame
=
CGRectMake
(
0
,
0
,
128
,
128
);
self
.shipLayer.
position
=
CGPointMake
(
150
,
150
);
self
.shipLayer.
contents
= (
__bridge
id
)[
UIImage
imageNamed
:
@"Ship.png"
].
CGImage
;
[
self
.containerView.
layer
addSublayer
:
self
.shipLayer];
}
- (
IBAction
)start
{
//animate the ship rotation
CABasicAnimation
*animation = [
CABasicAnimation
animation
];
animation.
keyPath
=
@"transform.rotation"
;
animation.
duration
=
2.0
;
animation.
byValue
=
@(
M_PI
*
2)
;
anima
tion.
delegate
=
self
;
[
self
.shipLayer
addAnimation
:animation
forKey
:
@"rotateAnimation"
];
}
- (
IBAction
)stop
{
[
self
.shipLayer
removeAnimationForKey
:
@"rotateAnimation"
];
}
- (
void
)animationDidStop:(
CAAnimation
*)anim finished:(
BOOL
)flag
{
//log that the animation stopped
NSLog
(
@"The animation stopped (finished: %@)"
, flag?
@"YES"
:
@"NO"
);
}
@end