问题描述
我正在以编程方式将 UIImageView
添加到 UIScrollView
中,并在imageView上添加点按手势。它恰好4次识别轻敲事件,第四次崩溃时没有任何明确的错误消息。这是在控制器名为 FeaturedListingDetailVC
I am adding UIImageView
into UIScrollView
programmatically and adding a tap gesture on imageView. It recognizes the tap event exactly 4 times and on fourth time it crashes without any clear error message. This is in the controller named FeaturedListingDetailVC
代码:
let frame = CGRect(x: 0, y: 0, width: 128, height: 128)
_iv = UIImageView(frame: frame)
_iv.image = UIImage(named: "no_media")
_iv.userInteractionEnabled = true
tapGesture = UITapGestureRecognizer(target: self, action: #selector(initImagePopup(_:)))
_iv.addGestureRecognizer(tapGesture)
_iv.widthAnchor.constraintEqualToConstant(CGFloat.init(128)).active = true
_iv.heightAnchor.constraintEqualToConstant(CGFloat.init(128)).active = true
hrScroll.addSubview(_iv)
在点击时调用的功能:
@objc func initImagePopup(gesture: UITapGestureRecognizer) {
print("I am tapped!!!")
}
此外,我的方式加载我的目标viewController (即FeaturedListingDetailVC)真的很重要,但我不知道为什么以及如何。因为当我将目标viewController推入 UINavigationViewController
时,它会在第一次点击时崩溃,否则当我通过演示文稿viewController初始化目标VC时,它会在第4次点击时崩溃。
Moreover, the way I load my target viewController (i.e. FeaturedListingDetailVC) really matters but I don't know why and how. Because when I push my target viewController into UINavigationViewController
, it crashes on the first tap otherwise when I initialize target VC by presentation viewController it crashes on the 4th tap.
使用以下代码初始化后首次崩溃时崩溃:
let storyboard = UIStoryboard(name: "Post", bundle: nil)
let featuredVC = storyboard.instantiateViewControllerWithIdentifier("FeaturedListingDetailVC") as! FeaturedListingDetailVC
self.vc?.navigationController!.pushViewController(featuredVC, animated: true)
使用以下代码初始化后第4次崩溃时崩溃:
let storyboard = UIStoryboard(name: "Post", bundle: nil)
let featuredVC = storyboard.instantiateViewControllerWithIdentifier("FeaturedListingDetailVC") as! FeaturedListingDetailVC
self.vc?.navigationController!.presentViewController(featuredVC, animated: true, completion: nil)
Backtrace:
* thread #1: tid = 0x78c3e, 0x0000000106365553 UIKit\`-[UIViewController(UIKitManual) release] + 122, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
frame #0: 0x0000000106365553 UIKit\`-[UIViewController(UIKitManual) release] + 122
frame #1: 0xfffffffee7368160
frame #2: 0x0000000105bf72f3 UIKit\`-[UIViewController setChildModalViewController:] + 248
frame #3: 0x0000000105be853e UIKit\`-[UIViewController dealloc] + 1329
frame #4: 0x0000000105f48b31 UIKit\`_UIGestureRecognizerSendTargetActions + 162
frame #5: 0x0000000105f4519a UIKit\`_UIGestureRecognizerSendActions + 162
frame #6: 0x0000000105f43197 UIKit\`-[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843
frame #7: 0x0000000105f4b655 UIKit\`___UIGestureRecognizerUpdate_block_invoke898 + 79
frame #8: 0x0000000105f4b4f3 UIKit\`_UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
frame #9: 0x0000000105f38e75 UIKit\`_UIGestureRecognizerUpdate + 2634
frame #10: 0x0000000105ac548e UIKit\`-[UIWindow _sendGesturesForEvent:] + 1137
frame #11: 0x0000000105ac66c4 UIKit\`-[UIWindow sendEvent:] + 849
frame #12: 0x0000000105a71dc6 UIKit\`-[UIApplication sendEvent:] + 263
frame #13: 0x0000000105a4b553 UIKit\`_UIApplicationHandleEventQueue + 6660
frame #14: 0x00000001046f2301 CoreFoundation\`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #15: 0x00000001046e822c CoreFoundation\`__CFRunLoopDoSources0 + 556
frame #16: 0x00000001046e76e3 CoreFoundation\`__CFRunLoopRun + 867
frame #17: 0x00000001046e70f8 CoreFoundation\`CFRunLoopRunSpecific + 488
frame #18: 0x000000010bf6ead2 GraphicsServices\`GSEventRunModal + 161
frame #19: 0x0000000105a50f09 UIKit\`UIApplicationMain + 171
* frame #20: 0x0000000103e34b22 JaClassified\`main + 114 at AppDelegate.swift:5
frame #21: 0x000000010886b92d libdyld.dylib\`start + 1
frame #22: 0x000000010886b92d libdyld.dylib\`start + 1
推荐答案
听起来很奇怪,我测试了一个很多你的代码,没有办法,总是崩溃。
Sounding strange, I've tested a lot your code, and there is no way, always crashes.
所以我只是尝试更改回调方法的名称:
So I simply tried changing name of callback method:
func didTapOnImagePopup(recognizer: UITapGestureRecognizer) {}
它有效。
我真的不知道为什么,我可以找到任何合理的解释。
I really don't know why and I can find any reasonable explanation.
只有我能想到的是应该存在一个内部的f unction叫做initImagePopup..但是很奇怪。
The only thing that I can think is that should exists an internal function called "initImagePopup".. but it's strange.
如果有人知道,我真的很好奇真正的解释。
I'm really curious on the real explanation, if someone know it.
这篇关于UITapGestureRecognizer在多次点击时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!