0

I can select the image, however the rotate/pinch functions are not working, this is the code;

in my viewDidLoad I init like;

[self initSingleTouchGesturesOnView: self.gestureView];
[self initMultiTouchGesturesOnView: self.gestureView];

Those methods are;

-(void) initSingleTouchGesturesOnView: (UIView *) targetView {
    //Pan
    bool found = FALSE;
    for (UIGestureRecognizer *recognizer in targetView.gestureRecognizers)
        if ([recognizer isKindOfClass: [UIPanGestureRecognizer class]])
            found = TRUE;
    if (!found) {
        UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget: self  action: @selector(pan:)] autorelease];
        [panRecognizer setDelegate: self];
        [targetView addGestureRecognizer: panRecognizer];
    }
}

-(void) initMultiTouchGesturesOnView: (UIView *) targetView {
    //Pinch
    bool found = FALSE;
    for (UIGestureRecognizer *recognizer in targetView.gestureRecognizers)
        if ([recognizer isKindOfClass: [UIPinchGestureRecognizer class]])
            found = TRUE;
    if (!found) {
        UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)] autorelease];
        [pinchRecognizer setDelegate:self];
        [targetView addGestureRecognizer:pinchRecognizer];
    }
    
    //Rotate
    found = FALSE;
    for (UIGestureRecognizer *recognizer in targetView.gestureRecognizers)
        if ([recognizer isKindOfClass: [UIRotationGestureRecognizer class]])
            found = TRUE;   
    if (!found) {
        UIRotationGestureRecognizer *rotationRecognizer = [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)] autorelease];
        [rotationRecognizer setDelegate:self];
        [targetView addGestureRecognizer:rotationRecognizer];
    }
}

For the gestures themselves;

#pragma mark - GestureDelegate

-(void)pan:(id)sender {
    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView: self.gestureView];
    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
        self.currentFaceView.dragFirstX = [self.currentFaceView center].x;
        self.currentFaceView.dragFirstY = [self.currentFaceView center].y;
    }
    
    translatedPoint = CGPointMake(self.currentFaceView.dragFirstX+translatedPoint.x, self.currentFaceView.dragFirstY+translatedPoint.y);
    [self.currentFaceView setCenter:translatedPoint];
}

-(void)scale:(id)sender {
    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
        self.currentFaceView.dragLastScale = 1.0;
        return;
    }
    
    CGFloat scale = 1.0 - (self.currentFaceView.dragLastScale - [(UIPinchGestureRecognizer*)sender scale]);
    CGAffineTransform currentTransform = self.currentFaceView.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    [self.currentFaceView setTransform:newTransform];
    self.currentFaceView.dragLastScale = [(UIPinchGestureRecognizer*)sender scale];
}

-(void)rotate:(id)sender {
    
    
    if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
        self.currentFaceView.dragLastRotation = 0.0;
        return;
    }
    
    CGFloat rotation = 0.0 - (self.currentFaceView.dragLastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
    CGAffineTransform currentTransform = self.currentFaceView.transform;
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
    [self.currentFaceView setTransform:newTransform];
    self.currentFaceView.dragLastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}

I also use;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}

But they do not seem to respond, no matter what, it's almost like when I am trying a gesture such as pinch, the screen 'taps' away, like I have lifted my fingers off without having done so.

DannyRoy
  • 81
  • 7
  • https://stackoverflow.com/a/41919444/793607 – HalR Jan 20 '22 at 19:02
  • [Code samples](//stackoverflow.com/help/mcve) should be *minimal*, complete and representative. Besides code, the problem should be clearly and completely described in English. Please read over "[ask]" for more guidelines. – outis Jan 30 '22 at 23:16

1 Answers1

0

So the solution in my case was actually changing this part of the code; It now works perfectly.

-(void) initSingleTouchGesturesOnView: (UIView *) targetView {
    
    //Pinch
    bool found = FALSE;
    for (UIGestureRecognizer *recognizer in targetView.gestureRecognizers)
        if ([recognizer isKindOfClass: [UIPinchGestureRecognizer class]])
            found = TRUE;
    if (!found) {
        UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)] autorelease];
        [pinchRecognizer setDelegate:self];
        [targetView addGestureRecognizer:pinchRecognizer];
    }
    
    //Rotate
    found = FALSE;
    for (UIGestureRecognizer *recognizer in targetView.gestureRecognizers)
        if ([recognizer isKindOfClass: [UIRotationGestureRecognizer class]])
            found = TRUE;
    if (!found) {
        UIRotationGestureRecognizer *rotationRecognizer = [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)] autorelease];
        [rotationRecognizer setDelegate:self];
        [targetView addGestureRecognizer:rotationRecognizer];
    }
    
    found = FALSE;
    for (UIGestureRecognizer *recognizer in targetView.gestureRecognizers)
        if ([recognizer isKindOfClass: [UIPanGestureRecognizer class]])
            found = TRUE;
    if (!found) {
        UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget: self  action: @selector(pan:)] autorelease];
        [panRecognizer setDelegate: self];
        [targetView addGestureRecognizer: panRecognizer];
    }
}
DannyRoy
  • 81
  • 7
  • [Code-only](//meta.stackoverflow.com/q/392712/90527) answers aren't very helpful. Please include a description of how the code works, and why it answers the question. – outis Jan 30 '22 at 23:17