ios - 突出显示时 UIButton 周围不需要的不可见检测区域

标签 ios iphone xcode swift uibutton

我正在 viewDidLoad 的通用设备应用程序中创建"is"和“否”UIButton,如下所示:

   let yesUnpressedTexture = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("yesButtonUnpressed.png"))!
    let yesPressedTexture = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("yesButtonPressed.png"))!
    let buttonWidth = yesUnpressedTexture.size.width
    var yesButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
    yesButton.setImage(yesUnpressedTexture, forState: UIControlState.Normal)
    yesButton.setImage(yesPressedTexture, forState: UIControlState.Highlighted)
    yesButton.frame = CGRect(x: device.x/2 - buttonWidth * worldScale - worldScale * 40, y:  device.y/2 - worldScale * 50, width: buttonWidth * worldScale, height: worldScale * 200)
    yesButton.addTarget(self, action: "deleteProgress:", forControlEvents: UIControlEvents.TouchUpInside)
    self.deleteView.addSubview(yesButton)

    let noUnpressedTexture = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("noButtonUnpressed.png"))!
    let noPressedTexture = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("noButtonPressed.png"))!
    var noButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
    noButton.frame = CGRect(x: device.x/2 + worldScale * 40, y:  device.y/2 - worldScale * 50, width: buttonWidth * worldScale, height: worldScale * 200)
    noButton.setImage(noUnpressedTexture, forState: UIControlState.Normal)
    noButton.setImage(noPressedTexture, forState: UIControlState.Highlighted)
    noButton.addTarget(self, action: "cancelDelete:", forControlEvents: UIControlEvents.TouchUpInside)
    self.deleteView.addSubview(noButton)

它们在我的 iPad 上测试时加载和工作正常,但是当我在任何 iPhone 模拟器上测试时会发生一些奇怪的事情。如果您将手指从任一按钮上移开以取消触摸,则必须将其远离按钮才能执行此操作。相应的按钮会一直突出显示,直到用户手指超出我在照片中显示的突出显示框之外。

enter image description here

有人知道如何解决这个问题吗?

我尝试使用 CGRectMake 设置框架,但没有任何变化。

最佳答案

这实际上是操作系统的一项功能以及触摸事件的工作方式。当您触摸 UIControl(在您的例子中是 UIButton)时,操作系统开始跟踪您的触摸和移动,并且您的控件周围有一个预定义的边界矩形。只有当您将手指移出此边界后,触摸才被认为已退出控件 - 这是当控件失去焦点(突出显示)以及 TouchDragExitTouchDragOutside 时事件被触发。

如果你想改变这个,你可以通过创建一个 UIButton 子类并覆盖 continueTrackingWithTouch(_:withEvent:) 方法来自定义跟踪行为,这样你就可以结束根据您自己的逻辑进行跟踪。

关于ios - 突出显示时 UIButton 周围不需要的不可见检测区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30738169/

相关文章:

ios - 安装企业 ios 应用程序而无需信任设备管理

UIImageView 上的 iOS CAKeyframeAnimation rotationMode

ios - 如何从 UIImagePickerController 拍摄的照片和视频中获取日期?

ios - 使用哪种声音格式? iOS通知

objective-c - armv7s 可执行文件大小增加

xcode - 命令行上的 MacOS 公证无法创建身份验证 session

ios - 我可以将我的应用程序转换为任何语言吗?

iphone - 如何将整数值存储到 iPhone 中的 NSMutableArray

iphone - 如何在不进入 View Controller 的情况下更改选项卡中的图像?

iPhone 开发 : Animating PNG Sequences