我有一个UIView并且我添加了点击手势
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
myView.addGesture(tap)
我试图在testfile中以编程方式调用它。
sendActionForEvent
我正在使用这个函数,但它不起作用:
myView.sendActionForEvent(UIEvents.touchUpDown)
它显示未识别的选择器发送到实例。
我该如何解决这个问题呢?
以下是在Swift 5中添加手势视图的最简单方法
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
addGestures()
}
// MARK: Add Gestures to target view
func addGestures()
{
// 1. Single Tap or Touch
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapGetstureDetected))
tapGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(tapGesture)
//2. Double Tap
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(self.doubleTapGestureDetected))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)
//3. Swipe
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeGetstureDetected))
view.addGestureRecognizer(swipeGesture)
//4. Pinch
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.pinchGetstureDetected))
view.addGestureRecognizer(pinchGesture)
//5. Long Press
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressGetstureDetected))
view.addGestureRecognizer(longPressGesture)
//6. Pan
let panGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.panGestureDetected))
view.addGestureRecognizer(panGesture)
}
// MARK: Handle Gesture detection
@objc func swipeGetstureDetected() {
print("Swipe Gesture detected!!")
}
@objc func tapGetstureDetected() {
print("Touch/Tap Gesture detected!!")
}
@objc func pinchGetstureDetected() {
print("Pinch Gesture detected!!")
}
@objc func longPressGetstureDetected() {
print("Long Press Gesture detected!!")
}
@objc func doubleTapGestureDetected() {
print("Double Tap Gesture detected!!")
}
@objc func panGestureDetected()
{
print("Pan Gesture detected!!")
}
//MARK: Shake Gesture
override func becomeFirstResponder() -> Bool {
return true
}
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?){
if motion == .motionShake
{
print("Shake Gesture Detected")
}
}
}
如果你想要Objective - C代码,如下所示,
UITapGestureRecognizer *gesRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; // Declare the Gesture.
gesRecognizer.delegate = self;
[yourView addGestureRecognizer:gesRecognizer]; // Add Gesture to your view.
// Declare the Gesture Recognizer handler method.
- (void)handleTap:(UITapGestureRecognizer *)gestureRecognizer{
NSLog(@"Tapped");
}
或者你想要swift代码如下所示,
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Add tap gesture recognizer to view
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
myView.addGestureRecognizer(tapGesture)
}
// this method is called when a tap is recognized
func handleTap(sender: UITapGestureRecognizer) {
print("tap")
}
}
试试下面的扩展
extension UIView {
func addTapGesture(action : @escaping ()->Void ){
let tap = MyTapGestureRecognizer(target: self , action: #selector(self.handleTap(_:)))
tap.action = action
tap.numberOfTapsRequired = 1
self.addGestureRecognizer(tap)
self.isUserInteractionEnabled = true
}
@objc func handleTap(_ sender: MyTapGestureRecognizer) {
sender.action!()
}
}
class MyTapGestureRecognizer: UITapGestureRecognizer {
var action : (()->Void)? = nil
}
然后使用它:
submitBtn.addTapGesture {
//your code
}
你甚至可以把它用于cell
cell.addTapGesture {
//your code
}
你需要用目标和动作初始化UITapGestureRecognizer,如下所示:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
myView.addGestureRecognizer(tap)
然后,你应该实现处理程序,它将在每次tap事件发生时被调用:
@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
// handling code
}
所以现在调用你的点击手势识别器事件处理程序就像调用一个方法一样简单:
handleTap()