gpt4 book ai didi

ios - 新的 UIView 阻止 UITapGesture

转载 作者:行者123 更新时间:2023-11-28 10:13:42 25 4
gpt4 key购买 nike

我的 scrollView 的每一页的背景中都有一个视频,启用了分页。我希望视频在点击可以看到视频的区域时播放/暂停(见左下图,在包含名称和图像标签的 UIView 和底部标签栏之间)(视频 subview 上有另一个 subview 在每页:顶部信息)。用于实现的代码是以下 for 循环(for 循环,因为每个页面都应具有这些属性):

//add subviews (pages)
for (pageIndex, page) in pagesViews.enumerated(){

//...create pages with views

//add Subview with videoplayer frame to individual "pages"
let videoPlayerFrame = CGRect(x: 0, y: 0, width: pageSize.width, height: pageSize.height)
let videoPlayerView = PlayerView(frame: videoPlayerFrame)
page.addSubview(videoPlayerView)

//add Subview with top info frame to individual "pages"
let postInfoFrame = CGRect(x: 0, y: 0, width: pageSize.width, height: 80)
let infoView = PostView(frame: postInfoFrame)
page.addSubview(infoView)

//WORKS FINE UNTIL HERE


//NEW SUBVIEW
//add Subview with bottom info frame to individual "pages"
let postPropertiesFrame = CGRect(x: 0, y: (pageSize.height - 200) / 2, width: pageSize.width, height: 200)
let propertiesView = PostPropsView(frame: postPropertiesFrame)
page.addSubview(propertiesView)

}

如果没有添加新的 subview ,它就是这个样子:WORKS UNTIL HERE

PlayerView 类作为第一个 subview 的基础,当我点击视频区域时视频播放/暂停(阅读评论以首先获得概览(例如微调器不重要)):

lass PlayerView: UIView {

//create spinner view
let activityIndicatorView: UIActivityIndicatorView = {

//define properties
let aiv = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
aiv.translatesAutoresizingMaskIntoConstraints = false

//start spinner
aiv.startAnimating()

return aiv

}()

//create controls container view containing the spinner
let controlsContainerView: UIView = {

//set properties of controls container view
let controlView = UIView()
controlView.backgroundColor = UIColor(white: 0, alpha: 1)

return controlView

}()

override init(frame: CGRect){

super.init(frame: frame)

//function below this override init
setupPlayerView()

//add subview with controls (e.g. spinner)
controlsContainerView.frame = frame
addSubview(controlsContainerView)

//add to subview and center spinner in subview
controlsContainerView.addSubview(activityIndicatorView)
activityIndicatorView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
activityIndicatorView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true

//enable interaction
controlsContainerView.isUserInteractionEnabled = true

//function below this override init
defInteractions()

//backgorund color of player
backgroundColor = .black

}

//define interactions (doupletap and singletap)
func defInteractions (){

//singletap
let singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTapDetected(_:)))
singleTap.numberOfTapsRequired = 1

//controlsContainerView
controlsContainerView.addGestureRecognizer(singleTap)
}

//define type
var player: AVPlayer?

//set playing to false
var isPlaying: Bool = false

//PLAY?PAUSE VIDEO WHEN video tapped
func singleTapDetected(_ sender: UITapGestureRecognizer) {

//play or pause
if(!isPlaying){

//play
player?.play()
isPlaying = true

}
else{

//pause
player?.pause()
isPlaying = false

}

}

//setup video in background
func setupPlayerView() {

//insert url
let urlString = "https://blurtime.com/images/testvideo.mov"

//check URL if can be converted to NSURL
if let videoURL = NSURL(string: urlString){

//player's video
if self.player == nil {
player = AVPlayer(url: videoURL as URL)
}

//add sub-layer
let playerLayer = AVPlayerLayer(player: player)
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.frame

//when are frames actually rendered (when is video loaded)
player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)

//loop through video
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: nil, using: { (_) in
DispatchQueue.main.async {
self.player?.seek(to: kCMTimeZero)
self.player?.play()
}
})

}

}

//what to do when video is loaded (spinner shall disappear)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

//when frames are actually being rendered
if keyPath == "currentItem.loadedTimeRanges" {

activityIndicatorView.stopAnimating()
controlsContainerView.backgroundColor = .clear

}

}

//reuired as suggested by XCode
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

但是,如果我用 PostPropsView 类添加 subview ,它就不再完全起作用了。如果点击黄色区域,它仍然可以正常工作,但不再在红色区域中工作......(右图)为什么它在较低的黄色区域工作,如果点击红色区域,为什么它不再工作?任何人都可以为这个问题提供解决方案(和解释)吗?

最佳答案

新 View 位于具有点击手势识别器的 View 之上,并且正在拦截这些触摸,并且没有针对它们的处理程序。

也许创建一个新的 UIView,其中包含 PostPropsView 和您的视频播放器,并将点击手势识别器放在上面而不是视频上?我不相信您可以将 UIView 直接添加到视频播放器。

关于ios - 新的 UIView 阻止 UITapGesture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44268711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com