【问题标题】:TouchUpInside boundaries outside of UIButtonUIButton 之外的 TouchUpInside 边界
【发布时间】:2014-07-14 21:29:09
【问题描述】:

我目前正在努力熟悉 Swift 下的 UIKit,并找出以编程方式添加 UI 元素的最佳方法。但是,我发现触摸可以在它开始的按钮之外结束,但仍注册为 TouchUpInside 事件。下面的 ViewController 来自单个视图应用程序,可以直接在按钮 17 上开始触摸,在按钮 18 上结束触摸,并且仍然让 buttonAction() 声明“Button tapped: 17”。

知道我在这里缺少什么吗? (编辑:这是在 Xcode 6 beta 3 BTW 下。)

// ViewController.swift

import UIKit

class ViewController: UIViewController {
    let scrollView:UIScrollView = UIScrollView()

    var GWIDTH:Float = 0.0
    var GHEIGHT:Float = 0.0


    override func viewDidLoad() {
        super.viewDidLoad()

        GWIDTH = self.view.bounds.size.width
        GHEIGHT = self.view.bounds.size.height

        scrollView.frame = CGRectMake(10, 10, GWIDTH-10, GHEIGHT-20)
        scrollView.contentSize = CGSize(width:GWIDTH-20, height: 0)
        self.view.addSubview(scrollView)

        for currentTag in 1...30{
            var currentButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
            currentButton.frame = CGRectMake(100, scrollView.contentSize.height, 100, 50)
            currentButton.backgroundColor = UIColor.greenColor()
            currentButton.setTitle("Test Button \(currentTag)", forState: UIControlState.Normal)
            currentButton.tag = currentTag
            currentButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

            scrollView.addSubview(currentButton)
            scrollView.contentSize = CGSize(width:GWIDTH-20,height:2.0+currentButton.frame.size.height+currentButton.frame.origin.y)
        }//next
    }// end viewDidLoad()


    func buttonAction(sender:UIButton!){
        println("Button tapped: \(sender.tag)")
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

【问题讨论】:

    标签: ios uibutton swift


    【解决方案1】:

    好吧,这需要一些挖掘才能深入了解,但有几个有用的 Obj-C 链接...

    UIControlEventTouchDragExit triggers when 100 pixels away from UIButton

    How to correctly subclass UIControl?

    http://www.bytearray.org/?p=5336(尤其是第 89 行)

    ...由于触摸界面,这种行为似乎是标准的(我个人本能地发现默认区域过多,但我确信 Apple 做了功课)并且可以通过子类化 UIControl 或询问来覆盖控制事件的位置。

    我选择了后者,这里有一个专门在 Swift 中的实现:

    // ViewController.swift
    
    import UIKit
    
    class ViewController: UIViewController {
        let buttonCount = 3
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            for currentTag:Int in 1...buttonCount{
                var currentButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
                currentButton.frame = CGRectMake(50, Float(currentTag*50), 120, 50)
                currentButton.backgroundColor = UIColor.greenColor()
                currentButton.setTitle("Test Button \(currentTag)", forState: UIControlState.Normal)
                currentButton.contentEdgeInsets = UIEdgeInsets(top:3,left:6,bottom:3,right:6)
                currentButton.tag = currentTag
                currentButton.addTarget(self,
                    action: "btn_TouchDown:",
                    forControlEvents: UIControlEvents.TouchDown)
                currentButton.addTarget(self,
                    action: "btn_TouchDragExit:",
                    forControlEvents: UIControlEvents.TouchDragExit)
                currentButton.addTarget(self,
                    action: "btn_TouchUpInside:event:",
                    forControlEvents: UIControlEvents.TouchUpInside)
                currentButton.sizeToFit()
    
                self.view.addSubview(currentButton)
            }//next
        }// end viewDidLoad()
    
    
        func btn_TouchDown(sender:UIButton!){
            println("TouchDown event: \(sender.tag)\n")
        }
    
        func btn_TouchDragExit(sender:UIButton!){
            println("TouchDragExit event: \(sender.tag)\n")
        }
    
        func btn_TouchUpInside(sender:UIButton!,event:UIEvent!){
            println("TouchUpInside event: \(sender.tag)")
    
            var currentTouch:CGPoint = event.allTouches().anyObject().locationInView(sender)
            println( "Point: \(currentTouch.x), \(currentTouch.y)\n" )
            if currentTouch.x > sender.frame.width{return}
            if currentTouch.x < 0 {return}
            if currentTouch.y > sender.frame.height{return}
            if currentTouch.y < 0 {return}
    
            println("Event ended within frame!")
        }
    
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
    
        override func prefersStatusBarHidden() -> Bool {
            return true
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      相关资源
      最近更新 更多