Last active
December 4, 2020 11:05
-
-
Save aryasurya21/f3c0e7030fad327d35408b2e4fa7300d to your computer and use it in GitHub Desktop.
Handler Function Implementation to Update View's Location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// draggabledemo | |
// | |
// Created by Philip Indra Prayitno ( https://github.com/s1rent ) on 03/12/20. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var draggableView: UIView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.draggableView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handler))) | |
} | |
@objc func handler(gesture: UIPanGestureRecognizer){ | |
let location = gesture.location(in: self.view) | |
let draggedView = gesture.view | |
draggedView?.center = location | |
if gesture.state == .ended { | |
if self.draggableView.frame.midX >= self.view.layer.frame.width / 2 { | |
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: { | |
self.draggableView.center.x = self.view.layer.frame.width - 40 | |
}, completion: nil) | |
}else{ | |
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: { | |
self.draggableView.center.x = 40 | |
}, completion: nil) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment