Swipe gesture on imageview inside image view collection - ios

I'm trying to add a swipe gesture to an image located in an UICollectionView
This is my existing hierarchy:
I've tried adding the swipe gesture to the UICollectionView as well as the UICollectionViewCell representing the image but neither worked.
In another UIViewController I have a UITableViewCell to which I added the swipe gesture and everything works fine so I'm sure my gesture initialization is correct, I'm adding the gesture to the UICollectionView at the method viewDidLoad, is that the right place or should I place it in different method?

Related

Pan gesture in UITableViewCell that can move image view all over tableview without going beneath the cell

I am trying to create a tinder like swipe left and right in UITableView cell. Here, the tableview cell contains an image view which I can move around with pan gesture. When I initiate the gesture on the image view, I want to move it freely over my device screen.
I am facing the problem that when I move the image view around it goes below the current cell. How can I bring the image view over all others so it doesn't go below the cell frame?
I tried using view.bringSubviewToFront but it doesn't work.
Please suggest a way I can achieve this.
An image view still contained in a cell is not going to be reliably brought on top of all other relevant views. You will have to move that view from the cell to the table view's parent view while dragging (and then return it to the cell if necessary at the end).
You will probably want to look into UIView.convert(_:from:) to figure out the frame to move the image view to in the parent view.

Add scroll controller to CollectionView

I have a Collection View in a ViewController, with a Pan Gesture Recognizer that manages the reordering of the cells. Because of that to scroll the Collection View I want to add a "scroll controller" on the side of the Collection View, like Noteshelf app, as showing here:
My Collection view is a simple collection view with a label for every cell:
Put your note view in a scrollview and disable user scrolling by setting isScrollEnabled to false. Add a UIPanGestureRecognizer to your side bar and make yourself the target of the gesture recognizer. In your tap gesture handler function you:
Get the current translation of the pan with translation(in:)
Use this value to scroll the scrollView with setContentOffset(_:animated:)
reset the translation so you get only the difference and not the total on the next call with setTranslation(_:in:)
Edit to clairfy, UICollectionView inherits from UIScrollView so it has all of the UIScrollView properties, you just need to look under UISCrollView and not UICollectionView.

Touches lost on UIScrollview

I am using a UIView subclass which can be resized by dragging on the borders and it is added as subview on a UIScrollView. I am also using a UIGestureRecognizer subclass to move the view on the UIScrollView such that the custom view remains visible and the scrollview autoscrolls.
This setup is working as intended but for one problem. Whenever the custom view is resized, the touches on UIScrollView are lost, i.e the scrollview does not recognise the tap or scroll on it unless the custom view is tapped or dragged once again. I have found that after resizing, neither custom UIView nor custom UIGestureRecognizer's touches began, cancelled or ended are called.
Where might the problem be?

ios swipe gesture not being recognized on subview with gesture on parent view

I have a tableview that holds a number of cells with an image centered in each cell. I set a right swipe gesture on the tabelview. Each image view overrides the touches* methods. I noticed that if I right swipe outside the images, the parent view responds. If I swipe over the image views in the cells, the parent does not respond.
Does the swipe gesture get blocked by the touches* override in the child image subviews?
You may add UIswipeguesture to your image subview. So you don't need to override any touch function
Uiswipeguesture gues=uiswipeguestu
Imageview addguesture: gues;

UITableViewCell horizontal custom swip- to-delete effect

I have a UITableView and would like to have a custom way to delete my cells. When someone swipe on it, the cell would move horizontally on the side and disappear.
Exactly like the multitasking menu on iOS 7 (http://movies.apple.com/media/us/ios/ios7/f34c5445-5a9c-4b3a-9556-8efe89147559/shared_multitasking/shared_multitasking_2x.mp4), but instead of swiping vertically, it would be horizontal.
Does any one knows how to do that? Should I just detect a swipe and change the frame of my cell with a 1 sec animation? Or are there nice subclasses of UITableViewCell you would recommend?
I just threw together a very basic example of how to do this and posted it here: https://github.com/NSPostWhenIdle/MMSwipeToDeleteCollection. Don't expect to be able to drag and drop this into your project and have it work, because quite frankly it probably won't.
The basic idea to create something like this starts with a subclass of UICollectionViewCell that adds a gesture to the cell. In my example, I used a swipe gesture because I'm lazy :p and there is more overhead involved in setting up a pan gesture (which you'll want in your end product) because there is conflict between that pan gesture, and the pan gesture in the collection view's scroll view.
From there it's basically smooth sailing. When the gesture recognizer gets called you animate the cell out the top of the screen. If you set this up with a pan gesture, you'll need to configure this to drag the cell, and animate up or down upon completion, but in my swiping example, the cell moves to just out the top (of the 4 inch simulator, I used static values).
Then all that's left to do is some clean up. Once the cell has exited the screen you can safely delete it from your datasource and then from the collection view. (I used notification center do alert the collection view, but you should probably make a protocol) The only issue I had with this was that the cell animated back down while fading out as part of the stock deletion animation. Setting its alpha to 0 after it leaves the screen solves this problem.

Resources