issue to swipe view swift - ios

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?

Related

Detecting Swipe Gestures on TableView

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.

Add single, double and triple tap gestures to all "buttons" in custom keyboard in swift

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))
}

Why does the swipe gesture recognizer give an error in swift?

I'm trying to add the swipe gesture recognizer to the collection view by using swift. Everytime the user swipe to right or left, I want my collection view reload. However, when I swipe to right or left in collection view on the simulator, it gives me an error and AppDelegate.swift class is automatically opened. The error says " libc++abi.dylib: terminating with uncaught exception of type NSException "
I write this part to the viewDidLoad() in the collectionViewController class
collectionView.userInteractionEnabled = true
collectionView.delegate = self
var left = UISwipeGestureRecognizer(target: collectionView, action: "swipping:")
left.direction = UISwipeGestureRecognizerDirection.Left
collectionView.addGestureRecognizer(left)
var right = UISwipeGestureRecognizer(target: collectionView, action: "swipping:")
right.direction = UISwipeGestureRecognizerDirection.Right
collectionView.addGestureRecognizer(right)
This is the swipping function:
func swipping (gesture : UIGestureRecognizer){
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Left:
collectionView.reloadData()
pageControl.currentPage++
case UISwipeGestureRecognizerDirection.Right:
collectionView.reloadData()
pageControl.currentPage--
default:
break
}
}
}
Added UIGestureRecognizerDelegate to the CollectionViewController class and AppDelegate class. What should I do?
Thanks.
When you declare gesture recogniser you specify collectionView as a target:
var left = UISwipeGestureRecognizer(target: collectionView, action: "swipping:")
But I believe the swiping: method is placed in the same file where the declaration is taking place.
You have to change target to self:
var left = UISwipeGestureRecognizer(target: self, action: "swipping:")
var right = UISwipeGestureRecognizer(target: self, action: "swipping:")
//Extended
This is not working because UICollectionView is a subclass of UIScrollView and when you swipe UIScrollView handle the gesture (this is because you can create collection in horizontal direction so collection view needs to know that you are scrolling left/right).
You should consider override UIScrollViewDelegate method:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
to find out the swipe and you should handle it instead of adding gesture recogniser.
Try something like that:
//Declare private property
#property (nonatomic, assign) CGFloat prevOffset
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.prevOffset > scrollView.contentOffset.x)
// You are swiping right
else if (self.prevOffset < scrollView.contentOffset.x)
// You are swiping left
self.prevOffset = scrollView.contentOffset.x;
}

Function is not being called using UISwipeGestureRecognizer

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.

How to detect Swipe Gesture in iOS?

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)

Resources