Swift TapGestureRecognizer and UIButton [duplicate] - ios

This question already has an answer here:
Swift: "Unrecognized selector sent to instance" error when trying to use tap gesture
(1 answer)
Closed 4 years ago.
I'm new to Swift so i'm sorry for any type of mistakes.
In my View Controller i have a view, CanvasView, and i want the user clicks on a button and add a shape where taps. I create a uiview named ShapeSquare and I have three buttons with three different shapes. Unfortunately i don't understand where i'm wrong. This is only a part of the code.
Any suggestions will be appreciated!
#IBOutlet weak var CanvasView: CanvasView!
override func viewDidLoad() {
super.viewDidLoad()
let tapGR = UITapGestureRecognizer(target: self, action: Selector(("didTap")))
tapGR.numberOfTapsRequired = 1
CanvasView.isUserInteractionEnabled = true
CanvasView.addGestureRecognizer(tapGR)
}
func didTap(tapGR: UITapGestureRecognizer) {
let tapPoint = tapGR.location(in: CanvasView)
let shapeView = ShapeSquare(origin: tapPoint)
CanvasView.addSubview(shapeView)
}
#IBAction func SquareShape(_ sender: UIButton) {
CanvasView.setNeedsDisplay()
}

1-
let tapGR = UITapGestureRecognizer(target: self, action: #selector(didTap(_:)))
2-
#objc func didTap(_ tapGR: UITapGestureRecognizer) {

Related

UILabel set onClick

I'm building an application in Swift 3, so I want to call a function if I click on a particular UILabel, so I'm write this code but not works:
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tap)
How can I render UILabel clickable ?
Set user interaction enabled for the UILabel and add the below code in the viewDidLoad()
self.label.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(self.labelTapped))
self.label.addGestureRecognizer(tap)
Add the tap action function as below :
#objc func labelTapped(_ gestureRecognizer: UITapGestureRecognizer) {
print("Label clicked")
}
Please make user that there is no other transparent view overlapping the UILabel in the view. If the UILabel is a part of another view then please make sure that the container View's user interaction is enabled.
Hope this helps.
Your selector should be an #objc func within self.
<#YourLabel#>.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleLabelTap)))
And when the user taps the label it will trigger:
#objc func handleLabelTap() {
// handle label tap here
}
You are lacking a function to trigger when the gesture touch is recognized. You need to add following:
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction(_:)))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tap)
#objc func tapFunction(_ gestureRecognizer: UITapGestureRecognizer) {
// handle label tap here
}
Please ensure that You have connected the outlet to UILabel because I have created simple demo code by copy-paste your code and it is worked as expected.
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tap)
}
#objc func tapFunction() {
print("tapFunction")
}
I suggest, Please remove UILabel from UIViewController and add it again.
Download sample code
Note: - Please ensure that user-interaction of UILabelis enabled
First you need to add Tap Gesture into storyboard
Create Action of that particular gesture
override func viewDidLoad() {
super.viewDidLoad()
let tapOnLabel = UITapGestureRecognizer(target: self, action: #selector(self.tapGestireAction))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tapOnLabel)
}
#IBAction func tapGestureAction(_ sender: UITapGestureRecognizer) {
//Perform action
}

Swift UIButton action and GestureRecognizer

I'm trying to solve this problem but I don't understand how to do it.
I have a view controller in which I have a view (CanvasView) and three buttons. Each button draw a type of shape. I want the user with a click of a button to add a shape with a tap in a certain point of the CanvasView only when the button is clicked.
Is it possible to allow tapGesture only when the button is clicked?
Here is the code:
#IBOutlet weak var CanvasView: CanvasView!
override func viewDidLoad() {
super.viewDidLoad()
let tapGR = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
tapGR.numberOfTapsRequired = 2
CanvasView.isUserInteractionEnabled = true
CanvasView.addGestureRecognizer(tapGR)
}
#IBAction func tap(_ sender: UITapGestureRecognizer) {
let tapPoint = sender.location(in: CanvasView)
let shapeView = ShapeSquare(origin: tapPoint)
CanvasView.addSubview(shapeView)
}
#IBAction func DrawSquare(_ sender: UIButton) {
CanvasView.setNeedsDisplay()
}
Keep the gesture disabled until the button is tapped.
Start by making the gesture a property:
var tapGR: UITapGestureRecognizer!
Then update viewDidLoad:
tapGR = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
tapGR.isEnabled = false
Then in your button handler:
tapGR.isEnabled = true

UIGestureRecognizer on only one part of the screen

If you look into the image, you'll see a textview, button and a slider.
I have completed all the steps except for the last one which is, when I tap on the slider, it constantly disappear. I know it disappear because I implemented UITapGestureRecognizer.
I guess my question is, I want the slider to disappear everytime I tap anywhere on the screen but when I am using the slider, I dont want the slider to disappear which is happening now every time I release my tap.
I have tried implementing one more UITapGestureRecognizer in sizeRefont with a function to keep sizeRefont.isHidden false but when I do that, the slider will not disappear whenever I tap on the screen.
I tried putting sizeRefont.isHidden = false in sizeRefont action and it doesnt work either.
class ResizeController: UIViewController {
#IBOutlet weak var sizeRefont: UISlider!
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissRefontSize(_:)))
view.addGestureRecognizer(tap)
}
#IBAction func sizeRefont(_ sender: AnyObject) {
let fontSize = CGFloat(sizeRefont.value)
textView.font = UIFont(name: textView.font!.fontName, size: fontSize * 30.0)
}
#IBAction func showSlider(_ sender: Any) {
sizeRefont.isHidden = false
}
func dismissRefontSize(_ sender: UITapGestureRecognizer) {
if sender.location(in: sizeRefont){
sizeRefont.isHidden = false
} else {
sizeRefont.isHidden = true
}
}
}
There is an error on if sender.location(in: sizeRefont) where it says CGPoint is not convertible to Bool
Image
First thing you need to do is you need to Adjust the dismissRefontSize() method to to the following :
func dismissRefontSize(_ sender: UITapGestureRecognizer) {
let location = sender.location(in: view)
If sizeReFont.frame.contains(location) {
// do nothing
}else {
sizeReFont.isHidden = true
}
}
The other thing you need to adjust is creating the tap recognized in your viewDidLoad() to the following:
let tap = UITapGestureRecognizer(target: self, action : #selector(dismissRefontSize(_:)))

unrecognized selector sent to instance Error when trying to setTarget action [duplicate]

I've searched for solutions to this problem but couldn't find anything that seems to address it in my case. I'm getting the above exception from a UITapGestureRecognizer.
Here's the simplified code:
import UIKit;
class ViewController : UIViewController, UIScrollViewDelegate
{
#IBOutlet weak var scrollView:UIScrollView!;
var imageView:UIImageView!;
override func viewDidLoad()
{
super.viewDidLoad();
... set up imageView/scrollView here ...
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "onScrollViewDoubleTapped");
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
scrollView.addGestureRecognizer(doubleTapRecognizer);
}
func onScrollViewDoubleTapped(recognizer:UITapGestureRecognizer)
{
}
}
Can anyone tell what is wrong with this code? It seems all correct to me. I suspect that it has to do with assigning ViewController as delegate to scrollView (or vice versa)? However the ViewController is set as the delegate to scrollView. But maybe it's something else that causes this error?
Try adding a colon to your selector string.
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "onScrollViewDoubleTapped:");
As cabellicar123 mentioned, this indicates that the selector takes an argument.
Swift 4 using #selector.
let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSelectItem(sender:)))
#objc func didSelectItem(sender: AnyObject?) {
print("didSelectItem: \(sender)")
}
Also try adding a parameter to your method:
...(target: self, action: "yourMethodName:")
func yourMethodName(sender: AnyObject?)
{
println("Clicked yourMethodName")
}
Maybe could help someone: I had this error because I declared private the selector method:
func setup() {
let singleFingerTap = UITapGestureRecognizer(target: self, action: "didTapOnViewInteraction:")
singleFingerTap.numberOfTapsRequired = 1
self.viewInteraction.addGestureRecognizer(singleFingerTap)
}
private func didTapOnViewInteraction(recognizer: UITapGestureRecognizer) {
}
Removing "private" keyword, all works great!
for swift 4
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(SignUpViewController.click))
userImage.addGestureRecognizer(tap)
userImage.isUserInteractionEnabled = true
}
#objc func click()
{
print("Tapped on Image")
}
where SignUpViewController is your viewcontroller name
In my case, with Swift 4, the selector was correctly defined, but since the item with the gesture was inside a collection view, it was causing the selector not to be found. I implemented a custom cell and configured the selector inside of it
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "recent", for: indexPath) as! MyCollectionViewCell
cell.configureGestureWith(entry: recentEntries.sorted(by: {$0.createdAt > $1.createdAt})[indexPath.row], sender: self)
Hope this helps some one.

Swift3 iOS - How to make UITapGestureRecognizer trigger function

I am trying to add a UITapGesture to a UIButton so it will trigger a function when tapped. I am using Swift 3 and is getting some error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SwiftRunner.ViewController tapBlurButton]: unrecognized selector sent to instance 0x149e07610'
This is roughly what I have:
// Swift 3
import UIKit
class ViewController {
#IBOutlet weak var qsBlurButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: Selector(("tapBlurButton")))
qsBlurButton.addGestureRecognizer(tapGesture)
}
func tapBlurButton(sender: UITapGestureRecognizer) {
print("Please Help!")
}
}
From your code you are using swift 3.0 so change your selector syntax like this
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(_:)))
and Your function like this
func tapBlurButton(_ sender: UITapGestureRecognizer) {
print("Please Help!")
}
Edit:
Not idea that you are using button with tap gesture, instead of that use inbuilt method addTarget for button no need to create tap gesture for it like this
qsBlurButton.addTarget(self, action: #selector(self.tapBlurButton(_:)), forControlEvents: .TouchUpInside)
func tapBlurButton(_ sender: UIButton) {
print("Please Help!")
}
Swift 3
In the case where the func for the selector looks like this (note: it doesn't have a _):
func tapBlurButton(sender: UITapGestureRecognizer) {
print("Please Help!")
}
Then the selector looks like this:
#selector(self.tapBlurButton(sender:))
So the final code for the selector is this:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(sender:)))
If you do not specify that the first parameter has _, then you need to use the full name of the first parameter.
To add a gesture recognizer you have to create one indicating which function it will call:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTapFrom(recognizer:)))
Then add it to the element you need. (You are using a button, and as others have explained buttons have their native 'addTarget' methods)
For explanation purposes imagine you want to add it to an UIView:
self.someView.addGestureRecognizer(tapGestureRecognizer)
And don't forget that some elements are "not user interactive" by default so you might have to change that property too:
self.someView.isUserInteractionEnabled = true
In Swift 4 the function needs the #objc declaration:
#objc func handleTapFrom(recognizer : UITapGestureRecognizer)
{
// Some code...
}

Resources