如何创建这样的场景:触摸屏幕,会有第一个视图,再次触摸屏幕,屏幕将显示ios中的第二个视图

时间:2016-05-11 07:19:41

标签: ios objective-c

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

   UITouch *touch = [touches anyObject];
  if (touch.tapCount == 1)
    {
      [UIView animateWithDuration:0.3 animations:^{
        UIImage *image=[UIImage imageNamed:@"view1.png"];
        imageView.image=image;
        [self.view addSubview:imageView];
    } completion:nil];

 }

else if(touch.tapCount == 2)
{
    [UIView animateWithDuration:0.3 animations:^{
        UIImage *image=[UIImage imageNamed:@"view2.png"];
        imageView.image=image;
        [self.view addSubview:imageView];
    } completion:nil];

  }

}

这是我的方式,但结果不是我想要的。  你必须双击,它会显示第二个视图,我也使用点击手势,但它也一样。

3 个答案:

答案 0 :(得分:0)

您可以要求点按手势numberOfTapsRequirednumberOfTouchesRequired。此外, (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 将获得任何触摸事件,包括UIButton触摸事件,但点击手势不能。

答案 1 :(得分:0)

创建Bool值全局

@property(nonatomic) BOOL isFirstViewAdded

 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

   UITouch *touch = [touches anyObject];
  if (!self.firstViewAdded)
    {
   self.firstViewAdded = true;
  [UIView animateWithDuration:0.3 animations:^{
    UIImage *image=[UIImage imageNamed:@"view1.png"];
    imageView.image=image;
    [self.view addSubview:imageView];
} completion:nil];

}

else if(self.firstViewAdded)
{
[UIView animateWithDuration:0.3 animations:^{
    UIImage *image=[UIImage imageNamed:@"view2.png"];
    imageView.image=image;
    [self.view addSubview:imageView];
} completion:nil];

 }

}

答案 2 :(得分:0)

正确使用Tap gesture recogniser来处理单击和双击。

UITapGestureRecognizer *singleTapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction::)];
singleTapGesture.numberOfTapsRequired=1;
[self.view addGestureRecognizer:_singleTap];

UITapGestureRecognizer *doubleTapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
doubleTapGesture.numberOfTapsRequired = 2;
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
[self.view addGestureRecognizer:_singleTap];


- (void)singleTapAction:(UITapGestureRecognizer *)gestureRecognizer {
    [UIView animateWithDuration:0.3 animations:^{
        UIImage *image=[UIImage imageNamed:@"view1.png"];
        imageView.image=image;
        [self.view addSubview:imageView];
    } completion:nil];

}

- (void)doubleTapAction:(UITapGestureRecognizer *)gestureRecognizer {
    [UIView animateWithDuration:0.3 animations:^{
        UIImage *image=[UIImage imageNamed:@"view2.png"];
        imageView.image=image;
        [self.view addSubview:imageView];
    } completion:nil];
}

希望这有帮助。