Graphics Reference
In-Depth Information
Figure 6.16
Still-frame from a video playing inside an
AVPlayerLayer
Although we're creating the
AVPlayerLayer
programmatically, we are still adding it to
a container view instead of directly inside the main view for the controller. This is so that
we can use ordinary autolayout constraints to center the layer; otherwise, we would have to
reposition it programmatically every time the device is rotated due to Core Animation not
supporting autoresizing or autolayout (see Chapter 3, “Layer Geometry,” for details).
Of course, because
AVPlayerLayer
is a subclass of
CALayer
, it inherits all of its cool
features. We're not restricted to playing our video in a simple rectangle; with the few extra
lines of code in Listing 6.16, we can rotate our video in 3D and add rounded corners, a
colored border, mask, drop shadow, and so on (see Figure 6.17).
Listing 6.16
Adding a Transform, Border, and Corner Radius to the Video
- (
void
)viewDidLoad
{
...
//set player layer frame and attach it to our view
playerLayer.
frame
=
self
.
containerView
.
bounds
;
[
self
.
containerView
.
layer
addSublayer
:playerLayer];
//transform layer
CATransform3D
transform =
CATransform3DIdentity
;
transform.
m34
= -
1.0
/
500.0
;
transform =
CATransform3DRotate
(transform,
M_PI_4
,
1
,
1
,
0
);
playerLayer.
transform
= transform;