0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

左右のスワイプで下から上へのスワイプになってしまう。また、戻る方法を教えてください。

Posted at

View Controller を3つ作り左右のスワイプで画面遷移しようとしましたが、左右にスワイプすると下から上へスワイプします。左右のスワイプにするには、どうしたらいいでしょうか?また、元の画面に戻るにはどうしたらいいでしょうか?
、、、、、、、
import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    
    super.viewDidLoad()
    
    
    //左スワイプ検知
    
    let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipeGesture(_:)))
    
    leftSwipe.direction = .left

    self.view.addGestureRecognizer(leftSwipe)
    
    
    //右スワイプ検知
    
    let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipeGesture(_:)))
    
    rightSwipe.direction = .right
    
    self.view.addGestureRecognizer(rightSwipe)

}


//スワイプ検知時の動作

func swipeGesture(_ sender: UISwipeGestureRecognizer){
    
    
    //左スワイプの動作
    
    if sender.direction == .left {
        
        //遷移先画面の紐付け(※"NextView"はStoryboardにて設定した画面IDを記述)
        
        let Swipe = self.storyboard!.instantiateViewController(withIdentifier: "NextView")
        
        
        //画面遷移処理
        
        self.present( Swipe, animated: true, completion: nil)

// show( Swipe, sender:self)
print("left")
}

    if sender.direction == .right{
        
        //遷移先画面の紐付け(※"PreviousView"はStoryboardにて設定した画面IDを記述)
        
        let Swipe = self.storyboard!.instantiateViewController(withIdentifier: "PreviousView")
        
        //画面遷移処理

// show(Swipe!, sender: self)
self.present( Swipe, animated: true, completion: nil)
print("right")
}
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

}

}

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?