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.
Zoomed UIImage inside ScrollView inside CollectionViewCell
Create UICollectionViewCell subclass with xib
Subclassing UICollectionViewCell and initialising from xib
or without xib
http://randexdev.com/2014/08/uicollectionviewcell/
then set delegate for scrollView is collectionViewCell subclass
Add UIScrollViewDelegate code to cell
You can access imageView from cell property
Reset Scrollview in UICollectionViewCell after scrolling
you need to implement this method: https://developer.apple.com/documentation/uikit/uicollectionviewdelegate/1618087-collectionview
and zoom out the scrollView before displaying the cell. This method will handle your case. Happy coding ;)
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath)
Related Topics
How to Integrate Nsurlconnection with Uiprogressview
Uicollectionview Horizontal Paging Not Centered
Getting Video Snapshot for Thumbnail
Editing Screenshots in Itunes Connect After iOS App Was Approved
How to Deselect a Segment in Segmented Control Button Permanently Till Its Clicked Again
Auto Layout Constraint on Calayer iOS
Xcode Is Creating Generic Xcode Archive Instead of iOS App Archive
Slservicetypefacebook Setinitialtext Is Not Working
Negative Spacer for Uibarbuttonitem in Navigation Bar on iOS 11
Fbsopenapplicationerrordomain Code =4 Error
Is It Necessary to Use [Unowned Self] in Closures of Uiview.Animatewithduration(...)
Connect to Vpn Programmatically in iOS 8
How to Delete App Build in New Itunes Connect Site
How to Set a Default Value of a Uipickerview