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

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

Related

Make The Swipe gesture Animated

i have swipe gesture that switch between tab bars when I swipe in the screen right or left its change the tab bar im in it
How would I make it so it looks like it is sliding to the right or left tab bar rather than just instantly changing the tab bar
class SwipeGesture: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let left = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft))
left.direction = .left
self.view.addGestureRecognizer(left)
let right = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight))
right.direction = .right
self.view.addGestureRecognizer(right)
}
#objc func swipeLeft() {
let total = self.tabBarController!.viewControllers!.count - 1
tabBarController!.selectedIndex = min(total, tabBarController!.selectedIndex + 1)
}
#objc func swipeRight() {
tabBarController!.selectedIndex = max(0, tabBarController!.selectedIndex - 1)
}
}
You can achieve this effect using this pod
If you want to build it from scratch, you need to subclass containerView. Then put the view controllers (childVCs) inside a scrollView (scrolls only in horizontal direction).

Transfer PanGesture to another view

I want to start pan gesture in 1 item. And at the instant the gesture is recognized I create a new item view at the exact same spot and transfer the pan gesture to the new item so the new hovering item moves with the fingers.
How can I do this?
Here is the code so far:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(useItemPanGesture(_:)))
item.addGestureRecognizer(panGesture)
func useItemPanGesture(panGesture: UIPanGestureRecognizer) {
let newView = createView()
newView.addPanGesture(target: self, action: #selector(self.actualPanGesture(panGesture)))
panGesture.enabled = false
}
func actualPanGesture(panGesture: UIPanGestureRecognizer) {
//do pan gesture stuff
}
I have trouble in the transfer of the pan gesture. Line starting with newView.addPangesture.

UicollectionView scroll between views in swift ios?

My collection view is working great. It shows a grid of photos and lists hundreds of them. You can swipe vertically to scroll through them all. Life is good. However, I now have a new requirement. I need to be able to detect when the user is swiping left or right. I need to be able to intercept this gesture so I can attach behavior to left and right swipes while keeping intact my collection view's vertical scroll capabilities. Any ideas?
In swift?
If it helps for reference heres a link to my Github project.
https://github.com/StarShowsStudios/GodCards
If you open the project in Xcode you can see the detail view controller. It gets its information from a plist file called cards according to the selected collection cell controller
You can add left and right swipe gesture recognizer to detect left and right swipe.
override func awakeFromNib() {
super.awakeFromNib()
// Add Left Swipe Gesture
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(SomeClass.respondToSwipeGesture(_:)))
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.addGestureRecognizer(swipeLeft)
// Add Right Swipe Gesture
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(SomeClass.respondToSwipeGesture(_:)))
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.addGestureRecognizer(swipeRight)
}
// This function detects Swipe direction and perform action
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
print("Swiped right")
rightSwipeAction()
case UISwipeGestureRecognizerDirection.Left:
print("Swiped left")
leftSwipeAction()
default:
break
}
}
}

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

Resources