Files
mediaplayer/ios/OralTrainer/Player/PlayerSurfaceView.swift
2026-08-17 20:13:28 +08:00

107 lines
3.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import AVFoundation
import SwiftUI
import UIKit
/// OralTrainerPlayerView
/// - /
/// - / / 退退 /
/// -
struct PlayerSurfaceView: UIViewRepresentable {
let controller: PlayerController
func makeUIView(context: Context) -> VideoSurfaceView {
let view = VideoSurfaceView()
view.playerLayer.player = controller.player
view.controller = controller
return view
}
func updateUIView(_ uiView: VideoSurfaceView, context: Context) {
uiView.playerLayer.player = controller.player
}
}
final class VideoSurfaceView: UIView {
weak var controller: PlayerController?
override class var layerClass: AnyClass {
AVPlayerLayer.self
}
var playerLayer: AVPlayerLayer {
layer as! AVPlayerLayer
}
private let minSwipeDistance: CGFloat = 48
private let minSwipeVelocity: CGFloat = 160
private let longPressTimeout: TimeInterval = 0.5
private var tapRecognizer: UITapGestureRecognizer!
private var panRecognizer: UIPanGestureRecognizer!
private var longPressRecognizer: UILongPressGestureRecognizer!
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .black
playerLayer.videoGravity = .resizeAspect
tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
panRecognizer.minimumNumberOfTouches = 1
panRecognizer.maximumNumberOfTouches = 1
longPressRecognizer = UILongPressGestureRecognizer(
target: self,
action: #selector(handleLongPress)
)
longPressRecognizer.minimumPressDuration = longPressTimeout
tapRecognizer.require(toFail: panRecognizer)
tapRecognizer.require(toFail: longPressRecognizer)
panRecognizer.require(toFail: longPressRecognizer)
addGestureRecognizer(tapRecognizer)
addGestureRecognizer(panRecognizer)
addGestureRecognizer(longPressRecognizer)
isUserInteractionEnabled = true
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc private func handleTap() {
controller?.handleGesture(.singleTap)
}
@objc private func handlePan(_ recognizer: UIPanGestureRecognizer) {
guard recognizer.state == .ended || recognizer.state == .cancelled else {
return
}
let translation = recognizer.translation(in: self)
let velocity = recognizer.velocity(in: self)
let dx = translation.x
let dy = translation.y
guard abs(dx) >= abs(dy),
abs(dx) >= minSwipeDistance,
abs(velocity.x) >= minSwipeVelocity else {
return
}
controller?.handleGesture(dx > 0 ? .swipeRight : .swipeLeft)
}
@objc private func handleLongPress(_ recognizer: UILongPressGestureRecognizer) {
let location = recognizer.location(in: self)
switch recognizer.state {
case .began:
controller?.beginLongPressSpeed(atX: location.x, width: bounds.width)
case .changed:
controller?.updateLongPressSpeed(atX: location.x, width: bounds.width)
case .ended, .cancelled:
controller?.endLongPressSpeed()
default:
break
}
}
}