1

So I have an UICollectionView and I want the user to be able to pinch out or in to either expand and collapse a collection cell. I used this tutorial to perform the expanding and collapsing bit. Which works. I then added the code below to my collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath delegate method.

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchCollection:)];
[cell addGestureRecognizer:pinchGesture];

Then created the action pinchCollection: like this:

-(void)pinchCollection:(id)sender {
    UIPinchGestureRecognizer *gesture = (UIPinchGestureRecognizer*)sender;
    if (gesture.state == UIGestureRecognizerStateBegan) {        
        if (gesture.scale <= -1) { // I also changed this to be gesture.scale < 1 but it didn't work. 
            // pinch in
            [self collapseCollection];
            gesture.scale = 1;

        }
        if (gesture.scale >= 1) { // I changed this to be gesture.scale > 1 but it didn't work either. 
            // pinch out 
            [self expandCollection];
            gesture.scale = -1;
        }
    }
}

But only the pinching out code works. I have searched for a tutorial or code that refers how to do this properly but with no luck.

Expanding a collection looks like this:

enter image description here

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Souljacker
  • 774
  • 1
  • 13
  • 35
  • 2
    It sounds like you are using "programming by coincidence" to write your code. It is a very bad practice. You need to understand how your code works, not changing the code arbitrarily and think it may "fix the bug". "0 <= scale < 1" is shrink, not "scale <= -1". – Hai Feng Kao May 20 '13 at 03:26
  • Haha thanks @HaiFengKao! Pragmatic Programming. I figured it out before but I didn't update the post. Thanks anyways. – Souljacker May 24 '13 at 19:58

1 Answers1

0

This isn't the answer, but here is one thing missing:

[gesture setDelegate:self];

I'm pretty sure a negative value for the scale makes no sense. The scale is usually used as a percentage, normalized to a value between 0 and 1. I'd have to see your methods for collapsing and expanding the collection view; but, if you're using the gesture's scale property to resize the collection view, it's just a matter of fixing bad math.

If you're using the property value -1 as a way to determine pinch direction, then it's a simple matter of doing that correctly:

CGPoint p1 = [sender locationOfTouch:0 inView:self];
CGPoint p2 = [sender locationOfTouch:1 inView:self];
    
// Compute the new spread distance.
CGFloat xd = p1.x - p2.x;
CGFloat yd = p1.y - p2.y;
CGFloat distance = sqrt(xd*xd + yd*yd);

if (distance < previousDistance) {
// add collapse method call
} else {
// add expand method call
}

previousDistance = distance;

In this sample code, previousDistance is a global or static variable used for comparison against the newly calculated distance; obviously, if the distance is larger than the previous distance, then the user is trying to expand the collection view (and vice versa).

If none of this seems to be the problem, then please provide the code to your expand and collapse methods.

James Bush
  • 1,485
  • 14
  • 19