Ios 在动画期间隐藏按钮
我的代码在容器周围随机移动一个按钮。我想要的是它出现,然后消失,然后出现在其他地方。但它始终可见 在重新定位时,如何将其隐藏在动画之间Ios 在动画期间隐藏按钮,ios,animation,Ios,Animation,我的代码在容器周围随机移动一个按钮。我想要的是它出现,然后消失,然后出现在其他地方。但它始终可见 在重新定位时,如何将其隐藏在动画之间 -(void)animationLoop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { self.button.hidden = NO; [UIView beginAnimations:nil context:nil]; [U
-(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;
}
您可以使用
self.button.hidden=YES代码>以隐藏它。将“隐藏”设置为“否”以再次显示该按钮。我使用了NSTimer来移动按钮。它每秒钟都在移动。我不确定我是否正确理解了你的问题,但如果你想让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;
}];
}
}];
当我用它的时候,它似乎把它藏起来了,然后又不带回来了。我已编辑代码以反映您的建议。你知道它为什么不回来吗?因为你在函数开始时将hidden设置为NO,然后在函数结束时将hidden=YES。您需要将此代码放在Beginanimation和Committeanimation之间。更好的是,您应该使用UIView动画块,您使用的代码早已被块取代。我已将它们移到begin和commit之间,并将其切换,现在它一点也不隐藏。始终可见。我是iOS开发新手,你所说的UIView动画块是什么意思?请看[UIView animateWithDuration:delay:options:animations:completion]