0
votes

I have an app that has a collectionView inside a scrollView. I am using the following code to be able to zoom in and pan around.

-(void) viewDidLoad {
    [super viewDidLoad];

    twoFingerPinchA = [[UIPinchGestureRecognizer alloc]
                                                initWithTarget:self
                                                action:@selector(twoFingerPinch:)]
    ;
    [self.view addGestureRecognizer:(twoFingerPinchA)];
}

-(void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer{

  CGAffineTransform transform = CGAffineTransformMakeScale(twoFingerPinchA.scale, twoFingerPinchA.scale);

    if (twoFingerPinchA.scale < 1) {
        twoFingerPinchA.scale = 1;
    }

    if (twoFingerPinchA.scale >2) {
        twoFingerPinchA.scale = 2;
    }
    self.scrolling.transform = transform;

}

This works well except when I am zoomed in and remove my fingers. When I put my two fingers back on the screen to zoom out or in again the view returns to not zoomed in. How can I get the view to stay zoomed in when I replace my two fingers on the screen to rezoom. I have tried capturing the twoFingerPinchA.scale value which I can do. But I don't know how to set the initial value of the twoFingerPinchA.scale to that value as it alway returns to 1.

Any ideas?

So I am now trying to detect when two fingers touch the screen and when the two finger touch stops so I can save and insert the value of the twoFingerPinchA.scale.

However No matter what I put in I can't seem to detect the touches.

I have my controllers regular view, then I have a scroll view in it and then a collectionView inside of it.

I have enabled interaction and multiple touch for all views and tried to detect the touches in all view but don't get a call back. Here is my code.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Event:%@",event);
// I don't even get this call back as a touch beginning on any view.
    if ([[event touchesForView:self.view] count] >0) {
        NSLog(@"touches");
    }
}
1

1 Answers

0
votes

So I figured this out.

First I used this code

-(void) viewWillAppear:(BOOL)animated{
    NSString *pinchString = @"1.0";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:pinchString forKey:@"pinchValue"];
  }

//this was to make sure that the pinchValue key had a value when the app is first installed.

then I put this

switch ([twoFingerPinchA state]) {
        case UIGestureRecognizerStatePossible:
        {
        }
            break;
        case UIGestureRecognizerStateBegan:
        {
            NSString *inPinch = [[NSUserDefaults standardUserDefaults] objectForKey:@"pinchValue"];
            float Pinch = [inPinch floatValue];
           twoFingerPinchA.scale = Pinch;
         }
            break;
        case UIGestureRecognizerStateChanged:
        {
         }
            break;
        case UIGestureRecognizerStateEnded:
        {
            NSString *pinchString = [NSString  stringWithFormat:@"%f",twoFingerPinchA.scale];
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:pinchString forKey:@"pinchValue"];
          }
            break;
        case UIGestureRecognizerStateCancelled:
        {
        }
            break;
        case UIGestureRecognizerStateFailed:
        {
        }
            break;
    }

in my -(void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer{}

This stores the value of the twoFingerPinchA.scale in usersDefaults when the pinch ends

and

sets the twoFingerPinchA.scales to that stored value when it begins again. From there it adjust up and down /in and out according to this value.

Works great