2013-03-06 53 views
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"); 
} 

我定製tile標籤

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

其中rotationToggle是靜態INT調用它。

如何刪除只有一個手勢識別器。當前方法刪除圖像視圖中的所有識別器。如果我只是 [imgView removeGestureRecognizer:rotate]; 或 [self.mainImgView removeGestureRecognizer:rotate];

它不起作用。提前致謝。讓我知道,如果我忘記在這裏提到的東西。

+0

PLZ忽略雙擊手勢操作 – CalZone 2013-03-06 23:06:01

回答

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]; 
} 
+1

感謝一大堆!它做到了。 – CalZone 2013-03-06 23:55:22