25

Swipe Gesture iOS Tutorial

 5 years ago
source link: https://www.tuicool.com/articles/hit/fiq6VnV
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The iOS SDK can detect a number of gestures. In this tutorial we show how to detect the left and right swipe gesture. We will create a label which moves to the side of the swipe. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

IbqyieY.png!web

For product name, use IOSSwipeGestureTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

RjM36zi.png!web

Go to the Storyboard and drag a Label to the main view. Set the title of the Label to "Swipe"  . Select the Resolve Auto Layout Issues button and select Reset to Suggested Constraints.

BFZ7R3r.png!web

The Storyboard should look like this.

ueyime6.png!web

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Labe to the ViewController class and create the following Outlet

nQVjemj.png!web

Change the viewDidLoad method to

override func viewDidLoad() {
    super.viewDidLoad()
        
    let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
    let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
        
    leftSwipe.direction = .left
    rightSwipe.direction = .right

    view.addGestureRecognizer(leftSwipe)
    view.addGestureRecognizer(rightSwipe)
}

The Swipe Gesture Recognisers are defined and added to the view. When a gesture is detected the handleSwipes(_:) method is called, implement this method.

@objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {
        
    if (sender.direction == .left) {
            print("Swipe Left")
        let labelPosition = CGPoint(x: swipeLabel.frame.origin.x - 50.0, y: swipeLabel.frame.origin.y)
        swipeLabel.frame = CGRect(x: labelPosition.x, y: labelPosition.y, width: swipeLabel.frame.size.width, height: swipeLabel.frame.size.height)
    }
        
    if (sender.direction == .right) {
        print("Swipe Right")
        let labelPosition = CGPoint(x: self.swipeLabel.frame.origin.x + 50.0, y: self.swipeLabel.frame.origin.y)
        swipeLabel.frame = CGRect(x: labelPosition.x, y: labelPosition.y, width: self.swipeLabel.frame.size.width, height: self.swipeLabel.frame.size.height)
    }
}

The direction of the swipe is checked, when a left swipe is detected the label moves left 50 points and with a right swipe it moves 50 points to the right. Build and Run the project and perform some swipes to move the label.

vEfY7fi.png!web

You can download the source code of the IOSSwipeGestureTutorial at the ioscreator repository on Github .


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK