Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayerLayer animates frame changes

Whenever I change the frame of my AVPlayerLayer, the video is not resized immediately, but animated to the new size.

For example: I change the frame from (0, 0, 100, 100) to (0, 0, 400, 400), the view's frame is changed immediately, but the video's size is animated to the new size.

Has anyone encountered this issue? And if yes does someone know a way to disable the default animation?

Thanks!

like image 393
Alpár Avatar asked Jul 01 '11 13:07

Alpár


3 Answers

You can try disabling implicit actions and using zero length animations:

CALayer *videolayer = <# AVPlayerLayer #>
[CATransaction begin];
[CATransaction setAnimationDuration:0];
[CATransaction setDisableActions:YES];
CGRect rect = videolayer.bounds;
rect.size.width /= 3;
rect.size.height /= 3;
videolayer.bounds = rect; 
[CATransaction commit];
like image 147
djromero Avatar answered Sep 21 '22 09:09

djromero


This is what I used:

AVPlayerLayer * playerLayer = <# AVPlayerLayer #>;
playerLayer.frame = <# CGRect #>;
[playerLayer removeAllAnimations];

I hope this helps. I don't know if its best practices, but it works for me. It seems that whenever ".frame" or "setFrame" is used, it adds animation to the layer.

like image 25
Atomicflare Avatar answered Sep 19 '22 09:09

Atomicflare


The easiest and cleanest way to deal with this is to create a UIView subclass that has AVPlayerLayer as its layerClass. When doing this the AVPlayerLayer will behave just like a regular UIView layer. You can change the frame of the view instead of the layer and no implicit animations will happen.

AVPlayerLayerView.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface AVPlayerLayerView : UIView

@property (nonatomic, readonly) AVPlayerLayer *playerLayer;

@end

AVPlayerLayerView.m

#import "AVPlayerLayerView.h"

@implementation AVPlayerLayerView

+ (Class)layerClass {
    return [AVPlayerLayer class];
}

- (AVPlayerLayer *)playerLayer {
    return (AVPlayerLayer *)self.layer;
}

@end

You can now do this:

playerLayerView.frame = CGRectMake(0, 0, 400, 400);

To associate the AVPlayerLayer with an AVPlayer simply do this:

playerLayerView.playerLayer.player = player;
like image 31
Anton Holmberg Avatar answered Sep 21 '22 09:09

Anton Holmberg