UISwipeGestureRecognizer Tutorial in Swift 3.0

A Gesture Recognizing is one type of touch behaviour on screen of user. By use of that we can finding which type of event on screen. user can do on screen like single tap , double tap , rotate his/her finger on screen , Drag finger , Pinch with finger , Swipe etc. 

There are Six types of Gesture Recognises :

 UITapGestureRecognizer
 UIPinchGestureRecognizer
 UIRotationGestureRecognizer
 UISwipeGestureRecognizer
 UIPanGestureRecognizer
 UILongPressGestureRecognizer




This is tutorial only for UISwipeGestureRecognizer.

Follow the below steps :

Step 1 :  Create Project with File > New > Project > TapGesture-Swift3.
Step 2 :  Adding a simple View on Storyboard. and also give IBOutlet connection of the view.
@IBOutlet weak var viewSwipe: UIView!
Step 3 :  Make an instance of the UISwipeGestureRecognizer.
var swipeGesture  = UISwipeGestureRecognizer()
Step 4 :  Initialise the UISwipeGestureRecognizer and adding swipe gesture in view. So add the code in ViewDidLoad. here adding four swipe gesture : right, left , up and down.
override func viewDidLoad() {
    super.viewDidLoad()
    let directions: [UISwipeGestureRecognizerDirection] = [.up, .down, .right, .left]
    for direction in directions {
        swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipwView(_:)))
        viewSwipe.addGestureRecognizer(swipeGesture)
        swipeGesture.direction = direction
        viewSwipe.isUserInteractionEnabled = true
        viewSwipe.isMultipleTouchEnabled = true
    }
}
Step 5 :  Method of the SwipeGesture.
func swipwView(_ sender : UISwipeGestureRecognizer){
    UIView.animate(withDuration: 1.0) {
        if sender.direction == .right {
            self.viewSwipe.frame = CGRect(x: self.view.frame.size.width - self.viewSwipe.frame.size.width, y: self.viewSwipe.frame.origin.y , width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
        }else if sender.direction == .left{
            self.viewSwipe.frame = CGRect(x: 0, y: self.viewSwipe.frame.origin.y , width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)

        }else if sender.direction == .up{
            self.viewSwipe.frame = CGRect(x: self.view.frame.size.width - self.viewSwipe.frame.size.width, y: 0 , width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
        }else if sender.direction == .down{
            self.viewSwipe.frame = CGRect(x: self.view.frame.size.width - self.viewSwipe.frame.size.width, y: self.view.frame.size.height - self.viewSwipe.frame.height , width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
        }
        self.viewSwipe.layoutIfNeeded()
        self.viewSwipe.setNeedsDisplay()
    }
}
Step 6 :  Output.


Step 7 :  Download UISwipeGestureRecognizer Demo

Thanks.
UISwipeGestureRecognizer Tutorial in Swift 3.0 UISwipeGestureRecognizer Tutorial in Swift 3.0 Reviewed by Kirit Modi on 20:20:00 Rating: 5

No comments:

Powered by Blogger.