iosswiftuicontrolevents

Swipe gesture mistaken for touch down


I have a UIScrollView with three sections that can be swiped to. On each section, there is a UIButton, and the user is supposed to select one of buttons. My problem is that if the user swipes through the UIButton, it is accidentally selected. My question is, what UIControlEvent should I set my UIButtons to so they don't mistake a swipe for a touch down?

here is the code for my button:

    var button1 = UIButton(frame: CGRectMake((bigSizes.width * (50/960)), (bigSizes.height * 20/568), (bigSizes.width * 220/960), (bigSizes.height * 300/568)))
    button1.setImage(image1, forState: UIControlState.Normal)
    view1.addSubview(button1)
    button1.addTarget(self, action: "button1Pressed", forControlEvents: UIControlEvents.TouchUpInside)

Solution

  • You can bypass this button problem if you instead use a UIImageView with a tap gesture recognizer:

    var button1 = UIImageView(frame: CGRectMake((bigSizes.width * (50/960)), (bigSizes.height * 20/568), (bigSizes.width * 220/960), (bigSizes.height * 300/568)))
    button1.image = image1
    button1.userInteractionEnabled = true
    view1.addSubview(button1)
    let tap = UITapGestureRecognizer(target: self, action: "handleTap:")
    button.addGestureRecognizer(tap)
    

    With, obviously a handleTap: method:

    func handleTap(gesture: UITapGestureRecognizer) {
        // handle tap
    }