Question

I have my code randomly moving a button around a container at the moment. What I want is for it to appear, then disappear, then appear somewhere else. But it is visible the whole time.

How can I make it hidden in between animations, whilst it is being re-positioned?

-(void)animationLoop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    self.button.hidden = NO;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];

    CGFloat x = (CGFloat) (arc4random() % (int) self.container.bounds.size.width);
    CGFloat y = (CGFloat) (arc4random() % (int) self.container.bounds.size.height);

    CGPoint squarePostion = CGPointMake(x, y);
    _button.center = squarePostion;
    // add:
    [UIView setAnimationDelegate:self]; // as suggested by @Carl Veazey in a comment
    [UIView setAnimationDidStopSelector:@selector(animationLoop:finished:context:)];

    [UIView commitAnimations];
    self.button.hidden = YES;
}
Was it helpful?

Solution 3

I used an NSTimer to move the button instead. Every second it moved.

OTHER TIPS

You can use self.button.hidden = YES; to hide it. Set hidden to NO to show it again.

I'm not sure if I understood your question correctly but if you want a UIButton to disappear and then appear at different position you can try do it like that

[UIView animateWithDuration:1.0 animations:^{
     self.button.alpha = 0;
} completion:^(BOOL finished) {
     if (finished) {
         CGFloat x = (CGFloat) (arc4random() % (int) self.container.bounds.size.width);
         CGFloat y = (CGFloat) (arc4random() % (int) self.container.bounds.size.height);
         CGPoint squarePostion = CGPointMake(x, y);
         self.button.centre = squarePosition;
         [UIView animateWithDuration:1.0 animations:^{
              self.button.alpha = 1.0;
          }];
      }
  }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top