fixed bugs
This commit is contained in:
@@ -13,15 +13,20 @@ struct PlayerSurfaceView: UIViewRepresentable {
|
||||
|
||||
func makeUIView(context: Context) -> VideoSurfaceView {
|
||||
let view = VideoSurfaceView()
|
||||
view.playerLayer.player = controller.player
|
||||
view.controller = controller
|
||||
view.onVerticalSwipe = onVerticalSwipe
|
||||
view.attach(player: controller.player)
|
||||
view.setContentHuggingPriority(.defaultLow, for: .vertical)
|
||||
view.setContentHuggingPriority(.defaultLow, for: .horizontal)
|
||||
view.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
|
||||
view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
|
||||
return view
|
||||
}
|
||||
|
||||
func updateUIView(_ uiView: VideoSurfaceView, context: Context) {
|
||||
uiView.playerLayer.player = controller.player
|
||||
uiView.controller = controller
|
||||
uiView.onVerticalSwipe = onVerticalSwipe
|
||||
uiView.attach(player: controller.player)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,13 +34,9 @@ final class VideoSurfaceView: UIView {
|
||||
weak var controller: PlayerController?
|
||||
var onVerticalSwipe: ((Bool) -> Void)?
|
||||
|
||||
override class var layerClass: AnyClass {
|
||||
AVPlayerLayer.self
|
||||
}
|
||||
private let videoLayer = AVPlayerLayer()
|
||||
|
||||
var playerLayer: AVPlayerLayer {
|
||||
layer as! AVPlayerLayer
|
||||
}
|
||||
var playerLayer: AVPlayerLayer { videoLayer }
|
||||
|
||||
private let minSwipeDistance: CGFloat = 48
|
||||
private let minSwipeVelocity: CGFloat = 160
|
||||
@@ -55,7 +56,11 @@ final class VideoSurfaceView: UIView {
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
backgroundColor = .black
|
||||
playerLayer.videoGravity = .resizeAspect
|
||||
isOpaque = true
|
||||
clipsToBounds = true
|
||||
videoLayer.videoGravity = .resizeAspect
|
||||
videoLayer.backgroundColor = UIColor.black.cgColor
|
||||
layer.addSublayer(videoLayer)
|
||||
|
||||
tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
|
||||
panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
|
||||
@@ -82,6 +87,25 @@ final class VideoSurfaceView: UIView {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
CATransaction.begin()
|
||||
CATransaction.setDisableActions(true)
|
||||
videoLayer.frame = bounds
|
||||
CATransaction.commit()
|
||||
}
|
||||
|
||||
func attach(player: AVPlayer) {
|
||||
if videoLayer.player !== player {
|
||||
videoLayer.player = player
|
||||
}
|
||||
if videoLayer.superlayer !== layer {
|
||||
layer.addSublayer(videoLayer)
|
||||
}
|
||||
videoLayer.frame = bounds
|
||||
videoLayer.isHidden = false
|
||||
}
|
||||
|
||||
@objc private func handleTap() {
|
||||
controller?.handleGesture(.singleTap)
|
||||
}
|
||||
@@ -96,7 +120,6 @@ final class VideoSurfaceView: UIView {
|
||||
case .began:
|
||||
resetSwipeTracking()
|
||||
case .changed:
|
||||
// 在滑动达到阈值时立即派发,并且同一个手势只允许派发一次。
|
||||
evaluateSwipe(dx: dx, dy: dy, velocity: velocity)
|
||||
case .ended, .cancelled, .failed:
|
||||
if !didDispatchSwipe {
|
||||
@@ -112,7 +135,6 @@ final class VideoSurfaceView: UIView {
|
||||
guard !didDispatchSwipe else { return }
|
||||
|
||||
if swipeAxis == nil {
|
||||
// 只有方向足够明确时锁定轴向,避免轻微斜滑被误判为上下滑。
|
||||
if abs(dx) > abs(dy) * 1.2 {
|
||||
swipeAxis = .horizontal
|
||||
} else if abs(dy) > abs(dx) * 1.2 {
|
||||
|
||||
Reference in New Issue
Block a user