user4806509 user4806509 - 1 year ago 138
iOS Question

Touch Drag Inside a UIButton after 5 pixels instead of default 1 pixel

I have a single

on a storyboard. The
is connected to a
Touch Drag Inside
. The action is triggered when a user drags the button from inside (

The problem is that the action is triggered after 1 pixel of inside dragging. However 1 pixel is too sensitive and can be triggered with just the slightest finger movement.

@IBAction func myTouchDragInsideAction(sender: UIButton) {

print("Button dragged inside")



How can I extend this action to trigger the inside dragging action only after at least 5 pixels of movement?

Answer Source

You have to create custom button for it. Following CustomButton may help you.

let DelayPoint:CGFloat = 5

class CustomButton: UIButton {

    var startPoint:CGPoint?

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

        if self.startPoint == nil {
          self.startPoint = touches.first?.previousLocationInView(self)

        if self.shouldAllowForSendActionForPoint((touches.first?.locationInView(self))!) {
            super.touchesMoved(touches, withEvent: event)

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.startPoint = nil
        super.touchesEnded(touches, withEvent: event)

    func shouldAllowForSendActionForPoint(location:CGPoint) -> Bool {

        if self.startPoint != nil {

            let xDiff = (self.startPoint?.x)! - location.x
            let yDiff = (self.startPoint?.y)! - location.y

            if (xDiff > DelayPoint || xDiff < -DelayPoint || yDiff > DelayPoint || yDiff < -DelayPoint) {

                return true
        return false

You change the "DelayPoint" as per your req. Hope this will help you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download