How to enable zoom for UICollectionView
I am not sure whether I got your question in correct way or not, but I think you might want to check these links:-
1. zoom entire UICollectionView - It also has a link to Sample code for UICollectionView. You might be interested in checking it.
2. Adding pinch zoom to a UICollectionView.
3. Make UICollectionView zoomable?
Hope this will help you.
How to do zoom in UICollectionView
You don't zoom a collection like you'd zoom a simple scroll view. Instead you should add a pinch gesture (or some other zoom mechanism) and use it to change the layout so your grid displays a different number of items in the visible part of the collection. This is basically changing the number of columns and thus the item size (cell size). When you update the layout the collection can animate between the different sizes, though it's highly unlikely you want a smooth zoom, you want it to go direct from N columns to N-1 columns in a step.
Make UICollectionView zoomable?
I think what you're asking for looks like what is done in the WWDC1012
video entitled Advanced Collection Views and Building Custom Layouts (demo starts at 20:20).
You basically have to add pinchGesture
to you UICollectionView
, then pass the pinch properties
(scale, center) to the UICollectionViewLayout
(which is a subclass
of UICollectionViewFlowLayout
), your layout will then perform the transformations
needed to zoom
on the desired cell.
How to zoom a UICollectionViewCell on selection Swift 3.0?
Instead of adding the border width for the selected cell, just use transformation scale for zooming the selected cell. Write this code in didSelect:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedCell = priorityCollectionView.cellForItem(at: indexPath) as? BCPriorityListCollectionViewCell
priorityCollectionView.bringSubview(toFront: selectedCell!)
UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 5, initialSpringVelocity: 0, options: [], animations: {
selectedCell?.transform = CGAffineTransform(scaleX: 1.2, y: 2)
})
}
And in didDeselect:
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let unselectedCell = priorityCollectionView.cellForItem(at: indexPath) as? BCPriorityListCollectionViewCell
UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 5, initialSpringVelocity: 0, options: [], animations: {
unselectedCell?.transform = .identity
})
}
Result:
How to zoom in the content of cell in uicollectionview
Just call this method when your collectionviewcell click
[self animateZoomforCell:cell]; // pass cell as collectionviewcell
-(void)animateZoomforCell:(UICollectionViewCell*)zoomCell
{
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
zoomCell.transform = CGAffineTransformMakeScale(1.6,1.6);
} completion:^(BOOL finished){
}];
}
-(void)animateZoomforCellremove:(UICollectionViewCell*)zoomCell
{
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
zoomCell.transform = CGAffineTransformMakeScale(1.0,1.0);
} completion:^(BOOL finished){
}];
}
How to zoom a UIScrollView inside of a UICollectionViewCell?
You might want to try manipulating the UIGestureRecognizers in order to do that. In the GalleryViewController
:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
GalleryImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"galleryImageCell" forIndexPath:indexPath];
ImageContext *imageContext = [self.images objectAtIndex:indexPath.row];
cell.imageContext = imageContext;
[self.collectionView addGestureRecognizer:cell.scrollView.pinchGestureRecognizer];
[self.collectionView addGestureRecognizer:cell.scrollView.panGestureRecognizer];
return cell;
}
From Apple's documentation on UIView:
Attaching a gesture recognizer to a view defines the scope of the represented gesture, causing it to receive touches hit-tested to that view and all of its subviews. The view retains the gesture recognizer.
So you'll also want to make sure to remove them when the cell is not showing anymore.
- (void)collectionView:(UICollectionView *)collectionView
didEndDisplayingCell:(UICollectionViewCell *)cell
forItemAtIndexPath:(NSIndexPath *)indexPath {
// Get the cell instance and ...
[self.collectionView removeGestureRecognizer:cell.scrollView.pinchGestureRecognizer];
[self.collectionView removeGestureRecognizer:cell.scrollView.panGestureRecognizer];
}
Since you're not modifying the UIGestureRecognizer's delegate, only its scope, it will still control the zooming of just that cell's scrollview.
EDIT:
I added the panGestureRecognizer
to the above examples, following a suggestion from the OP that it was needed. The zooming itself is completely handled by the pinchGestureRecognizer
, but it's true that in most cases, after zooming an image to a point where only a subset of it is visible, you'll want to pan to move the visible portion around. That is, it's part of a proper zooming experience.
Adding pinch zoom to a UICollectionView
The good bit – how to make it work
Some very minor tweaks to the above code have resolved What Doesn't Work 1 & What Doesn't Work 2 in the question.
I have added the following lines in to the viewDidLoad
method of my UICollectionViewController
:
[collectionView setMinimumZoomScale: 0.25];
[collectionView setMaximumZoomScale: 4];
I've also updated the example project so that instead of text labels, the view is made of little circles. As you zoom in and out, these are resized. Here's what it looks like now (zoomed out and zoomed in):
During a zoom the views for the circles are not redrawn, but just interpolated from their pre-zoom size. The redraw is postponed until the zoom finishes. Here's a capture of how that looks after a zoom in of several times:
It would be great to have the redrawing during zoom happen in a background thread so that the artefacts are less noticeable, but that's well out of the scope of this question and I've not worked on it yet either.
You can find the entire project, with fixes, on Bit Bucket so you can grab the files there.
The Bad Part – I don't know why it works
I was hoping that with this question answered, I'd have lots of new certainty about UIScrollView
zooming. I don't.
From what I've read about UIScrollView, this "fix" should not have made any difference and it should have already worked in the first place anyway.
A UIScrollView
isn't supposed to enable scrolling until you give it a delegate that implements viewForZoomingInScrollView:
, which I've not done.
Related Topics
Get Country Code from Country Name in iOS
Convert an Uiimage in a Texture
How to Add an Identifier to Auto Layout Constraints in Interface Builder
How Reliable Is Kvo with Uikit
Uilabel with Two Different Color Text
Best Way to Use Background Location Updates in iOS (Swift)
Switui - Two Navigationlink in a List
How to Render View into Image Faster
Xcode: Issue "File Xxx.Png Is Missing from Working Copy" at Project Building
Swiftui Drag Gesture Across Multiple Subviews
Error Itms-90086 Submitting App
Determine User's "Temperature Unit" Setting on iOS 10 (Celsius/Fahrenheit)
iOS 11 Uibarbuttonitem Images Not Sizing
Clang(Llvm) Compile with Frameworks