我试图让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")
}
}
Run Code Online (Sandbox Code Playgroud)
而且,在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)
Run Code Online (Sandbox Code Playgroud)
我的方法
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")
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,除默认权限外,没有任何滑动工作正在进行.有任何想法吗?
Ram*_*van 83
第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)
}
Run Code Online (Sandbox Code Playgroud)
第2步:检查handleGesture()方法中的手势检测
func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == .right {
print("Swipe Right")
}
else if gesture.direction == .left {
print("Swipe Left")
}
else if gesture.direction == .up {
print("Swipe Up")
}
else if gesture.direction == .down {
print("Swipe Down")
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这会对某人有所帮助.
小智 15
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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 14
我遇到了同样的问题.实际上,任何刷卡都会使我的代码崩溃(来自Rob Percival,对吧?).所以我下载了他完成的代码,在XCode 8中打开它并让它转换为Swift 3.两点被改变了.
In ViewDidLoad:
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swiped(_:)))
Run Code Online (Sandbox Code Playgroud)
并且在函数内部:
func swiped(_ gesture: UIGestureRecognizer)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
44697 次 |
| 最近记录: |