Xcode如何在多次触摸后删除图像?

问题描述:

我使用此代码触摸'云'的事件动画。所有工作正常,但我想在用户点击三次云后淡入淡出/删除云。所以我希望它们在第三次触摸后消失。我该怎么做呢?Xcode如何在多次触摸后删除图像?

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

UITouch *touch = [touches anyObject]; 
CGPoint touchLocation = [touch locationInView:self.view]; 

CGRect cloudBLRect = [[[self.cloudBL layer] presentationLayer] frame]; 


if (CGRectContainsPoint(cloudBLRect, touchLocation)) { 
    NSLog(@"cloudBL tapped!"); 

    cloudBLPressed = true; 

    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationOptionCurveEaseInOut 
        animations:^{ 
         self.cloudBL.center = CGPointMake(200, 600); 
         self.cloudBL.alpha = 0.5; 


        } 
        completion:^(BOOL finished) { 
         [UIView animateWithDuration:2.0 
               delay:2.0 
              options: UIViewAnimationOptionCurveEaseInOut 
              animations:^{ 

               self.cloudBL.center = CGPointMake(100, 700); 
               self.cloudBL.alpha = 0.5; 


              } completion:^(BOOL finished) { 
               self.cloudBL.alpha = 1.0; 


              }]; 


    } else { 
      NSLog(@"cloud not tapped."); 
      return; 
         } 



if (cloudBLPressed) return; 

} 

以一个可变count并将其初始化为0。每个触摸,通过1.增加它在touchGesture方法还要检查,如果count变量等于2则设定为alpha0.0

事情是这样的:在 .m文件需要私人诠释vairiable:int count; in viewDidLoad: count = 0; cloudView.alpha = 1.0;

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
count++; 
if(count<2) 
{ 
    cloudView.alpha-=0.33; 
} 

else { 
    cloudView.alpha = 0.0; 
} 
       } 

它添加在动画中的逻辑。希望有所帮助。

您可以设置它在你的代码是这样的:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    CGRect cloudBLRect = [[[self.cloudBL layer] presentationLayer] frame]; 

    if(count < 2) 
    { 
     count++; 
    if (CGRectContainsPoint(cloudBLRect, touchLocation)) { 
     NSLog(@"cloudBL tapped!"); 

     cloudBLPressed = true; 


     [UIView animateWithDuration:1.0 
           delay:0.0 
          options:UIViewAnimationOptionCurveEaseInOut 
         animations:^{ 
          self.cloudBL.center = CGPointMake(200, 200); 
//        self.cloudBL.alpha -=0.33; 


         } 
         completion:^(BOOL finished) { 
          [UIView animateWithDuration:2.0 
                delay:2.0 
               options: UIViewAnimationOptionCurveEaseInOut 
               animations:^{ 

                self.cloudBL.center = CGPointMake(100, 300); 
                self.cloudBL.alpha -=0.33; 


               } completion:^(BOOL finished) { 
//             self.cloudBL.alpha = 1.0; 


               }]; 


         }]; 
    } 

     else { 
          NSLog(@"cloud not tapped."); 
          return; 
         } 



     if (cloudBLPressed) return; 

    } else { 
     [UIView animateWithDuration:2.0 
           delay:2.0 
          options: UIViewAnimationOptionCurveEaseInOut 
         animations:^{ 

          self.cloudBL.center = CGPointMake(100, 300); 
          self.cloudBL.alpha =0.0; 


         } completion:^(BOOL finished) { 
         }]; 
    } 
} 

你可以这样做:

UITapGestureRecognizer * tripleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourAction:)]; 

tripleTap.numberOfTapsRequired = 3; 

[yourView addGestureRecognizer:tripleTap]; 
+0

这需要用户轻按三图像(快) – damirstuhec

+0

不是设置单击手势和“UITapGestureRecognier”子类上的迭代器可能是一个解决方案。 – BriceB

+0

在游戏中,水龙头可能不会按正常顺序发生。玩家可能会按照1-2-1-3-2-3-3-2-1的顺序触摸3个云。你的解决方案只适用于1-1-1-2-2-2-3-3-3的情况,这是一种极不可能的情况,事件总是以不规则的方式发生。 – holex

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnImage:)]; 
[cloud addGestureRecognizer:tapRecognizer]; 

}

- (void)tapOnImage:(UITapGestureRecognizer *)gesture 
{ 
    tapsCounter++; 

    if (tapsCounter == 3) 
    { 
     // do your stuff 

     tapsCounter = 0; 
    } 
} 

我建议你这个逻辑进入UIImageView子类,代表对象。

适当的委托UIGestureRecognizerDelegate添加到您的接口。

然后在您的viewDidLoad

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMethod)]; 
tapped.delegate=self; 
tapped.numberOfTapsRequired = 3; 
[self.view addGestureRecognizer:tapped]; 

,然后在您的视图控制器:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{ 
    if (touch.view != cloudView&& cloudView) 
    { 
     return YES; 
    } 
    return NO; 
} 

-(void)tapMethod 
{ 
    [cloudView removeFromSuperview]; 
    cloudView = nil; 
}