Jevon Cowell
Jevon Cowell

Reputation: 411

How to Make UIAction button Bounce Back when out of Bounds

I have a movable UIAction Button similar to Reddit's Scroll up button and the Assistive Touch Feature. Right now I'm having a hard time finding a way to either: 1. Not go out of bounds in the first place or 2. Bounce Back to the screen similar tot he way that Assistive Touch does.

Here's what I have in my UIPanGesture function:

@IBAction func dragSettingsButton(_ sender: UIPanGestureRecognizer) {

    if sender.state == .began || sender.state == .changed {

        if displayView.frame.contains(sender.location(in: self.view)) {

            let translation = sender.translation(in: displayView)
            // note: 'view' is optional and need to be unwrapped
            sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
            sender.setTranslation(CGPoint.zero, in: displayView)


        }
        else {

            print("WOW BRO YOURE OUT OF BOUNDS")
        }
    }

}

Things to note: I have a ViewController where there are 3 views: A view to act as a custom navigation bar, a view to act as a custom segmented view, and a display view to display different views when the segmented view is switched. There is also a Tab bar on the bottom

Upvotes: 0

Views: 129

Answers (1)

Andrew McKinley
Andrew McKinley

Reputation: 1137

To bounce back:

On sender.state == .end use the same logic to determine if its out of bounds. If so calculate where it should be. Then animate it to that location.

Upvotes: 1

Related Questions