I have three stackviews that I want to add tap gesture, here is my code, but I have to copy a same code again and again, cold you show me a better way to do that?
func addTapGestureToInformationLables(){
let tapLanguage = UITapGestureRecognizer(target: self, action: #selector(self.handleTapLanguage(_:)))
languageStack.addGestureRecognizer(tapLanguage)
let tapFollower = UITapGestureRecognizer(target: self, action: #selector(self.handleFollowers(_:)))
followrsStack.addGestureRecognizer(tapFollower)
let tapFollowing = UITapGestureRecognizer(target: self, action: #selector(self.handleFollowing(_:)))
followingStack.addGestureRecognizer(tapFollowing)
}
#objc func handleTapLanguage(_ sender: UITapGestureRecognizer? = nil) {
performSegue(withIdentifier: "language", sender: nil)
}
#objc func handleFollowers(_ sender: UITapGestureRecognizer? = nil) {
performSegue(withIdentifier: "followers", sender: nil)
}
#objc func handleFollowing(_ sender: UITapGestureRecognizer? = nil) {
performSegue(withIdentifier: "following", sender: nil)
}
Many Thanks
You can use a dictionary to map the UIStackView to the segue identifier and then use a single handleTap() routine to look up the identifier using the view associated with the UITapGestureRecognizer:
var actions = [UIStackView : String]()
func addTapGestureToInformationLables(){
actions = [languageStack: "language", followrsStack: "followers", followingStack: "following"]
for stackview in actions.keys {
let recognizer = UITapGestureRecognizer(target, self, action: #selector(self.handleTap))
stackview.addGestureRecognizer(recognizer)
}
}
#objc func handleTap(_ sender: UITapGestureRecognizer) {
guard let stackview = sender.view as? UIStackView,
let identifier = actions[stackview]
else { return }
performSegue(withIdentifier: identifier, sender: nil)
}
Related
I can not able to show custom MenuController while long Press gesture on tableview cell.
My code is as follow:
override func viewDidLoad() {
super.viewDidLoad()
self.setupLongPressGesture()
}
func setupLongPressGesture() {
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress))
self.tbl.addGestureRecognizer(longPressGesture)
}
#objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){
if gestureRecognizer.state == .began {
let touchPoint = gestureRecognizer.location(in: self.tbl)
if let indexPath = tbl.indexPathForRow(at: touchPoint) {
let cell = tbl.cellForRow(at: indexPath) as! ChattingTextCell
self.view.becomeFirstResponder()
let copy = UIMenuItem(title: "piy", action: #selector(copy_text))
let menucontroller = UIMenuController.shared
menucontroller.menuItems = [copy]
menucontroller.update()
//menucontroller.setTargetRect(cell.frame, in: tbl)
menucontroller.setTargetRect(CGRect(x: 100, y: 200, width: 100, height: 50), in: self.view)
menucontroller.setMenuVisible(true, animated: true)
}
}
}
func canBecomeFirstResponder() -> Bool {
return true
}
func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool
{
print(action)
return true
}
I already refer to many sites but all of them saying the same thing... I can not find any good tutorial for this as well...
Please help me to solve this....
I'm having problems trying to get a UI button to have multiple taps I know it's not ideal but if someone could show me what I'm doing wrong or point me in the right direction that would be great. Below is my current code where I'm trying to create a function called two pressed that responds based off how many times its tapped.
var resultOfLabel = ""
#IBOutlet weak var outputLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture1 = UITapGestureRecognizer(target: self, action: #selector(singleTap(gesture:)))
UIButton.addGestureRecognizer(tapGesture1)
let tapGesture2 = UITapGestureRecognizer(target: self, action: #selector(doubleTap(gesture:)))
tapGesture2.numberOfTapsRequired = 2
UIButton.addGestureRecognizer(tapGesture2)
let tapGesture3 = UITapGestureRecognizer(target: self, action: #selector(tripleTap(gesture:)))
tapGesture3.numberOfTapsRequired = 3
UIButton.addGestureRecognizer(tapGesture3)
let tapGesture4 = UITapGestureRecognizer(target: self, action: #selector(quadrupleTap(gesture:)))
tapGesture4.numberOfTapsRequired = 4
UIButton.addGestureRecognizer(tapGesture4)
// Do any additional setup after loading the view.
}
#IBAction func twoPressed(sender: UIButton) {
#objc func singleTap() {
print(“2”)
}
#objc func doubleTap() {
print(“a”)
}
#objc func tripleTap() {
print(“b”)
}
#objc func quadrupleTap() {
print(“c”)
}
resultOfLabel += "\(sender)"
outputLabel.text = resultOfLabel
}
There is no problem in that except get these 4 functions out of the IBAction method
let tapGesture1 = UITapGestureRecognizer(target: self, action: #selector(self.singleTap(_:)))
UIButton.addGestureRecognizer(tapGesture1)
//
#objc func singleTap(_ sender: UITapGestureRecognizer? = nil) {
print(“2”)
self.twoPressed(sender.View as! UIButton)
}
#objc func doubleTap() {
print(“a”)
}
#objc func tripleTap() {
print(“b”)
}
#objc func quadrupleTap() {
print(“c”)
}
#IBAction func twoPressed(sender: UIButton) {
resultOfLabel += "\(sender)"
outputLabel.text = resultOfLabel
}
I have implement UITapGestureRecognizer and UILongPressGestureRecognizer on button click. My problem is when i tapped first time on button its not working but second time when i press the button its start working.
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
longGesture.minimumPressDuration = 0.5
button.addGestureRecognizer(longGesture)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))
tapGesture.delegate = self
tapGesture.numberOfTouchesRequired = 1
tapGesture.numberOfTapsRequired = 1
button.addGestureRecognizer(tapGesture)
#objc func longTap(_ sender: UIGestureRecognizer){
print("long tap")
}
#objc func normalTap(_ sender: UIGestureRecognizer){
print("Normal tap")
}
Try this
class ViewController: UIViewController, UIGestureRecognizerDelegate {
#IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
longGesture.minimumPressDuration = 0.5
button.addGestureRecognizer(longGesture)
}
#IBAction func buttonClicked(_ sender: Any) {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))
tapGesture.delegate = self
tapGesture.numberOfTouchesRequired = 1
tapGesture.numberOfTapsRequired = 1
button.addGestureRecognizer(tapGesture)
}
#objc func longTap(_ sender: UIGestureRecognizer){
print("long tap")
}
#objc func normalTap(_ sender: UIGestureRecognizer){
print("Normal tap")
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "editinfo"{
let selecteditem: NSManagedObject = items[self.tableView.indexPathForSelectedRow!.row] as! NSManagedObject
let viewcon: addViewController = segue.destinationViewController as! addViewController
viewcon.usernameupdate = selecteditem.valueForKey("username") as! String
viewcon.emailupdate = selecteditem.valueForKey("email") as! String
viewcon.existeditem = selecteditem
}
i tried the following but always the segue trigered on single tap
let doubleTap = UITapGestureRecognizer(target: self, action: nil)
doubleTap.numberOfTapsRequired = 2
doubleTap.numberOfTouchesRequired = 1
cell.addGestureRecognizer(doubleTap)
Check my code to double tap, its very simple.
You can put in a sample project, for debug, maybe something else is blocking your code to work properly.
override func viewDidLoad() {
super.viewDidLoad()
let doubleTap = UITapGestureRecognizer(target: self, action: "doubleTapHandler")
doubleTap.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTap)
}
func doubleTapHandler() {
print("double tap")
}
I want to wire an action such that if the gesture is a tap, it does animates an object in a particular way but if the press duration was more than .5 secs it does something else.
Right now, I just have the animation hooked up. I don't know how I can differentiate between a long press and a tap?
How do I access the press duration to achieve the above?
#IBAction func tapOrHold(sender: AnyObject) {
UIView.animateKeyframesWithDuration(duration, delay: delay, options: options, animations: {
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {
self.polyRotate.transform = CGAffineTransformMakeRotation(1/3 * CGFloat(M_PI * 2))
})
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {
self.polyRotate.transform = CGAffineTransformMakeRotation(2/3 * CGFloat(M_PI * 2))
})
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {
self.polyRotate.transform = CGAffineTransformMakeRotation(3/3 * CGFloat(M_PI * 2))
})
}, completion: { (Bool) in
let vc : AnyObject! = self.storyboard?.instantiateViewControllerWithIdentifier("NextView")
self.showViewController(vc as UIViewController, sender: vc)
})
Define two IBActions and set one Gesture Recognizer to each of them. This way you can perform two different actions for each gesture.
You can set each Gesture Recognizer to different IBActions in the interface builder.
#IBAction func tapped(sender: UITapGestureRecognizer)
{
println("tapped")
//Your animation code.
}
#IBAction func longPressed(sender: UILongPressGestureRecognizer)
{
println("longpressed")
//Different code
}
Through code without interface builder
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
self.view.addGestureRecognizer(tapGestureRecognizer)
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
self.view.addGestureRecognizer(longPressRecognizer)
func tapped(sender: UITapGestureRecognizer)
{
println("tapped")
}
func longPressed(sender: UILongPressGestureRecognizer)
{
println("longpressed")
}
Swift 5
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped))
self.view.addGestureRecognizer(tapGestureRecognizer)
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed))
self.view.addGestureRecognizer(longPressRecognizer)
#objc func tapped(sender: UITapGestureRecognizer){
print("tapped")
}
#objc func longPressed(sender: UILongPressGestureRecognizer) {
print("longpressed")
}
For swift2
let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
self.featuredCouponColView.addGestureRecognizer(lpgr)
Action
//MARK: - UILongPressGestureRecognizer Action -
func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state != UIGestureRecognizerState.Ended {
//When lognpress is start or running
}
else {
//When lognpress is finish
}
}
For Swift 4.2/ Swift 5
let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
self.colVw.addGestureRecognizer(lpgr)
//MARK: - UILongPressGestureRecognizer Action -
#objc func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state != UIGestureRecognizer.State.ended {
//When lognpress is start or running
}
else {
//When lognpress is finish
}
}
Through code without interface builder
// Global variables declaration
var longPressed = false
var selectedRow = 0
override func viewDidLoad() {
super.viewDidLoad()
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ContactListTableViewController.handleLongPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.allowableMovement = 15 // 15 points
longPressGesture.delegate = self
self.tableView.addGestureRecognizer(longPressGesture)
}
// Long tap work goes here !!
if (longPressed == true) {
if(tableView.cellForRowAtIndexPath(indexPath)?.accessoryType == .Checkmark){
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .None
self.selectedRow -= 1
if(self.selectedRow == 0){
self.longPressed = false
}
} else {
self.selectedRow += 1
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark
}
} else if(self.selectedRow == 0) {
// Single tape work goes here !!
}
But the only problem is the long press gesture runs two times. If you have found any solution do comment below !
Swift 5 using interface builder
for the normal tap you can simply create a touch up inside action from your button.
for the long press, create an outlet for your button, create the tap gesture recognizer and set it to the button then create the selector method to perform the long press tasks.
#IBOutlet var myButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(addToListButtonLongPress(_:)))
longPressRecognizer.numberOfTouchesRequired = 1
longPressRecognizer.allowableMovement = 10
longPressRecognizer.minimumPressDuration = 0.5
myButton.addGestureRecognizer(longPressRecognizer)
}
// Connected to myButton in interface builder.
#IBAction func myButtonTapped(_ sender: UIButton) {
print("button tapped")
}
#objc func myButtonLongPressed(_ sender: UILongPressGestureRecognizer) {
print("button long pressed")
}