gpt4 book ai didi

ios - 不要在 subview 中触发 UITapGesture

转载 作者:行者123 更新时间:2023-11-30 12:25:20 25 4
gpt4 key购买 nike

如下图所示,我的视频几乎覆盖了整个屏幕。我希望每当有人点击视频时就播放/停止。代码工作得几乎完美,问题是我调用 defInteractions 函数的类还包含 booked 和 comments subview ,它们也可以可以在下面找到。因此,当有人点击我不想要的这些区域时,视频也会播放/停止。

UITapGestureRecognizer 触发播放/暂停视频的函数:

//set interactions
func defInteractions (){

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

//controlsContainerView
controlsContainerView.addGestureRecognizer(singleTap)
}

//define type
var player: AVPlayer?

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

func singleTapDetected(_ sender: UITapGestureRecognizer) {

//play or pause
if(!isPlaying){

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

}
else{

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

}

}

每个 subview 基本上看起来像这样:

//create controls container view
let comments: UIView = {

//set properties of controls container view
let commentrect = CGRect(x: viewWidth / 2, y: viewHeight - 110, width: viewWidth / 2, height: 50)
let entireCommentView = UILabel(frame: commentrect)

entireCommentView.translatesAutoresizingMaskIntoConstraints = true
entireCommentView.backgroundColor = .white

entireCommentView.font = UIFont(name: "HelveticaNeue", size: 20)

entireCommentView.text = "3 comments"
entireCommentView.textColor = .black
entireCommentView.textAlignment = .center

return entireCommentView

}()

覆盖中,它们被添加为 subview 。我尝试在各个 subview 中将 isUserInteractionEnabled 设置为 false(例如注释:entireCommentView.isUserInteractionEnabled = false),但这不起作用,也不知道如何实现我的目标。有人能帮我吗?我可以从 UITapGestureRecognizer 识别器的目标中排除这些 subview 吗?

[The video shall play/pause when the video (and only the video) is clicked].[1]

编辑(第一个答案的结果):result of first answer

最佳答案

修改 singleTapDetected 以在点击位于任何顶部或底部 UIView 中时返回。

func singleTapDetected(_ sender: UITapGestureRecognizer) {

let view = sender.view
let loc = sender.location(in: controlsContainerView)
if let subview = view?.hitTest(loc, with: nil) {
if subview == entireCommentView || subview == bookedView {
return
}
}

//play or pause
player?.rate != 0 && player?.error == nil ? player?.pause() : player?.play

}

或者,您可以让您的 UIViewController 采用 UIGestureRecognizerDelegate 协议(protocol)并调用以下方法来检查 View 。在此方法中,根据 touch.view 检查您的 View 并返回适当的 bool 值(是/否)。像这样的事情:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return !touch.view?.isDescendant(of: controlsContainerView)
}

关于ios - 不要在 subview 中触发 UITapGesture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44283514/

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