AVPlayerLayer 动画帧变化

发布于 2024-11-18 13:37:28 字数 184 浏览 2 评论 0原文

每当我更改 AVPlayerLayer 的帧时,视频不会立即调整大小,而是动画到新的大小。

例如:我将帧从 (0, 0, 100, 100) 更改为 (0, 0, 400, 400),视图的帧立即更改,但视频的大小会动画化为新大小。

有人遇到过这个问题吗?如果是的话,有人知道禁用默认动画的方法吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

若能看破又如何 2024-11-25 13:37:28

您可以尝试禁用隐式操作并使用零长度动画:

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];

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];
月下客 2024-11-25 13:37:28

这就是我使用的:

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

我希望这会有所帮助。我不知道它是否是最佳实践,但它对我有用。
似乎每当使用“.frame”或“setFrame”时,它都会向图层添加动画。

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.

尽揽少女心 2024-11-25 13:37:28

处理这个问题最简单、最干净的方法是创建一个 UIView 子类,其层类为 AVPlayerLayer。执行此操作时,AVPlayerLayer 的行为就像常规 UIView 层一样。您可以更改视图的框架而不是图层,并且不会发生隐式动画。

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

您现在可以执行此操作:

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

要将 AVPlayerLayer 与 AVPlayer 关联,只需执行以下操作:

playerLayerView.playerLayer.player = player;

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;
如果没结果 2024-11-25 13:37:28

你用吗?:

- (void)setPlayer:(AVPlayer *)player {
    [(AVPlayerLayer *)[self layer] setPlayer:player];
    [(AVPlayerLayer *)[self layer] setVideoGravity:AVLayerVideoGravityResize];
}

Do you use ?:

- (void)setPlayer:(AVPlayer *)player {
    [(AVPlayerLayer *)[self layer] setPlayer:player];
    [(AVPlayerLayer *)[self layer] setVideoGravity:AVLayerVideoGravityResize];
}
◇流星雨 2024-11-25 13:37:28

以下是我在更改 playerLayerframe 后立即在 Swift 中执行的操作:

playerLayer.removeAllAnimations()

Here is what I do in Swift right after changing the playerLayer's frame:

playerLayer.removeAllAnimations()
英雄似剑 2024-11-25 13:37:28

我通过以下方式在 Swift 4 中实现了此功能:

    var videoLayer = AVPlayerLayer()
    CATransaction.begin()
    CATransaction.setAnimationDuration(0)
    CATransaction.setDisableActions(true)
    var rect = videoLayer.bounds
    rect.size.width /= 3
    rect.size.height /= 3
    videoLayer.bounds = rect
    CATransaction.commit()

I got this working in Swift 4 via the following:

    var videoLayer = AVPlayerLayer()
    CATransaction.begin()
    CATransaction.setAnimationDuration(0)
    CATransaction.setDisableActions(true)
    var rect = videoLayer.bounds
    rect.size.width /= 3
    rect.size.height /= 3
    videoLayer.bounds = rect
    CATransaction.commit()
北恋 2024-11-25 13:37:28

可能是这样,在某些情况下会有帮助:

在保存播放器层的视图中添加自定义“setFrame:”设置器

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];

    self.playerLayer.frame = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height);
}

Probably this, what will help in some cases:

adding custom 'setFrame:' setter in view that holds the player layer

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];

    self.playerLayer.frame = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文