Ios ui按钮随动画显示和消失
我想创建一个按钮,将显示只有5秒,然后消失。用户必须在按钮消失之前单击该按钮。所以我使用animationWithDuration将alpha设置为1,然后将其设置回0。这是我的密码Ios ui按钮随动画显示和消失,ios,animation,uibutton,Ios,Animation,Uibutton,我想创建一个按钮,将显示只有5秒,然后消失。用户必须在按钮消失之前单击该按钮。所以我使用animationWithDuration将alpha设置为1,然后将其设置回0。这是我的密码 __block UIButton *showButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)]; [showButton setTitle:@"go to next page!" forState:UIControlStateN
__block UIButton *showButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
[showButton setTitle:@"go to next page!" forState:UIControlStateNormal];
showButton.alpha = 0;
[showButton addTarget:self action:@selector(showButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton];
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
showButton.alpha = 1.0;
}
completion:nil];
[UIView animateWithDuration:0.5
delay:3.0
options: UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
animations:^{
showButton.alpha = 0.0;
}
completion:^(BOOL finished){
[showButton removeFromSuperview];
showButton = nil;
}];
-(void)showButtonClick{
NSLog(@"click")
}
但是showButtonClick不能被调用。我做错了什么?您应该创建一个NSTimer或其他东西来调用动画。因为两个动画同时被调用。这就是为什么您无法单击按钮,因此showButtonClick没有被调用 请尝试以下代码:
UIButton *showButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
[showButton setTitle:@"go to next page!" forState:UIControlStateNormal];
showButton.alpha = 0;
[showButton addTarget:self action:@selector(showButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton];
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
[showButton setHidden:NO];
showButton.alpha = 1.0;
}
completion: {
[UIView animateWithDuration:0.5
delay:0
options: UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
animations:^{
showButton.alpha = 0.0;
}
completion:^(BOOL finished){
[showButton setHidden:YES];
}];
}];
-(void)showButtonClick{
NSLog(@"click")
}
我不明白,为什么您要使用动画两次….因为您的按钮被分配为initWithFrame:CGRectMake(0,0,150,40)…请检查上面的动画代码….它会将按钮从0动画到100动画…动画完成后,按钮将被删除…您何时点击按钮?在按钮完全可见之后,或者在第一个动画中它是可见的?在按钮完全可见之后。拖延似乎不起作用。是的,你说得对。我在(dispatch_time(dispatch_time_NOW,(int64_t)(每秒5*NSEC_))、dispatch_get_main_queue()之后添加一个dispatch_;你知道为什么animateWithDuration:delay:optoins:animations:completion:method中的延迟参数仍然会导致动画阻止按钮单击吗?它仍然无法工作。我找到了答案。感谢您的回复:)
[UIView animateWithDuration:5.0
animations:^{
initWithFrame:CGRectMake(100, 0, 150, 40)
//Animation code goes here
} completion:^(BOOL finished) {
//Code to run once the animation is completed goes here
[your button remove from removefromsuperview];
}];