David
David

Reputation: 7525

iOS Swift: UIPanGestureRecognizer in UIScrollView

I have a UIScrollView that has 3 pages (which all contain a separate UIContainerView and onload starts at the second page (so that you can swipe left or right)

I have added a UIPanGestureRecogniser to the second page (in the actual containerView controller) as I want to be able to track when the user swipe up or down on this page.

I have got this working but by doing so it disables the scrollView from scrolling

Heres my code:

let panRec = UIPanGestureRecognizer()

@IBOutlet var containerView: UIView!      // (second page container view)

override func viewDidLoad() {

    super.viewDidLoad()

    containerView.addGestureRecognizer(panRec)

    panRec.addTarget(self, action: "draggedView:")

}

func draggedView(sender:UIPanGestureRecognizer){

    var translation = sender.translationInView(self.view)


    if(translation.y > 100)
    {
        NSNotificationCenter.defaultCenter().postNotificationName("showExtras", object:nil)
    }

    if(translation.y < -100)
    {
        NSNotificationCenter.defaultCenter().postNotificationName("hideExtras", object:nil)
    }



}

Hoping someone has a way I can scroll the scroll view but also use the pan gesture for vertical swiping?

Any help would be much appreciated!

Thanks, Dave

Upvotes: 3

Views: 1980

Answers (1)

jrturton
jrturton

Reputation: 119242

You need to give the pan recogniser a delegate, then return true for shouldRecogniseSimultaneouslyWith....

You may also need to do the same with the scroll view's pan recogniser, which is available as a property.

Alternatively, add another target/action to the scroll view's pan recogniser (using addTarget(_, action:) instead of creating your own.

Upvotes: 3

Related Questions