swift教學 - swift中文



在Swift 3中滑動手勢 (5)

Swift 4,Xcode 9.2此代碼將使您能夠在整個ViewController上識別用戶的滑動方向(所有iPhone屏幕,不是特定於按鈕)謝謝@ Ram-madhavan

import UIKit

class ViewController: UIViewController {

    //Label to show test and see Gesture direction. 

    @IBOutlet weak var swipeDirectionLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
      //Defining the Various Swipe directions (left, right, up, down)  
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
        swipeLeft.direction = .left
        self.view.addGestureRecognizer(swipeLeft)

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
        swipeRight.direction = .right
        self.view.addGestureRecognizer(swipeRight)

        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
        swipeUp.direction = .up
        self.view.addGestureRecognizer(swipeUp)

        let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
        swipeDown.direction = .down
        self.view.addGestureRecognizer(swipeDown) 
    }

    //Function to Print to console Swipe Direction, and Change the label to show the directions. The @objc before func is a must, since we are using #selector (above). You can add to the function, in my case, I'll add a sound, so when someone flips the page, it plays a page sound. 

    @objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
        if gesture.direction == UISwipeGestureRecognizerDirection.right {
            print("Swipe Right")
            swipeDirectionLabel.text = "Swiped Right"
        }
        else if gesture.direction == UISwipeGestureRecognizerDirection.left {
            print("Swipe Left")
            swipeDirectionLabel.text = "Swiped Left"

        }
        else if gesture.direction == UISwipeGestureRecognizerDirection.up {
            print("Swipe Up")
            swipeDirectionLabel.text = "Swiped Up"

        }
        else if gesture.direction == UISwipeGestureRecognizerDirection.down {
            print("Swipe Down")
            swipeDirectionLabel.text = "Swiped Down"

        }
    }
}

我試圖讓UISwipeGestureRecognizer在Swift 3中工作,默認滑動右邊工作正常但不向上或向左。

我通過控制拖動Action來嘗試它

@IBAction func hole(_ recognizer: UISwipeGestureRecognizer) {
    if (recognizer.direction == UISwipeGestureRecognizerDirection.left)
    {
        print("left")
    }else if recognizer.direction == .right {
        print("right")
    }else {
        print("other")
    }
}

而且,在ViewDidLoad中

//gesture recognisers
let swipeRight = UISwipeGestureRecognizer(target: self, action: "holeSwiped:")
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)

let swipeLeft = UISwipeGestureRecognizer(target: self, action: "holeSwiped:")
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)

我的方法

func holeSwiped(gesture: UISwipeGestureRecognizer)
{
    if let swipeGesture = gesture as? UISwipeGestureRecognizer{
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            print("right swipe")
        case UISwipeGestureRecognizerDirection.left:
            print("left swipe")
        default:
            print("other swipe")
        }
    }
}

現在,除默認權限外,沒有任何滑動工作正在進行。 有任何想法嗎?


Answer #1

在,Swift 3你可以嘗試這個。

let swipeRightOrange = UISwipeGestureRecognizer(target: self, action:#selector(slideToRightWithGestureRecognizer))
swipeRightOrange.direction = UISwipeGestureRecognizerDirection.Right;

let swipeLeftOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToLeftWithGestureRecognizer))
swipeLeftOrange.direction = UISwipeGestureRecognizerDirection.Left;

@IBAction func  slideToLeftWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.blueColor()
}
@IBAction func slideToRightWithGestureRecognizer
(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.lightGrayColor()
}

Answer #2

我遇到了同樣的問題。 實際上,任何刷卡都會使我的代碼崩潰(來自Rob Percival,對吧?)。 所以我下載了他完成的代碼,在XCode 8中打開它並讓它轉換為Swift 3.兩點被改變了。

In ViewDidLoad:
  let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swiped(_:)))

並且在函數內部:

func swiped(_ gesture: UIGestureRecognizer)

Answer #3

第1步:在viewDidLoad()方法中添加滑動手勢。

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeLeft.direction = .left
    self.view.addGestureRecognizer(swipeLeft)

    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeRight.direction = .right
    self.view.addGestureRecognizer(swipeRight)

    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeUp.direction = .up
    self.view.addGestureRecognizer(swipeUp)

    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeDown.direction = .down
    self.view.addGestureRecognizer(swipeDown)
}

第2步:檢查handleGesture()方法中的手勢檢測

func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
   if gesture.direction == UISwipeGestureRecognizerDirection.right {
        print("Swipe Right")
   }
   else if gesture.direction == UISwipeGestureRecognizerDirection.left {
        print("Swipe Left")
   }
   else if gesture.direction == UISwipeGestureRecognizerDirection.up {
        print("Swipe Up")
   }
   else if gesture.direction == UISwipeGestureRecognizerDirection.down {
        print("Swipe Down")
   }
}

我希望這會對​​某人有所幫助。


Answer #4
    func holeSwiped(gesture: UISwipeGestureRecognizer)
    {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer{
    switch swipeGesture.direction {
    case UISwipeGestureRecognizerDirection.right:
        NSLog("right swipe")
    case UISwipeGestureRecognizerDirection.left:
        NSLog("left swipe")
    default:
        NSLog("other swipe")
    }
   }
  }




swift