简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM