1

我想将自定义识别器的手势移除到图像视图。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");
}

我在自定义磁贴选项卡中调用它

 if(rotationToggle%2==0){
            [self enableRotation];
            NSLog(@"%d", rotationToggle);
        }
        else{
            [self removeRotationGestureFromView:self.mainImageView];
                            NSLog(@"%d", rotationToggle);
        }
        rotationToggle +=1;

其中rotationToggle 是一个静态整数。

如何删除只有一个手势识别器。当前方法删除图像视图中的所有识别器。如果我只是 [imgView removeGestureRecognizer:rotate]; 或 [self.mainImgView removeGestureRecognizer:rotate];

这没用。提前致谢。让我知道如果我忘了在这里提及一些事情。

4

1 回答 1

2

您正在创建一个新手势实例并尝试删除它,而不是删除之前添加的一个。保留前一个的引用并将其删除。

例如:-

@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];
}
于 2013-03-06T23:11:55.273 回答