I want to trigger two action on button click and button long click. I have add a UIbutton in my interface builder. How can i trigger two action using IBAction can somebody tell me how to archive this ?
this is the code i have used for a button click
#IBAction func buttonPressed (sender: UIButton) {
....
}
can i use this method or do i have to use another method for long click ?
If you want to perform any action with single tap you and long press the you can add gestures into button this way:
#IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, #selector (tap)) //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, #selector(long)) //Long function will call when user long press on button.
tapGesture.numberOfTapsRequired = 1
btn.addGestureRecognizer(tapGesture)
btn.addGestureRecognizer(longGesture)
}
#objc func tap() {
print("Tap happend")
}
#objc func long() {
print("Long press")
}
This way you can add multiple method for single button and you just need Outlet for that button for that..
#IBOutlet weak var countButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
addLongPressGesture()
}
#IBAction func countAction(_ sender: UIButton) {
print("Single Tap")
}
#objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began {
print("Long Press")
}
}
func addLongPressGesture(){
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
longPress.minimumPressDuration = 1.5
self.countButton.addGestureRecognizer(longPress)
}
Why not create a custom UIButton class, create a protocol and let the button send back the info to delegte. Something like this:
//create your button using a factory (it'll be easier of course)
//For example you could have a variable in the custom class to have a unique identifier, or just use the tag property)
func createButtonWithInfo(buttonInfo: [String: Any]) -> CustomUIButton {
let button = UIButton(type: .custom)
button.tapDelegate = self
/*
Add gesture recognizers to the button as well as any other info in the buttonInfo
*/
return button
}
func buttonDelegateReceivedTapGestureRecognizerFrom(button: CustomUIButton){
//Whatever you want to do
}
Related
I have UITextField and UIButton in my application.
To hide system keyboard which is shown when UITextField is clicked, I added UITapGestureRecognizer to my view.
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(didTap(_:)))
view.addGestureRecognizer(tapGesture)
}
#objc func didTap(_ recognizer: UIGestureRecognizer) {
view.endEditing(true)
}
#IBAction func onClickedButton(_ sender: Any) {
print("aaa")
}
This code worked very well when I touched outside of my button.
However, when I clicked the button which has IBAction(onClickedButton), the keyboard did not disappear and only the message "aaa" printed in output console.
What I want to do is to hide keyboard and invoke IBAction at the same time. In other words, I want to invoke Tap gesture and IBAction at the same time, when I clicked my button.
How can I acheive this?
I found the solution.
Just setting
tapGesture.cancelsTouchesInView = false
can acheive this.
By doing like this, tapGesture hides keyboard and after that, passes touch event to the UIButton.
You can just add view.endEditing(true) to your onClickedButton.
try this
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(didTap(_:)))
view.addGestureRecognizer(tapGesture)
}
#objc func didTap(_ recognizer: UIGestureRecognizer) {
view.endEditing(true)
}
#IBAction func onClickedButton(_ sender: Any) {
print("aaa")
view.endEditing(true)
}
}
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
}
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
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(_:)))
I'm creating a camera app in swift and I have a UIButton. I want to propose two options: when user single taps the button - it takes photo and when user holds his finger on a button - it records the movie until user releases the button.
I have functions for recording and taking photo, now I need to distinguish the user action on a button.
Available actions for this button are:
and I tried to start recording on touch down and stop recording on touch up inside, but then I don't know where should I put the code responsible for taking photos. If I put it also in touch down then when user starts recording movie - will also take a photo, and I want to avoid it.
The gesture recognizers for tap and long tap work well with each other to short this out (The tap defers firing until its sure its not a long press).
class ViewController: UIViewController{
#IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap)))
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGestureRecognizer.minimumPressDuration = 1
button.addGestureRecognizer(longPressGestureRecognizer)
}
#objc private func tap(tapGestureRecognizer: UITapGestureRecognizer) {
print("tap")
}
#objc private func longPress (longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == .began {
print("long press began")
}
}
}
For just taking a single photo, you can just connect the button to an IBAction with Touch Up Inside.
For taking a video you need to first connect the button to an IBOutlet, and then add a UILongPressGestureRecognizer to it to detect when the user long press the button.
#IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress(gesture:)))
longPressGesture.minimumPressDuration = 1 // Minimum duration to trigger the action
self.button.addGestureRecognizer(longPressGesture)
}
func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
print("Began")
// Video starts recording
} else if gesture.state == .ended {
print("End")
// Video stops recording
}
}
#IBAction func takePhoto(_ sender: UIButton) {
// Take photo
}
You can use UILongPressGestureRecognizer for record video and #IBAction - Touch Up Inside for take photo function.
Step 1: In the storyboard, create an UIButton and drag UILongPressGestureRecognizer from Object libray into this button
Step 2: In the ViewController.swift, we have this code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
#IBOutlet weak var button: UIButton!
#IBAction func takePhoto(_ sender: AnyObject) {
label.text = "Take photo"
}
#IBAction func recordVideo(_ sender: AnyObject) {
label.text = "Record video"
}
}
Step 3: Open Assistant editor and connect these #IBOutlet and #IBAction
That's it!
You can use following code to give button action on long press :
I used gesture to detect the log press of the button, and it work for me.
Objective-C
UILongPressGestureRecognizer *lPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(TakePhotoLongPressAction:)];
[self.btnPhoto addGestureRecognizer:lPress];
[lPress release];
- (void)TakePhotoLongPressAction:(UILongPressGestureRecognizer*)gevent
{
if ( gevent.state == UIGestureRecognizerStateEnded ) {
NSLog(#"Button Long Press");
}
}
SWIFT
// add guesture recognizer
let lPress = UILongPressGestureRecognizer(target: self, action: #selector(takePhotoLongPressAction(_:)))
self.button.addGestureRecognizer(lPress)
func takePhotoLongPressAction(gevent: UILongPressGestureRecognizer) {
if gevent.state == UIGestureRecognizerState.Began {
print("Long Press button")
}
}
Please check and let me know if any thing required.