UIControlEvents 我的總結

API:

public struct UIControlEvents : OptionSetType {
    public init(rawValue: UInt)
    
    public static var TouchDown: UIControlEvents { get } // on all touch downs
    public static var TouchDownRepeat: UIControlEvents { get } // on multiple touchdowns (tap count > 1)
    public static var TouchDragInside: UIControlEvents { get }
    public static var TouchDragOutside: UIControlEvents { get }
    public static var TouchDragEnter: UIControlEvents { get }
    public static var TouchDragExit: UIControlEvents { get }
    public static var TouchUpInside: UIControlEvents { get }
    public static var TouchUpOutside: UIControlEvents { get }
    public static var TouchCancel: UIControlEvents { get }
    
    public static var ValueChanged: UIControlEvents { get } // sliders, etc.
    @available(iOS 9.0, *)
    public static var PrimaryActionTriggered: UIControlEvents { get } // semantic action: for buttons, etc.
    
    public static var EditingDidBegin: UIControlEvents { get } // UITextField
    public static var EditingChanged: UIControlEvents { get }
    public static var EditingDidEnd: UIControlEvents { get }
    public static var EditingDidEndOnExit: UIControlEvents { get } // 'return key' ending editing
    
    public static var AllTouchEvents: UIControlEvents { get } // for touch events
    public static var AllEditingEvents: UIControlEvents { get } // for UITextField
    public static var ApplicationReserved: UIControlEvents { get } // range available for application use
    public static var SystemReserved: UIControlEvents { get } // range reserved for internal framework use
    public static var AllEvents: UIControlEvents { get }
}

代碼:

        let btn = UIButton(frame: CGRectMake( 20 , 80 , 100 , 50))
        btn.layer.borderWidth = 1
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 5
        btn.setTitle("btn title", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.view.addSubview(btn)
        
        btn.addTarget(self, action: "TouchDownAction:", forControlEvents: UIControlEvents.TouchDown)
        btn.addTarget(self, action: "TouchDownRepeatAction:", forControlEvents: UIControlEvents.TouchDownRepeat)
        btn.addTarget(self, action: "TouchDragEnterAction:", forControlEvents: UIControlEvents.TouchDragEnter)
        btn.addTarget(self, action: "TouchDragExitAction:", forControlEvents: UIControlEvents.TouchDragExit)
        btn.addTarget(self, action: "TouchDragInsideAction:", forControlEvents: UIControlEvents.TouchDragInside)
        btn.addTarget(self, action: "TouchDragOutsideAction:", forControlEvents: UIControlEvents.TouchDragOutside)
        btn.addTarget(self, action: "TouchUpInsideAction:", forControlEvents: UIControlEvents.TouchUpInside)
        btn.addTarget(self, action: "TouchUpOutsideAction:", forControlEvents: UIControlEvents.TouchUpOutside)
        btn.addTarget(self, action: "TouchCancelAction:", forControlEvents: UIControlEvents.TouchCancel)
        btn.addTarget(self, action: "PrimaryActionTriggeredAction:", forControlEvents: UIControlEvents.PrimaryActionTriggered)

action方法:

    func TouchDownAction(sender:AnyObject?){
        print("TouchDown Action")
    }
    func TouchDownRepeatAction(sender:AnyObject?){
        print("TouchDownRepeat Action")
    }
    func TouchDragEnterAction(sender:AnyObject?){
        print("TouchDragEnter Action")
    }
    func TouchDragExitAction(sender:AnyObject?){
        print("TouchDragExit Action")
    }
    func TouchDragInsideAction(sender:AnyObject?){
        print("TouchDragInside Action")
    }
    func TouchDragOutsideAction(sender:AnyObject?){
        print("TouchDragOutside Action")
    }
    func TouchUpInsideAction(sender:AnyObject?){
        print("TouchUpInside Action")
    }
    func TouchUpOutsideAction(sender:AnyObject?){
        print("TouchUpOutside Action")
    }
    func TouchCancelAction(sender:AnyObject?){
        print("TouchCancel Action")
    }
    func PrimaryActionTriggeredAction(sender:AnyObject?){
        print("PrimaryActionTriggered Action")
    }

我的總結:

   單擊執行順序

TouchDown Action
TouchUpInside Action
PrimaryActionTriggered Action

    連續點擊

TouchDown Action
TouchUpInside Action
PrimaryActionTriggered Action
TouchDown Action
TouchDownRepeat Action
TouchUpInside Action
PrimaryActionTriggered Action
TouchDown Action
TouchDownRepeat Action
TouchUpInside Action
PrimaryActionTriggered Action

    重點

在按鈕內按下執行TouchDown,向外滑動執行TouchDragInside,滑到邊界執行TouchDragExit,滑到邊界外執行TouchDragOutside,再次滑到邊界執行TouchDragEnter,滑到邊界內執行TouchDragInsideapp

在按鈕外鬆開執行TouchUpOutsideide

在按鈕內鬆開執行TouchUpInsidespa

相關文章
相關標籤/搜索