Ios AVPlayerLayer为帧更改设置动画
每当我更改AVPlayerLayer的帧时,视频不会立即调整大小,而是设置为新大小的动画 例如:我将帧从(0,0,100,100)更改为(0,0,400,400),视图的帧将立即更改,但视频的大小将设置为新大小的动画 有人遇到过这个问题吗?如果是,有人知道禁用默认动画的方法吗 谢谢 你用什么Ios AVPlayerLayer为帧更改设置动画,ios,video,avfoundation,movie,Ios,Video,Avfoundation,Movie,每当我更改AVPlayerLayer的帧时,视频不会立即调整大小,而是设置为新大小的动画 例如:我将帧从(0,0,100,100)更改为(0,0,400,400),视图的帧将立即更改,但视频的大小将设置为新大小的动画 有人遇到过这个问题吗?如果是,有人知道禁用默认动画的方法吗 谢谢 你用什么 - (void)setPlayer:(AVPlayer *)player { [(AVPlayerLayer *)[self layer] setPlayer:player]; [(AVPl
- (void)setPlayer:(AVPlayer *)player {
[(AVPlayerLayer *)[self layer] setPlayer:player];
[(AVPlayerLayer *)[self layer] setVideoGravity:AVLayerVideoGravityResize];
}
您可以尝试禁用隐式动作并使用零长度动画:
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];
CALayer*videolayer=
[交易开始];
[CATTransaction setAnimationDuration:0];
[CATTransaction setDisableActions:是];
CGRect rect=videolayer.bounds;
rect.size.width/=3;
rect.size.height/=3;
videolayer.bounds=rect;
[CATransaction-commit];
这是我使用的:
AVPlayerLayer * playerLayer = <# AVPlayerLayer #>;
playerLayer.frame = <# CGRect #>;
[playerLayer removeAllAnimations];
AVPlayerLayer*playerLayer=;
playerLayer.frame=;
[玩家层移除警报];
我希望这有帮助。我不知道它是否是最佳实践,但它对我来说是有效的。
似乎每当使用“.frame”或“setFrame”时,它都会将动画添加到层中。也许这在某些情况下会有所帮助: 在视图中添加自定义的“setFrame:”setter以保存播放器层
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
self.playerLayer.frame = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height);
}
处理此问题最简单、最干净的方法是创建一个UIView子类,该子类将AVPlayerLayer作为其layerClass。执行此操作时,AVPlayerLayer的行为将与常规UIView层一样。您可以更改视图的帧而不是层,并且不会发生隐式动画 AVPlayerLayerView.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface AVPlayerLayerView : UIView
@property (nonatomic, readonly) AVPlayerLayer *playerLayer;
@end
您现在可以执行以下操作:
playerLayerView.frame = CGRectMake(0, 0, 400, 400);
要将AVPlayerLayer与AVPlayer关联,只需执行以下操作:
playerLayerView.playerLayer.player = player;
以下是我在更改
playerLayer
的帧之后在Swift中所做的操作:
playerLayer.removeAllAnimations()
我通过以下途径在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()
是的,看起来很好用。干杯其他人注意:这要求QuartzCore.framework使用CATTransaction(在swift中)尝试了这一点,但将playerLayer.removeAllAnimations()放在了错误的位置。你需要在每次更改帧后直接调用它,而不是在创建层时调用。有趣的是,我有一个相反的问题:我不能让动画在AVPlayerLayer上工作!
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()