动画期间隐藏按钮

时间:2014-05-14 18:49:22

标签: ios animation

此刻我的代码随机地在容器周围移动了一个按钮。我想要的是它出现,然后消失,然后出现在其他地方。但它一直是可见的。

如何重新定位动画之间,如何将其隐藏在动画之间?

-(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;
}

3 个答案:

答案 0 :(得分:0)

您可以使用self.button.hidden = YES;隐藏它。将隐藏设置为NO以再次显示它。

答案 1 :(得分:0)

我不确定我是否正确理解了你的问题,但如果你想让UIButton消失然后出现在不同的位置,你可以尝试这样做

[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;
          }];
      }
  }];

答案 2 :(得分:-1)

我使用NSTimer来移动按钮。每一秒它都会移动。