How to zoom a UIScrollView inside of a UICollectionViewCell? - ios

I'm trying to add a UIScrollView inside of a UICollectionViewCell. The idea is that you can use pinch to zoom the UIScrollView (and with it, the image within), but the scrollview doesn't seem to handle any gesture. I'm guessing they are being caught by the UICollectionView.
I've set the delegate of the UIScrollView to be the UICollectionViewCell, but none of the delegate methods are being called.
EDIT:
I've made a github repo with the code (simplified as much as I could). Even though it's just a few lines of code, I cannot see what I did wrong.
EDIT2: After the answer was found, I added the fixes to the above-mentioned repo, hope others find it helpful too :)
https://github.com/krummler/gallery-pinchzoom-example

I just made an implementation for SWIFT 3 on iOS 9.3+ and all i done was:
1. Place the image view inside a scrollview
2. Connect the scrollview delegate to collectionview cell class
3. Implement the code below on the collectionview subclass
class FullScreenImageTextDetailCollectionViewCell: UICollectionViewCell, UIScrollViewDelegate {
#IBOutlet var scrollview: UIScrollView!
#IBOutlet weak var backgroundImageView: UIImageView!
override func awakeFromNib() {
self.scrollview.minimumZoomScale = 0.5
self.scrollview.maximumZoomScale = 3.5
self.scrollview.delegate = self
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.backgroundImageView
}
}
No gesture recognizer adding or removing on the parent collectionview controller was necessary, worked like a charm!
Thanks for previous examples for reaching this!

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.

Please add scrollview in your cell and your current cell image view add in scrollview.
then use below code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ImageContext *context = [self.images objectAtIndex:indexPath.row];
ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"imageCell" forIndexPath:indexPath];
cell.cellScrollView.autoresizesSubviews = YES;
cell.cellScrollView.multipleTouchEnabled =YES;
cell.cellScrollView.maximumZoomScale = 4.0;
cell.cellScrollView.minimumZoomScale = 1.0;
cell.cellScrollView.clipsToBounds = YES;
cell.cellScrollView.delegate = self;
cell.cellScrollView.zoomScale = 1.0;
[cell.imageView setImage:[UIImage imageNamed:context.thumbImageUrl]];
return cell;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{ NSLog(#"%i",scrollView.subviews.count);
for (UIView *v in scrollView.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
return v;
}
}
}

Check if all related view's multitouch is on. iOS disabled multitouch by default on most views to save energy.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (touch.view == [self myScrollView]) //<- whatever your scrollView is called
{
return YES;
}
return NO;
}
Don't know your code but try playing around with the above code to see if you can filter your touches to the objects you want. The above code is from the UIGestureRecognizerDelegate Protocol Reference.

The delegates are not getting called because you added it inside the UICollectionview.The touch events are availabel for the collection view which is the superview and hence not obtainable by the view inside.You may have to think of some other way to achieve this model.
UICollectionView is having the same pattern as UITableView,The problem occours in tableview inside scrollview ,that the touch event is accepted by scrollview[which is the superview] and to make the tableview scrollable in that case ,The scroll of scrollview must be disabled.This is another version of that problem
The apple docs says that the above said case make unpredictable results.So This may be same for your problem.
In my opinion you must go for better design which can achieve what you look for

I've set the delegate of the UIScrollView to be the UICollectionViewCell, but none of the delegate methods are being called.
For that just define one function in collectionViewCell
#IBOutlet weak var mainScrollView:UIScrollView!
func setup(){
mainScrollView.minimumZoomScale=1
mainScrollView.maximumZoomScale=10.0
mainScrollView.delegate=self
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return yourimageviewinside of scrollview
}
call this fuction in
collectionview(_,cellForItem ) method of collectionview

Related

Call a method on UIImageView inside of a UITableViewCell

I have a a custom UITableViewCell class and in it I have a UIImageView. This UIImageView is only displayed when the text of the UITableViewCell is equal to a certain string, eg "Home". When the method mapView:regionWillChangeAnimated: is called, I want to apply an animation on this UIImageView.
My question is how can I, when mapView:regionWillChangeAnimated: is called, go through my UITableView and custom UITableViewCells and perform an animation on the UIImageView within the cells which have the UIImageView set as visible?
I've been able to call methods on cells before using the UITableView delegate methods but I'm struggling to find a solution in this scenario because the the UITableView is not being directly called by the map panning method, it's not a didSelectRowAtIndex path issue?
Thank you.
If you have reference to table view in your map delegate method, you can get all visible cells using visibleCells method in UITableView and apply animation for them if needed. General structure may look like:
// Controller
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
...
[self applyCellsAnimation];
}
- (void)applyCellsAnimation {
for (CustomCell* cell in [self.tableView visibleCells]) {
[cell performImageAnimationIfNeeded];
}
}
// CustomCell
- (void)performImageAnimationIfNeeded {
if (!self.myImageView.hidden) {
// perform animation
}
}
You can get an array of all the visible rows in your table view using
[myTableView visibleCells]
You then just need to integrate through each cell and animate the image.
i.e.
for (MyCustomCell* cell in [myTableView visibleCells])
{
[cell animateImage];
}
The implementation of animateImage can then do whatever you want - just check the imageView.hidden property and animate it if the imageView is visible.

My gesture recognizer is attached to the wrong view

I have a UICollectionView that has elements that can be dragged and dropped around the screen. I use a UILongPressGestureRecognizer to handle the dragging. I attach this recognizer to the collection view cells in my collectionView:cellForItemAtIndexPath: method. However, the recognizer's view property occasionally returns a UIView instead of a UICollectionViewCell. I require some of the methods/properties that are only on UICollectionViewCell and my app crashes when a UIView is returned instead.
Why would the recognizer that is attached to a cell return a plain UIView?
Attaching the recognizer
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
EXSupplyCollectionViewCell *cell = (EXSupplyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:nil];
longPressRecognizer.delegate = self;
[cell addGestureRecognizer:longPressRecognizer];
return cell;
}
Handling the gesture
I use a method with a switch statement to dispatch the different states of the long press.
- (void)longGestureAction:(UILongPressGestureRecognizer *)gesture {
UICollectionViewCell *cell = (UICollectionViewCell *)[gesture view];
switch ([gesture state]) {
case UIGestureRecognizerStateBegan:
[self longGestureActionBeganOn:cell withGesture:gesture];
break;
//snip
default:
break;
}
}
When longGestureActionBeganOn:withGesture is called if cell is actually a UICollectionViewCell the rest of the gesture executes perfectly. If it isn't then it breaks when it attempts to determine the index path for what should be a cell.
First occurrence of break
- (void)longGestureActionBeganOn:(UICollectionViewCell *)cell withGesture:(UILongPressGestureRecognizer *)gesture
{
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell]; // unrecognized selector is sent to the cell here if it is a UIView
[self.collectionView setScrollEnabled:NO];
if (indexPath != nil) {
// snip
}
}
I also use other properties specific to UICollectionViewCell for other states of the gesture. Is there some way to guarantee that the recognizer will always give me back the view that I assigned it to?
Views like UICollectionView and UITableView will reuse their cells. If you blindly add a gestureRecognizer in collectionView:cellForItemAtIndexPath: you will add a new one each time the cell is reloaded. If you scroll around a bit you will end up with dozens of gestureRecognizers on each cell.
In theory this should not cause any problems besides that the action of the gestureRecognizer is called multiple times. But Apple uses heavy performance optimization on cell reuse, so it might be possible that something messes up something.
The preferred way to solve the problem is to add the gestureRecognizer to the collectionView instead.
Another way would be to check if there is already a gestureRecognizer on the cell and only add a new one if there is none. Or you use the solution you found and remove the gestureRecognizer in prepareForReuse of the cell.
When you use the latter methods you should check that you remove (or test for) the right one. You don't want to remove gestureRecognizers the system added for you. (I'm not sure if iOS currently uses this, but to make your app proof for the future you might want to stick to this best practice.)
I had a similar problem related to Long-Touch.
What I ended up doing is override the UICollectionViewCell.PrepareForReuse and cancel the UIGestureRecognizers attached to my view. So everytime my cell got recycled a long press event would be canceled.
See this answer

Make part of an UICollectionViewCell not clickable

Basically what i want to do is to have a cell in a UICollectionView, which part of it will respond to tap, and part of it won't.
This cell will be a normal cell that is expanded when tapped, showing a subview on the expanded area that doesn't respond to tap.
I considered expanding the cell and adding a subview above it, that wouldn't be clickable. But i think that is kind of a "bad" way of accomplishing it.
Is there a best way of doing this?
This is what i want to do:
Any help is appreciated, thanks!
You need subclass UICollectionViewCell, then override hitTest:withEvent rather than pointInside:withEvent, inside hitTest:withEvent: you should define the targetRect, then check if the point user clicked is inside of targetRect.
Here is the code which works on my tableViewCell (similar for collectionViewCell).
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
CGRect targetRect = CGRectMake(0, 0, CGRectGetWidth(self.bounds), 20);
if (CGRectContainsPoint(targetRect, point)) {
return nil;
} else {
return [super hitTest:point withEvent:event];
}
}
With in the selector
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
you must return a cell, and in this function you should determine whether or not the cell is clickable and then simply do
cell.userInteractionEnabled = shouldAllowTouch;

Cannot reposition ImageView in collectionView:cellForItemAtIndexPath:

I have an ImageView, say imageViewA, in a UICollectionViewCell, say ComicStripViewCellA.xib. I need to reposition imageViewA if a condition is true. I can access imageViewA or change its alpha value in collectionView:cellForItemAtIndexPath, but cannot reposition it if this is the first time that this viewCell is dequeued.
But if this viewCell is already in the pool, I can reposition it the next time when collectionView:cellForItemAtIndexPath is executed.
-(UICollectionView *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
id <ComicStripViewCell> viewCell = [collectionView dequeueReusableCellWithReuseIdentifier:#"comicStripCell" forIndexPath:indexPath];
if (condition) {
viewCell.imageViewA.alpha = 0.5; // Always work.
[viewCell reposition:YES]; // Only work if viewCell is already in the pool.
}
return (UICollectionView *)viewCell;
}
The following routine is in ComicStripViewCellA.m:
-(void)reposition:(BOOL)flag
{
if (flag) {
CGPoint center = _imageViewA.center;
center.x -= 60;
_imageViewA.center = center;
}
}
I guess it has something to do with the view cell initialization not complete when the reposition routine is run, but I am not sure.
Any help is appreciated!
Thanks!
I've been experimenting around, it would appear that the UICollectionViewCell outlets can't be repositioned programmatically (or at least I couldn't get them to reposition, no matter what). One possible solution, however, is to have a simple UIView as the only outlet, and to add subviews to that outlet programmatically, in your case the UIImageView. Once you do that, you can freely reposition any subview in your UIView outlet. I hope this helps!

voice over can only see a page of a uicollectionview

So i have a UICollectionView with a set of UICollectionViewCells displayed using a custom UILayout.
I've configured the UILayout to lay out all the UICollectionViewCells almost exactly the same as how they are laid out in the photos app on ios.
The problem is, it seems when voice over is turned on, and the user is traversing through the UICollectionViewCells using swipe, when the user gets to the last visible cell on the page, and tries to swipe forward to the next cell, it simply stops.
I know that in UITableView the cells will just keep moving forward, and the table view will scroll down automatically.
Does anyone know how to get this behaviour?
This answer worked for me, too. Thanks!
There is one other call you must have enabled to get this to work. Otherwise your method (void)accessibilityElementDidBecomeFocused will never get called. You must enable accessibility on the object Cell.
Option 1: In ViewController, set the cell instance to have accessibility.
Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
[cell setIsAccessibilityElement:YES];
Option 2: Implement the accessibility interface in the cell object:
- (BOOL)isAccessibilityElement
{
return YES;
}
- (NSString *)accessibilityLabel {
return self.label.text;
}
- (UIAccessibilityTraits)accessibilityTraits {
return UIAccessibilityTraitStaticText; // Or some other trait that fits better
}
- (void)accessibilityElementDidBecomeFocused
{
UICollectionView *collectionView = (UICollectionView *)self.superview;
[collectionView scrollToItemAtIndexPath:[collectionView indexPathForCell:self] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally|UICollectionViewScrollPositionCenteredVertically animated:NO];
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, self);
}
After hours and hours of headache, the solution was really simple. If anyone else comes across a similar problem, this is what i did:
In the subclass of UICollectionViewCell that you are using for your CollectionView, override accessibilityElementDidBecomeFocused and implement it like this:
- (void)accessibilityElementDidBecomeFocused
{
UICollectionView *collectionView = (UICollectionView *)self.superview;
[collectionView scrollToItemAtIndexPath:[collectionView indexPathForCell:self] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally|UICollectionViewScrollPositionCenteredVertically animated:NO];
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
}
Stephen's answer worked for me! Thanks.
I want to add that this seems to affect only iOS6; it looks like they fixed it in iOS7.
Also, you can make the scrolling slightly faster and cleaner by passing self instead of nil to UIAccessibilityPostNotification -- like so:
- (void)accessibilityElementDidBecomeFocused {
UICollectionView *collectionView = (UICollectionView *)self.superview;
[collectionView scrollToItemAtIndexPath:[collectionView indexPathForCell:self] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally|UICollectionViewScrollPositionCenteredVertically animated:NO];
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, self);
}
This is how you would do it in Swift:
override func accessibilityElementDidBecomeFocused() {
guard let collectionView = superview as? UICollectionView,
let indexPath = collectionView.indexPath(for: self) else {
return
}
collectionView.scrollToItem(at: indexPath, at: .left, animated: true)
UIAccessibility.post(notification: .layoutChanged, argument: nil)
}

Resources