UIbutton 只在 longPress 上看起来被点击(突出显示)? [英] UIbutton only looks clicked (highlighted) on longPress?

查看:27
本文介绍了UIbutton 只在 longPress 上看起来被点击(突出显示)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个有点奇怪的问题.我在放置在常规 ViewController 中的 tableView 中的迭代 tableViewCell 中有一个 UIButton.出于某种原因,它看起来只是在长按时被点击(从默认的蓝色突出显示为浅蓝色,然后再回到蓝色).它会在常规点击时执行我分配给它的任何操作(只是从一个普通的旧打印语句开始),但只是没有看起来被点击.有谁知道如何解决这个问题?正常状态下的按钮:

This seems to be a bit of a weird issue. I have a UIButton in an iterated tableViewCell in a tableView thats been placed in a regular ViewController. For some reason, it only looks clicked (highlights from its default blue color to the lighter blue briefly and then back to the blue) on long press. It does whatever action I assign it (just started with a normal old print statement) on the regular click, but just doesn't look clicked. Anybody know how to fix this? Button in its normal state:

点击后的按钮:

长按后的按钮(抱歉,不得不用手机拍照)

Button after being long pressed (sorry, had to take a picture with phone for this)

这与iOS7问题中的答案不同,因为我正在寻找iOS 10 swift解决方案,而不是objective-c.也不是iOS7

This is different from the answers in the iOS7 question because I am looking for an iOS 10 swift solution, not objective-c. Nor iOS7

推荐答案

这是正常行为.这是由于 UIScrollView 的 delaysContentTouches 属性.根据 Apple 的 docs:

This is normal behavior. It's due to UIScrollView's delaysContentTouches property. Per Apple's docs:

如果这个属性的值为YES,scroll view会延迟处理触摸手势,直到它可以确定滚动是否是意图.如果值为 NO ,则滚动视图立即调用touchesShouldBegin:withEvent:inContentView:.

If the value of this property is YES, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value is NO , the scroll view immediately calls touchesShouldBegin:withEvent:inContentView:.

这个答案给出了以下解决方案.我已经测试过了,它确实有效.然而,由于它在施展魔法,我不建议你真的这样做,因为当 Apple 决定在幕后改变他们的视图层次结构时,它不可避免地会中断.

This answer gives the the below solution. I've tested it and it does work. However, as it's doing casting magic, I wouldn't recommend you actually do this because it will inevitably break when Apple decides to change their view hierarchy behind-the-scenes.

tableView.delaysContentTouches = false
for case let x as UIScrollView in tableView.subviews {
    x.delaysContentTouches = false
}

旧答案:这个答案 给出了一个解决方案,但它确实是一个黑客和可能会导致您的应用由于使用私有类而被 Apple 拒绝.由于该行为确实是一项功能,因此我建议您保持原样.

OLD ANSWER: This answer gives a solution, but it's really a hack and might cause your app to get rejected by Apple due to its use of private classes. Since the behavior is really a feature, I'd recommend you to leave it as it is.

这篇关于UIbutton 只在 longPress 上看起来被点击(突出显示)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆