Inside of a VC, I'm trying to figure out how to have a UIButton recognize a horizontal swipe and then tap.
There is an array of text.
var plotList = ["stuff", "to do", "this", "or that"]
default display is plotList[0].
the carousel repeats in an infinite loop doesn't stop at the end of the array.
as user swipes over button only, i don't want the other components of the VC to react to this particular swipe, i want the button to display the plotList string.
when the user taps on the button, i want to shove the result into a switch that will launch the appropriate UIView.
i'd like to avoid doing this UILabel/UIButton combination. See Handling Touch Event in UILabel and hooking it up to an IBAction.
so far i'm here
import UIKit
class carouselVC: UIViewController {
#IBOutlet var carouselView: UIView!
#IBOutlet var labelOutlet: UILabel!
#IBOutlet var buttonOutlet: UIButton!
var plotList = ["stuff", "this", "that"]
let swipeRec = UISwipeGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
var swipeButtonLeft: UISwipeGestureRecognizer =
UISwipeGestureRecognizer(target: self, action: "buttonLeft")
swipeButtonLeft.direction = UISwipeGestureRecognizerDirection.Left
self.buttonOutlet.addGestureRecognizer(swipeButtonLeft)
var swipeButtonRight: UISwipeGestureRecognizer =
UISwipeGestureRecognizer(target: self, action: "buttonRight")
swipeButtonRight.direction = UISwipeGestureRecognizerDirection.Right
self.buttonOutlet.addGestureRecognizer(swipeButtonRight)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func buttonLeft(){
println("buttonLeft")
}
func buttonRight(){
println("buttonRight")
}
#IBAction func buttonAction(sender: UIButton) {
sender.setTitle(plotList[1], forState: .Normal)
}
}
any help?
Use a UIView with a UILabel, instead of button. Add UIView over UILabel and make it clear color. You may make UIlabel look like a button. Add a swipe gesture/tap gesture on it. And do any operation in the action of the gestures.
Related
I have a UIViewController and I have embedded a Search Bar and a Collection View. When I press on the searchbar, the keyboard appears. I would like to hide this keyboard if the user decides to change his mind by tapping any where on the screen but the search bar. I have tried the following without success:
Adding a Tap Gesture Recognizer
using 'self.mySearchBar.endEditing(true)'
class CollectionViewFolder: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate ,UICollectionViewDelegateFlowLayout, UISearchBarDelegate{
/*** OUTLETS ***/
#IBOutlet weak var mySearchBar: UISearchBar!
// 1. I have tried adding a Tap Gesture Recognizer
// TAP ON SCREEN LISTENER
#IBAction func tapOnScreen(_ sender: UITapGestureRecognizer) {
print("tap tap ...")
self.mySearchBar.resignFirstResponder()
}
// 2. Added the following to the viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.mySearchBar.endEditing(true)
}
}
You can use this extension.
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
#objc func dismissKeyboard() {
view.endEditing(true)
}
}
Usage. In your viewController:
override func viewDidLoad() {
super.viewDidLoad()
hideKeyboardWhenTappedAround()
}
I am trying (and failing) to create just one tapGestureRecognizer to use on several UILabels.
Right now I am creating for every label in viewDidLoad a separate tapGestureRecognizer and adding it to the appropriate label. I ran into this problem because every touch should obviously call a different function.
This is how I create them:
#IBOutlet weak var buttonOne: UILabel!
#IBOutlet weak var buttonTwo: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//tapGestureRecognizer for buttonOne
buttonOne.isUserInteractionEnabled = true
let oneGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MainViewController.buttonOneAction))
buttonOne.addGestureRecognizer(oneGestureRecognizer)
//tapGestureRecognizer for buttonTwo
buttonTwo.isUserInteractionEnabled = true
let twoGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MainViewController.buttonTwoAction))
buttonTwo.addGestureRecognizer(twoGestureRecognizer)
...
They work fine, but how and where could I create just one tapGestureRecognizer and add it in viewDidLoad to each label with a different action?
Just create one per label.
The alternative is to create a single tap gesture recognizer that you attach to the common superview of all target views, and write a BUNCH of code that does hit-testing to figure out if the tap landed on any of your labels, and if so, which one, and dispatches the desired method for that label.
However, that's the whole point of tap gesture recognizers. You'd probably spend several days developing a bunch of code that has no benefits over using multiple tap gesture recognizers.
As already mentioned in comments by #Duncan C you can switch the gesture recognizer view as follow:
class ViewController: UIViewController {
#IBOutlet weak var buttonOne: UILabel!
#IBOutlet weak var buttonTwo: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
[buttonOne, buttonTwo].forEach {
$0?.isUserInteractionEnabled = true
$0?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapGesture)))
}
}
#objc func tapGesture(_ gesture: UITapGestureRecognizer) {
guard let label = gesture.view as? UILabel else { return }
switch label {
case buttonOne: print("buttonOne")
case buttonTwo: print("buttonTwo")
default: break
}
}
}
I'm making a swipe gesture recogniser which receives swipes in particular UIView but I also want it to be transparent, making the alpha=0 doesnt responds to swipes.
This is the code for adding the gesture:
#IBOutlet weak var swipeView: UIView!
let swipeRec = UISwipeGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
swipeRec.addTarget(self, action: #selector(ViewController.swipedView))
swipeView.addGestureRecognizer(swipeRec)
swipeView.isUserInteractionEnabled = true
swipeRec.direction=UISwipeGestureRecognizerDirection.right
}
func swipedView(){
print("swiped")
}
Setting the background color of the view to ClearColor rather than alpha to 0 should work.
swipeView.backgroundColor = UIColor.clearColor()
As of right now, when I run my program and tap, I am getting the println() message after one tap. However, after the first println() message, I have to double tap for every following println() message.
I want it so every time I have to double tap (including the first time).
In my View Controller I have the following:
#IBOutlet weak var graphview: GraphView! {
didSet {
//
graphview.addGestureRecognizer(UITapGestureRecognizer(target: graphview, action: "doubleTap:"))
}
}
And my function in the View is the following:
func doubleTap(gesture: UITapGestureRecognizer)
{
gesture.numberOfTapsRequired = 2
println("hit twice")
}
You have to set
gesture.numberOfTapsRequired = 2
before adding the gesture recognizer to the view, i.e.
#IBOutlet weak var graphview: GraphView! {
didSet {
var doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: graphview, action: "doubleTap:")
doubleTap.numberOfTapsRequired = 2
graphview.addGestureRecognizer(doubleTap)
}
}
and
func doubleTap(gesture: UITapGestureRecognizer)
{
println("hit twice")
}
I'm trying to add gesture(swipe) functionality to an existing button in my view but I can't figure out how to attach the swipe to the button's area.
The desired effect is to have a button I can press as well as swipe to produce different outcomes. So far the way I'm implementing gestures applies it to my entire view not just the button.
I have a feeling it's pretty simple but it's been escaping me for a couple of days - I might just be searching for the wrong thing.
(I'm assigning '#IBOutlet var swipeButton: UIButton!' to my button BTW)
Code below:
class ViewController: UIInputViewController {
#IBOutlet var swipeButton: UIButton!
let swipeRec = UISwipeGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
self.loadInterface()
var swipeButtonDown: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "ButtonDown")
swipeButtonDown.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipeButtonDown)
}
#IBAction func buttonPressed(sender: UIButton) {
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.insertText("button")
}
func buttonDown(){
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.insertText("swipe")
}
}
If you want to add swipeGesture into Button then do it like this way:
self.yourButton.addGestureRecognizer(swipeButtonDown)
and also there is a mistake in selector you sent it should be like:
var swipeButtonDown: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "buttonDown")
change ButtonDown to buttonDown