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")
}
Related
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)
}
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 am trying long press Gesture in Swift for Copy Option.
But it is not working. It is not identifying the Gesture in UiView Or UILabel either.
Below is my Code
In View DidLoad
let copyLongPress = UILongPressGestureRecognizer(target: self, action: #selector(ContactDetailController.handleLongPress(_:)))
copyLongPress.numberOfTouchesRequired = 0
copyLongPress.delegate = self
copyLongPress.minimumPressDuration=0.5
self.lblDynaMobile.addGestureRecognizer(copyLongPress)
self.lblDynaMobile.userInteractionEnabled = true
self.lblDynaDDI.addGestureRecognizer(copyLongPress)
self.lblDynaDDI.userInteractionEnabled = true
self.GeneralView.addGestureRecognizer(copyLongPress)
self.EmailView.addGestureRecognizer(copyLongPress)
self.AddressView.addGestureRecognizer(copyLongPress)
New Mothod
func handleLongPress(longPressView :UILongPressGestureRecognizer) {
let lblFont:UILabel = (longPressView.view as? UILabel)!
UIPasteboard.generalPasteboard().string = lblFont.text
}
I have added UIGestureRecognizerDelegate too in the Declaration of class
Try this, and see (it's working.)
// in viewDidLoad()
let copyLongPress = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress(_:)))
self.lblDynaMobile.addGestureRecognizer(copyLongPress)
func handleLongPress(_ gesture: UILongPressGestureRecognizer) {
if let lblFont = gesture.view as? UILabel {
//UIPasteboard.generalPasteboard().string = lblFont.text
}
}
i tried to add gesture recognizer in my UIImageView
let rc = UITapGestureRecognizer(target: self, action: "foo:")
rc.numberOfTapsRequired = 1
rc.numberOfTouchesRequired = 1
cell.bar.tag = indexPath.row
cell.bar.addGestureRecognizer(rc)
but didn't call my foo function
func foo(sender: UIImageView! ) {
self.performSegueWithIdentifier("VC", sender: sender)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "VC" {
let vc = segue.destinationViewController as! VC
vc.item = items[sender.tag]
}
}
So, as I mentioned before your problem is UIImageView by default has property userInteractionEnabled set to false. You can change this in you storyboard, or add line cell.bar.userInteractionEnabled = true.
Next your problem is in your foo: method implementation: you specify sender as UIImageView!, but it should be UITapGestureRecognizer. This is why it crashes - it cannot be UIImageView, so when it unwraps (!) it is nil.
Solution: change your foo method declaration to foo(recognizer: UITapGestureRecognizer). If you need access your imageView inside this method you can use code below:
if let imageView = recognizer.view as? UIImageView {
...
}
or with new guard keyword (Swift 2.0)
guard let imageView = recognizer.view as? UIImageView
else { return }
...
You can change
foo(recognizer: UITapGestureRecognizer) {
}
I have 77 buttons on one view. These 77 buttons are in a collection outlet. The buttons are wired to trigger the same segue. The segue presents a detailViewController with information passed to it from the button. I need to know what button triggered the segue so that I know what data to pass to the detail controller.
I set the tag in the viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
self.containerView.backgroundColor = UIColor.blackColor()
var count = 0
for item in buttonOutlets {
item.layer.cornerRadius = 2.0
item.layer.borderWidth = 2.0
item.tag = count
item.layer.borderColor = UIColor.yellowColor().CGColor
item.addTarget(self, action: Selector("handleButtonPress"), forControlEvents: UIControlEvents.TouchUpInside)
count = count + 1
println(item.tag) // prints correct tag numbers
}
self.fetchAllObjects()
}
This is my prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var upcoming: itemDetail = segue.destinationViewController as! itemDetail
if (segue.identifier == "loadDetailView") {
println(buttonOutlets[1].tag) // prints correct tag number
let objectPlace = sender?.tag
upcoming.parseObject = collectionObjects[objectPlace!] as? PFObject
}
The answer lies in item.addTarget. The action selector calls handleButtonPress. My original button press handler was:
func handleButtonPress(sender: UIButton) {
self.performSegueWithIdentifier("loadDetailView", sender: self)
}
What I was missing was sender: sender:
func handleButtonPress(sender: UIButton) {
self.performSegueWithIdentifier("loadDetailView", sender: sender)
}
Then add a colon to handleButtonPress call:
item.addTarget(self, action: Selector("handleButtonPress:"), forControlEvents: UIControlEvents.TouchUpInside)