1
votes

I want to make a UIView go off the screen upon a touch and come back after a small delay after touch up. So far:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if ([touch view] == myMainView)
    {
    [disappearingView.layer removeAllAnimations];   
    [UIView animateWithDuration:0.5
                          delay:0
         usingSpringWithDamping:0.7
          initialSpringVelocity:0.2
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^
     {
        disappearingView.transform = CGAffineTransformMakeScale(0, 0);

     }
                     completion:^(BOOL finished)
     {

     }];

    }
}

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if ([touch view] == myMainView)
    {
        [UIView animateWithDuration:0.5
                          delay:1
         usingSpringWithDamping:0.7
          initialSpringVelocity:0.2
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^
     {
        disappearingView.transform = CGAffineTransformMakeScale(1, 1);

     }
                     completion:^(BOOL finished)
     {

     }];

    }
}

Above code works fine so far. However, if the user lift the finger up and touch down again before the 1 second delay expires, disappearing view still comes back, even with the touch down. If you touch down and up randomly, animation is very inconsistent.

I want view to come back only after 1 second and there are no touches at all.

1

1 Answers

1
votes

Set a flag in touchesBegan and clear it again in touchesEnded. In your animation block for touchesEnded, check the flag and do not rescale to (1,1) if the flag is set. That way if you get a second touch after the first has ended but before the animation completes, it will not come back.