justanotherguy
justanotherguy

Reputation: 516

Only allow UIPageViewController to scroll when not touching inside UIImageView

In XCode/Swift, Is it possible to have a UIPageViewController that will only scroll horizontally when the touch happens outside of a UIImageView that is contained within the page controller's pages?

So far from searching similar questions, I can only find a way to disable all touch inside the UIPageViewController, but doing that makes the UIImageView untouchable. The code below shows how I'm imagining this could work if at all possible...

@IBOutlet weak var MyImageView: UIImageView!
var location = CGPoint(x: 0, y: 0)

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    var touch : UITouch! = touches.anyObject() as UITouch
    location = touch.locationInView(self.view)

    if /* 1.) detect if touch is inside of MyImageView */ {
        // 2.) Disable Page Controller scroll but still
        // allow any kind of touch actions to take place within MyImageView
    } else {
        // 3.) Enable Page Controller scroll?
    }
}

1.) I could easily come up with a primitive way of handling this. But figure I could learn something from somebody more experienced with Swift

2.) Cannot figure this out...

3.) Cannot figure this out...

Again I'm new to this... Let me know if there's more information I need to provide, and thanks for taking a look at this problem!

Upvotes: 0

Views: 933

Answers (1)

Aggressor
Aggressor

Reputation: 13549

1) You need to check if the view contains the point of the hit.

view.frame.contains(point)

2) What you can do is log the current content offset of the scrollview, and each frame keep setting the content offset to the same value (this lets you enable touch interactions but keeps the content at the same place:

scrollview.contentOffset = frozenContentOffsetValue

3) Just stop assigning the above contentoffset when you want to allow scrolling again.

//Updated based on using a UIPageView controller instead of a UIScrollView

1) Same as above, check if view.frame.contains(point)

2) If your view does contain the point what you do is this:

UIPageViewController.view.userinteractionEnabled = false
UIPageViewController.view.userinteractionEnabled = true

What that does is disable the effects of the current touch but will allow touches again the next time. But its set up so it always cancels the touch when its from your UIView you want to cancel touches from.

Upvotes: 1

Related Questions