I want to remove gesture a custom recognizer to an imageview. It is one finger rotation from kerby turner.
-(void) enableRotation{
[[self mainImageView] setUserInteractionEnabled:YES];
[self addRotationGestureToView:[self mainImageView]];
//[self addTapGestureToView:[self mainImageView] numberOfTaps:1];
}
- (void)addTapGestureToView:(UIView *)view numberOfTaps:(NSInteger)numberOfTaps
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tap setNumberOfTapsRequired:numberOfTaps];
[view addGestureRecognizer:tap];
}
- (void)addRotationGestureToView:(UIView *)view
{
NSLog(@"'Adding KT Rotation recognizer to the rotation");
KTOneFingerRotationGestureRecognizer *rotation = [[KTOneFingerRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotating:)];
[view addGestureRecognizer:rotation];
}
- (void)rotating:(KTOneFingerRotationGestureRecognizer *)recognizer
{
UIView *view = [recognizer view];
[view setTransform:CGAffineTransformRotate([view transform], [recognizer rotation])];
}
- (void)removeRotationGestureFromView: (UIImageView *) imgView {
//UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationRemoved:)];
//[imgView removeGestureRecognizer:rotate];
KTOneFingerRotationGestureRecognizer *rotation = [[KTOneFingerRotationGestureRecognizer alloc] init];
while(self.mainImageView.gestureRecognizers.count){
[self.mainImageView removeGestureRecognizer:[self.mainImageView.gestureRecognizers objectAtIndex:0]];
//[self.mainImageView removeGestureRecognizer:rotation];
}
NSLog(@"Trying to remove rotation on this gesture");
}
I'm calling it in a custom tile tab
if(rotationToggle%2==0){
[self enableRotation];
NSLog(@"%d", rotationToggle);
}
else{
[self removeRotationGestureFromView:self.mainImageView];
NSLog(@"%d", rotationToggle);
}
rotationToggle +=1;
where rotationToggle is a static int.
How can I remove the just one gesture recognizer. The current method removes all recognizers in the image view. if I do just [imgView removeGestureRecognizer:rotate]; OR [self.mainImgView removeGestureRecognizer:rotate];
It does not work. Thanks in advance. Let me know if I forgot to mention something here.
You are creating an instance of new gesture and trying to remove that instead of removing the previously added one. Keep the reference of previous one and remove that.
For eg:-
@property (nonatomic, strong) KTOneFingerRotationGestureRecognizer *rotation;
- (void)addRotationGestureToView:(UIView *)view
{
self.rotation = [[KTOneFingerRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotating:)];
[view addGestureRecognizer:rotation];
}
- (void)removeRotationGestureFromView:(UIImageView *)imgView {
[self.mainImageView removeGestureRecognizer:self.rotation];
}