ios 如何识别所有 4 个方向的滑动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24215117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to recognize swipe in all 4 directions
提问by user3739367
I need to use swipe to recognize swipe gesture down and then right. But on swift UISwipeGestureRecognizer has predeterminate Right direction.. And I don't know how make this for use other directions..
我需要使用滑动来识别向下然后向右滑动的手势。但是在 swift UISwipeGestureRecognizer 上有预先确定的正确方向..我不知道如何使它用于其他方向..
回答by Nate Cook
You need to have one UISwipeGestureRecognizer
for each direction. It's a little weird because the UISwipeGestureRecognizer.direction
property is an options-style bit mask, but each recognizer can only handle one direction. You can send them all to the same handler if you want, and sort it out there, or send them to different handlers. Here's one implementation:
UISwipeGestureRecognizer
每个方向都需要一个。这有点奇怪,因为该UISwipeGestureRecognizer.direction
属性是一个选项样式的位掩码,但每个识别器只能处理一个方向。如果需要,您可以将它们全部发送到同一个处理程序,并在那里进行分类,或者将它们发送到不同的处理程序。这是一种实现:
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)
}
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case .right:
print("Swiped right")
case .down:
print("Swiped down")
case .left:
print("Swiped left")
case .up:
print("Swiped up")
default:
break
}
}
}
Swift 3:
斯威夫特 3:
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
self.view.addGestureRecognizer(swipeDown)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped right")
case UISwipeGestureRecognizerDirection.down:
print("Swiped down")
case UISwipeGestureRecognizerDirection.left:
print("Swiped left")
case UISwipeGestureRecognizerDirection.up:
print("Swiped up")
default:
break
}
}
}
回答by Alexandre Cassagne
I just felt like contributing this, looks more elegant in the end:
我只是想贡献这个,最后看起来更优雅:
func addSwipe() {
let directions: [UISwipeGestureRecognizerDirection] = [.Right, .Left, .Up, .Down]
for direction in directions {
let gesture = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipe:"))
gesture.direction = direction
self.addGestureRecognizer(gesture)
}
}
func handleSwipe(sender: UISwipeGestureRecognizer) {
print(sender.direction)
}
回答by user3099333
From the storyboard:
从故事板:
- Add four swipe gesture recognizers to your view.
- Set each one with the target direction from the attribute inspector. You can select right, left, up or down
- One by one, select the swipe gesture recognizer, control + drag to your view controller. Insert the name (let us say leftGesture, rightGesture, upGesture and downGesture), change the connection to: Action and type to: UISwipeGestureRecognizer
- 将四个滑动手势识别器添加到您的视图中。
- 使用属性检查器中的目标方向设置每个。您可以选择向右、向左、向上或向下
- 一一选择滑动手势识别器,控制+拖动到您的视图控制器。插入名称(让我们说 leftGesture、rightGesture、upGesture 和 downGesture),将连接更改为:Action 并键入:UISwipeGestureRecognizer
From your viewController:
从您的视图控制器:
@IBAction func rightGesture(sender: UISwipeGestureRecognizer) {
print("Right")
}
@IBAction func leftGesture(sender: UISwipeGestureRecognizer) {
print("Left")
}
@IBAction func upGesture(sender: UISwipeGestureRecognizer) {
print("Up")
}
@IBAction func downGesture(sender: UISwipeGestureRecognizer) {
print("Down")
}
回答by Bogdan Farca
Looks like things have changed lately. In XCode 7.2 the following approach works:
看来最近情况有变。在 XCode 7.2 中,以下方法有效:
override func viewDidLoad() {
super.viewDidLoad()
let swipeGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
swipeGesture.direction = [.Down, .Up]
self.view.addGestureRecognizer(swipeGesture)
}
func handleSwipe(sender: UISwipeGestureRecognizer) {
print(sender.direction)
}
Tested in Simulator on iOS 8.4 and 9.2 and on actual device on 9.2.
在 iOS 8.4 和 9.2 上的模拟器以及 9.2 上的实际设备上进行了测试。
Or, using mlcollard's handy extension here:
或者,在这里使用mlcollard的方便扩展:
let swipeGesture = UISwipeGestureRecognizer() {
print("Gesture recognized !")
}
swipeGesture.direction = [.Down, .Up]
self.view.addGestureRecognizer(swipeGesture)
回答by John
Apple Swift version 3.1 - Xcode Version 8.3 (8E162)
Apple Swift 3.1 版 - Xcode 8.3 版 (8E162)
The handy way from Alexandre Cassagne's approach
亚历山大·卡萨涅方法的便捷方法
let directions: [UISwipeGestureRecognizerDirection] = [.up, .down, .right, .left]
for direction in directions {
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(YourClassName.handleSwipe(gesture:)))
gesture.direction = direction
self.view?.addGestureRecognizer(gesture)
}
func handleSwipe(gesture: UISwipeGestureRecognizer) {
print(gesture.direction)
switch gesture.direction {
case UISwipeGestureRecognizerDirection.down:
print("down swipe")
case UISwipeGestureRecognizerDirection.up:
print("up swipe")
case UISwipeGestureRecognizerDirection.left:
print("left swipe")
case UISwipeGestureRecognizerDirection.right:
print("right swipe")
default:
print("other swipe")
}
}
回答by iOS
In Swift 4.2 and Xcode 9.4.1
在 Swift 4.2 和 Xcode 9.4.1 中
Add Animation delegate, CAAnimationDelegateto your class
将动画委托CAAnimationDelegate添加到您的类
//Swipe gesture for left and right
let swipeFromRight = UISwipeGestureRecognizer(target: self, action: #selector(didSwipeLeft))
swipeFromRight.direction = UISwipeGestureRecognizerDirection.left
menuTransparentView.addGestureRecognizer(swipeFromRight)
let swipeFromLeft = UISwipeGestureRecognizer(target: self, action: #selector(didSwipeRight))
swipeFromLeft.direction = UISwipeGestureRecognizerDirection.right
menuTransparentView.addGestureRecognizer(swipeFromLeft)
//Swipe gesture selector function
@objc func didSwipeLeft(gesture: UIGestureRecognizer) {
//We can add some animation also
DispatchQueue.main.async(execute: {
let animation = CATransition()
animation.type = kCATransitionReveal
animation.subtype = kCATransitionFromRight
animation.duration = 0.5
animation.delegate = self
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
//Add this animation to your view
self.transparentView.layer.add(animation, forKey: nil)
self.transparentView.removeFromSuperview()//Remove or hide your view if requirement.
})
}
//Swipe gesture selector function
@objc func didSwipeRight(gesture: UIGestureRecognizer) {
// Add animation here
DispatchQueue.main.async(execute: {
let animation = CATransition()
animation.type = kCATransitionReveal
animation.subtype = kCATransitionFromLeft
animation.duration = 0.5
animation.delegate = self
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
//Add this animation to your view
self.transparentView.layer.add(animation, forKey: nil)
self.transparentView.removeFromSuperview()//Remove or hide yourview if requirement.
})
}
If you want to remove gesture from view use this code
如果您想从视图中删除手势,请使用此代码
self.transparentView.removeGestureRecognizer(gesture)
Ex:
前任:
func willMoveFromView(view: UIView) {
if view.gestureRecognizers != nil {
for gesture in view.gestureRecognizers! {
//view.removeGestureRecognizer(gesture)//This will remove all gestures including tap etc...
if let recognizer = gesture as? UISwipeGestureRecognizer {
//view.removeGestureRecognizer(recognizer)//This will remove all swipe gestures
if recognizer.direction == .left {//Especially for left swipe
view.removeGestureRecognizer(recognizer)
}
}
}
}
}
Call this function like
像这样调用这个函数
//Remove swipe gesture
self.willMoveFromView(view: self.transparentView)
Like this you can write remaining directions and please careful whether if you have scroll view or not from bottom to top and vice versa
像这样你可以写下剩余的方向,请注意是否有从下到上的滚动视图,反之亦然
If you have scroll view, you will get conflictfor Top to bottom and view versa gestures.
如果您有滚动视图,则从上到下会发生冲突,而查看相反的手势。
回答by Doci
Swipe gesture to the view you want, or viewcontroller whole view in Swift 5 & XCode 11 based on @Alexandre Cassagne
滑动手势到你想要的视图,或者基于@Alexandre Cassagne在 Swift 5 和 XCode 11 中的视图控制器整个视图
override func viewDidLoad() {
super.viewDidLoad()
addSwipe()
}
func addSwipe() {
let directions: [UISwipeGestureRecognizer.Direction] = [.right, .left, .up, .down]
for direction in directions {
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe))
gesture.direction = direction
self.myView.addGestureRecognizer(gesture)// self.view
}
}
@objc func handleSwipe(sender: UISwipeGestureRecognizer) {
let direction = sender.direction
switch direction {
case .right:
print("Gesture direction: Right")
case .left:
print("Gesture direction: Left")
case .up:
print("Gesture direction: Up")
case .down:
print("Gesture direction: Down")
default:
print("Unrecognized Gesture Direction")
}
}
回答by Imanou Petit
UISwipeGestureRecognizer
has a direction
property that has the following definition:
UISwipeGestureRecognizer
具有direction
具有以下定义的属性:
var direction: UISwipeGestureRecognizerDirection
The permitted direction of the swipe for this gesture recognizer.
此手势识别器允许的滑动方向。
The problem with Swift 3.0.1 (and below) is that even if UISwipeGestureRecognizerDirection
conforms to OptionSet
, the following snippet will compile but won't produce any positive expected result:
Swift 3.0.1(及更低版本)的问题在于,即使UISwipeGestureRecognizerDirection
符合OptionSet
,以下代码段也会编译但不会产生任何积极的预期结果:
// This compiles but does not work
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(gestureHandler))
gesture.direction = [.right, .left, .up, .down]
self.addGestureRecognizer(gesture)
As a workaround, you will have to create a UISwipeGestureRecognizer
for each desired direction
.
作为一种解决方法,您必须UISwipeGestureRecognizer
为每个所需的direction
.
The following Playground code shows how to implement several UISwipeGestureRecognizer
for the same UIView
and the same selector
using Array's map
method:
以下 Playground 代码显示了如何使用 Array 的方法UISwipeGestureRecognizer
为相同UIView
和相同的对象实现多个:selector
map
import UIKit
import PlaygroundSupport
class SwipeableView: UIView {
convenience init() {
self.init(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
backgroundColor = .red
[UISwipeGestureRecognizerDirection.right, .left, .up, .down].map({
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(gestureHandler))
gesture.direction = 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)
}
@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
if gesture.direction == UISwipeGestureRecognizer.Direction.right {
print("Swipe Right")
}
else if gesture.direction == UISwipeGestureRecognizer.Direction.left {
print("Swipe Left")
}
else if gesture.direction == UISwipeGestureRecognizer.Direction.up {
print("Swipe Up")
}
else if gesture.direction == UISwipeGestureRecognizer.Direction.down {
print("Swipe Down")
}
}
self.addGestureRecognizer(gesture)
})
}
func gestureHandler(sender: UISwipeGestureRecognizer) {
switch sender.direction {
case [.left]: frame.origin.x -= 10
case [.right]: frame.origin.x += 10
case [.up]: frame.origin.y -= 10
case [.down]: frame.origin.y += 10
default: break
}
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(SwipeableView())
}
}
let controller = ViewController()
PlaygroundPage.current.liveView = controller
回答by Lijith Vipin
Swipe Gesturein Swift 5
轻扫手势在斯威夫特5
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
}
// Example Tabbar 5 pages
@objc func swiped(_ gesture: UISwipeGestureRecognizer) {
if gesture.direction == .left {
if (self.tabBarController?.selectedIndex)! < 5 {
self.tabBarController?.selectedIndex += 1
}
} else if gesture.direction == .right {
if (self.tabBarController?.selectedIndex)! > 0 {
self.tabBarController?.selectedIndex -= 1
}
}
}
}
回答by ikbal
First create a baseViewController
and add viewDidLoad
this code "swift4":
首先创建一个baseViewController
并添加viewDidLoad
此代码“swift4”:
class YourViewController: BaseViewController {
// its done. Swipe successful
//Now you can use all the Controller you have created without writing any code.
}
And use this baseController
class:
并使用这个baseController
类: