Graphics Reference
In-Depth Information
[
super
viewDidLoad
];
//set up images
self
.images =
@[
[
UIImage
imageNamed
:
@"Anchor.png"
],
[
UIImage
imageNamed
:
@"Cone.png"
],
[
UIImage
imageNamed
:
@"Igloo.png"
],
[
UIImage
imageNamed
:
@"Spaceship.png"
]
]
;
}
- (
IBAction
)switchImage
{
//set up crossfade transition
CATransition
*transition = [
CATransition
animation
];
transition.
type
=
kCATransitionFade
;
//apply transition to imageview backing layer
[
self
.imageView.
layer
addAnimation
:transition
forKey
:
nil
];
//cycle to next image
UIImage
*currentImage =
self
.imageView.
image
;
NSUInteger
index = [
self
.images
indexOfObject
:currentImage];
index = (index +
1
) % [
self
.images
count
];
self
.imageView.
image
=
self
.images[index];
}
@end
As you can see from the code, transitions are added to a layer in the same way as property
or group animations, using the
-addAnimation:forKey:
method. Unlike property
animations, though, only one
CATransition
can operate on a given layer at a time. For this
reason, regardless of what value you specify for the key, the transition will actually be
attached with a key of
"transition"
, which is represented by the constant
kCATransition
.