Exactly what the title implies. How do the gesture recognizers work, specifically UIGestureRecognizer. Here is a small snippet of my code
var keyboardDismiser: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "gestureRecognizer:")
keyboardDismiser.direction = .Right | .Left
noteView.addGestureRecognizer(keyboardDismiser)
and
func gestureRecognizer(sender: UISwipeGestureRecognizer!) {
println("swipe")
self.view.endEditing()
}
My goal is to dismiss the keyboard when switching from view to view in a UIScrollView with 3 pages. What am I doing wrong? There isn't much documentation on this in Swift.
First you setting the recognizer on the note view. It will only be active on the note view.
In addition, you are not setting direction correctly. You are setting then changing the it's value. To set it to both right and left, you use the | operator. Also direction knows it a UISwipeGestureRecognizerDirection so you don't need specify that.
var keyboardDismiser = UISwipeGestureRecognizer(target: self, action: "gestureRecognizer:")
keyboardDismiser.direction = .Right | .Left
self.view.addGestureRecognizer(keyboardDismiser)
Finally, I would use endEditing() instead of resignFirstResponder().
func gestureRecognizer(sender: UISwipeGestureRecognizer!) {
println("swipe")
self.view.endEditing(true)
}
Hope that helps.
I believe that selectors in Swift do not need the : at the end; they're just a string with the name of the function: gestureRecognizer. So this is what you should have:
var keyboardDismiser = UISwipeGestureRecognizer(target: self, action: "gestureRecognizer")
Relevant question here.
Related
Is there a way to detect up & down swipes on TableView in a ViewController?
in ViewDidLoad();
let upSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swipeUp:"))
let downSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swipeDown:"))
upSwipe.direction = .Up
downSwipe.direction = .Down
tableView.addGestureRecognizer(upSwipe)
tableView.addGestureRecognizer(downSwipe)
Unfortunately, this doesn't work.
func swipeDown() {
print("A")
}
nothing returned.
What is the appropriate way to detect gestures on TableView (not cell/as a whole)?
Implement UIScrollViewDelegate
Define scrollViewDidScroll(_:)
Set your tableview's scrollview's delegate to whatever object you've got the above defined in.
I'm trying to create a keyboard the allows single tap, double tap, and triple tap. So I want to add a UITapGestureRecognizer() to each button in my keyboard. I know how to do this manually from the xib file (add each letter it's own gesture, which would take ages) but not quite sure how to do it in the Controller.
I wrote this for double tap in the viewDidLoad() method:
let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTap.numberOfTapsRequired = 2
for button in self.view.subviews{
button.addGestureRecognizer(doubleTap)
}
and created a doubleTapCharacter() method but it's still not working. I also want to be able to send information to the doubleTapCharacter method.
Any help would be much appreciated. Also, I'm very new to swift so if the instructions are complicated, I'd highly appreciate it if you can break it down a little.
create and add the gesture recognizers:
for button in view.subviews {
// create the gesture recognizer
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTapRecognizer.numberOfTapsRequired = 2
// add gesture recognizer to button
button.addGestureRecognizer(doubleTapRecognizer)
}
then implement the target method:
func doubleTapCharacter(doubleTapRecognizer: UITapGestureRecognizer) {
let tappedButton = doubleTapRecognizer.view as! UIButton
print(tappedButton.titleForState(UIControlState.Normal))
}
If I want to swipe a lot of UIView in a UIViewController, what is the best method?
Now the UIView after each swipe the hidden property is set to true, so the (for example)second UIView is visible.
But I have a problem because inside the UIView I have some UIButton and it cover the UIView so it's difficult to swipe.
Any ideas?
var swipe = UISwipeGestureRecognizer(target: self, action: "yourSwipeMethod")
swipe.direction = (UISwipeGestureRecognizerDirection.Left | UISwipeGestureRecognizerDirection.Up | UISwipeGestureRecognizerDirection.Right | UISwipeGestureRecognizerDirection.Down)
self.view.addGestureRecognizer(swipe)
func yourSwipeMethod()
{
//do something
}
Is this what you wanted?
Before you respond, keep in mind I am very new to swift (last time I coded was 2 years ago and that was OBJC), and I seem to be having an error. Here's a sample of my code:
init(sourceView:UIView, menuItems:Array<String>) {
originView = sourceView
sideBarTableViewController.tableData = menuItems
animator = UIDynamicAnimator(referenceView: originView)
let showGestureRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
showGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Right
originView.addGestureRecognizer(showGestureRecognizer)
let hideGestureRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
}
I seem to be having errors with the UISwipeGestureRecognizer target. I can't set it to self, because it gives me this error: 'self' used before super.init call.
Any help is very much appreciated.
I don't think it's linked to the gesture recognisers. Try to modify your Init() method, by adding at the beginning (inside):
super.init()
This will initialize the object with the parameters of the class you are overriding. Also it will probably ask you to add override before your init method.
In my iPhone app, I require to recognize the swipe gesture made by the user on the view.
I want the swipe gestures to be recognized and perform a function on swipe.
I need that the view should horizontally slide and show another view as a user makes a swipe gesture.
What needs to be done?
How do I recognize it?
If You know how it works, but still need a quick example, here it is! (it will become handy at least for me, when I will need copy-paste example, without trying remembering it)
UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(doSomething)];
[mSwipeUpRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:mSwipeUpRecognizer];
and in .h file add:
<UIGestureRecognizerDelegate>
Use the UISwipeGestureRecognizer. Not much else to say really, gesture recognizers are easy. There are WWDC10 videos on the subject even. Sessions 120 and 121. :)
The following link below redirects you to a video tutorial which explains you how to detect swipes on the iPhone in Objective-C:
UISwipeGestureRecognizer Tutorial (Detecting swipes on the iPhone)
Code sample below, to achieve that in Swift:
You need to have one UISwipeGestureRecognizer for each direction. It's a little weird because the UISwipeGestureRecognizer.direction property is an options-style bit mask, but each recognizer can only handle one direction. You can send them all to the same handler if you want, and sort it out there, or send them to different handlers. Here's one implementation:
override func viewDidLoad() {
super.viewDidLoad()
var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipeDown)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
println("Swiped right")
case UISwipeGestureRecognizerDirection.Down:
println("Swiped down")
default:
break
}
}
}
Swift 5 version:
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGestureRight))
swipeRight.direction = UISwipeGestureRecognizer.Direction.right
self.view.addGestureRecognizer(swipeRight)