ios - View 已绘制但未检测到手势识别器
<p><p>我有一个 <code>UIScrollView</code>,它有一个 <code>UIView</code> 作为 subview ,作为返回,它包含我所有的 <code>FooView</code>,这是一个 <code>UIView</code> 也是如此。这些 <code>FooView</code> 中的每一个都有一个手势识别器。在我的 ScrollView 中,第一个 <code>FooView</code> 的手势检测得很好(意味着选择器被正确触发),但所有后续 <code>FooView</code> 都不会触发该手势.</p>
<p>这个问题的答案<a href="https://stackoverflow.com/questions/38043832/subview-gesture-recognizer-not-being-called" rel="noreferrer noopener nofollow">Subview Gesture Recognizer not being called</a>我假设解释了我面临的问题。引用:“[...] 问题在于带有手势识别器的 subview 位于父 View 的框架之外。这意味着即使正在绘制 View ,也没有检测到手势”。</p>
<p>我了解手头的问题,但不知道如何解决。</p>
<p>顺便说一句:如果我将 <code>FooView</code> 直接添加到 ScrollView ,而不是将它们添加到 <code>contentView</code>,它会完美运行。但是,对于我的用例,这不是一个选项,因为我使用的是自动布局,否则约束会被搞砸。</p>
<p>附上你会发现我的 MWE 在那里你可以看到点击第一个 View 将打印出“Tapped”,但其他 View 将没有输出。该代码只是一个 Playground 示例,因此可以复制到那里运行。</p>
<pre><code>import UIKit
import PlaygroundSupport
class AwesomeView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.getRandomColor()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapHandler))
self.addGestureRecognizer(tapGesture)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
guard let superview = superview else {
return
}
self.frame = superview.frame
}
func tapHandler(_ sender: AnyObject) {
print("Tapped")
}
}
extension UIColor {
static func getRandomColor(_ hash: UInt32 = arc4random()) -> UIColor{
let randomRed:CGFloat = CGFloat(hash) / CGFloat(UInt32.max)
let randomGreen:CGFloat = CGFloat(hash << 4) / CGFloat(UInt32.max)
let randomBlue:CGFloat = CGFloat(hash << 2) / CGFloat(UInt32.max)
return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
}
}
class DummyVC: UIViewController, UIScrollViewDelegate {
let scrollView = UIScrollView(frame: CGRect(x:0, y:0, width:320,height: 300))
var contentView: UIView!
var frame: CGRect = CGRect(x:0, y:0, width:0, height:0)
override func viewDidLoad() {
super.viewDidLoad()
contentView = UIView(frame: scrollView.frame)
scrollView.addSubview(contentView)
self.view.addSubview(scrollView)
for index in 0..<4 {
frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
frame.size = self.scrollView.frame.size
self.scrollView.isPagingEnabled = true
let subView = AwesomeView(frame: frame)
self.contentView.addSubview(subView)
}
self.scrollView.contentSize = CGSize(width:self.scrollView.frame.size.width * 4, height: self.scrollView.frame.size.height)
}
}
PlaygroundPage.current.liveView = DummyVC().view
PlaygroundPage.current.needsIndefiniteExecution = true
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我认为您需要正确设置 contentView 的大小。现在你将 contentView 的 frame 设置为 scrollView 的 frame 的大小,即总大小的 1/4。注意 <code>viewDidLoad</code> 中的最后一行,我们将 contentView 的 frame 设置为 scrollView 的内容大小。 </p>
<pre><code> class DummyVC: UIViewController, UIScrollViewDelegate {
let scrollView = UIScrollView(frame: CGRect(x:0, y:0, width:320,height: 300))
var contentView: UIView!
var frame: CGRect = CGRect(x:0, y:0, width:0, height:0)
override func viewDidLoad() {
super.viewDidLoad()
var contentFrame = scrollView.frame
contentFrame.size.width *= 4
contentView = UIView(frame: .zero)
scrollView.addSubview(contentView)
self.view.addSubview(scrollView)
for index in 0..<4 {
frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
frame.size = self.scrollView.frame.size
self.scrollView.isPagingEnabled = true
let subView = AwesomeView(frame: frame)
self.contentView.addSubview(subView)
}
self.scrollView.contentSize = CGSize(width:self.scrollView.frame.size.width * 4, height: self.scrollView.frame.size.height)
//new line to set contentView's frame
contentView.frame = CGRect(origin: .zero, size: scrollView.contentSize)
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios -View 已绘制但未检测到手势识别器,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/44101656/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/44101656/
</a>
</p>
页:
[1]