我有一个UIView并且我添加了点击手势
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
myView.addGesture(tap)
我试图在testfile中以编程方式调用它。
sendActionForEvent
我正在使用这个函数,但它不起作用:
myView.sendActionForEvent(UIEvents.touchUpDown)
它显示未识别的选择器发送到实例。
我该如何解决这个问题呢?
对于Swift 4:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
view.addGestureRecognizer(tap)
view.isUserInteractionEnabled = true
self.view.addSubview(view)
// function which is triggered when handleTap is called
@objc func handleTap(_ sender: UITapGestureRecognizer) {
print("Hello World")
}
在Swift 4中,你需要显式地指出触发的函数是可从Objective-C调用的,所以你需要添加@objc你的handleTap函数。
请看@Ali Beadle的回答:Swift 4添加手势:覆盖vs @objc
内部ViewDidLoad
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
self.imgMainAdView.isUserInteractionEnabled = true
self.imgMainAdView.addGestureRecognizer(tapGestureRecognizer)
//MARK: - Image Tap Method -
@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
print("Tapped")
if let url = URL(string: self.strMAinAdvLink)
{
UIApplication.shared.open(url, options: [:])
}
}
打电话的目的
@IBAction func btnCall1Action(_ sender: Any)
{
let text = self.strPhoneNumber1!
let test = String(text.filter { !" -()".contains($0) })
UIApplication.shared.openURL(NSURL(string: "tel://\(test)")! as URL)
}
邮件的目的
MFMailComposeViewControllerDelegate
@IBAction func btnMailAction(_ sender: Any)
{
let strEmail = SAFESTRING(str: (self.dictEventDetails?.value(forKeyPath: "Email.value_text.email") as! String))
if !MFMailComposeViewController.canSendMail()
{
AppDelegate.sharedInstance().showAlertAction(strTitle: "OK", strMessage: "Mail services are not available") { (success) in
}
return
}
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
composeVC.setToRecipients([strEmail])
composeVC.setSubject("")
composeVC.setMessageBody("", isHTML: false)
self.present(composeVC, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
{
controller.dismiss(animated: true, completion: nil)
}
以下是在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")
}
}
}
对于Swift 4:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
view.addGestureRecognizer(tap)
view.isUserInteractionEnabled = true
self.view.addSubview(view)
// function which is triggered when handleTap is called
@objc func handleTap(_ sender: UITapGestureRecognizer) {
print("Hello World")
}
在Swift 4中,你需要显式地指出触发的函数是可从Objective-C调用的,所以你需要添加@objc你的handleTap函数。
请看@Ali Beadle的回答:Swift 4添加手势:覆盖vs @objc