Objective c 设置隐藏和取消隐藏按钮的动画
因此,我有一个Objective c 设置隐藏和取消隐藏按钮的动画,objective-c,animation,uibutton,Objective C,Animation,Uibutton,因此,我有一个ui按钮,当UITextField处于编辑模式时,它将被隐藏和取消隐藏。问题是,它的变化非常好(从隐藏变为未隐藏),但没有动画效果。我尝试了setAlpha:,但这只在它将alpha设置为0到100,而不是100到0时起作用。以下是我目前的代码: -(BOOL) textFieldShouldBeginEditing:(UITextField *)textField { negButton = [UIButton buttonWithType:UIButtonTypeRounded
ui按钮
,当UITextField
处于编辑模式时,它将被隐藏和取消隐藏。问题是,它的变化非常好(从隐藏变为未隐藏),但没有动画效果。我尝试了setAlpha:
,但这只在它将alpha设置为0到100,而不是100到0时起作用。以下是我目前的代码:
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
negButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
negButton.frame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 37, textField.frame.size.height);
[negButton setHidden:YES];
return YES;
}
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField isEditing])
{
[UIView animateWithDuration:0.3 animations:^
{
CGRect frame = textField.frame;
frame.size.width -= 40;
frame.origin.x += 40;
[negButton setHidden:NO];
[textField setFrame:frame];
[self.view addSubview:negButton];
}];
}
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.3 animations:^
{
CGRect frame = textField.frame;
frame.size.width += 40;
frame.origin.x -= 40;
[negButton setHidden:YES];
[negButton removeFromSuperview];
[textField setFrame:frame];
}
];
}
编辑:我解决了这个问题。我不需要调用
removeFromSuperview
函数,我必须从hidden切换到alpha。(见下面的@David的答案)你的动画有问题。将其更改为以下内容:
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
negButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
negButton.frame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 37, textField.frame.size.height);
[negButton setAlpha:0];
[self.view addSubView:negButton];
return YES;
}
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField isEditing])
{
CGRect frame = textField.frame;
frame.size.width -= 40;
frame.origin.x += 40;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[negButton setAlpha:1];
[textField setFrame:frame];
[UIView commitAnimations];
}
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
CGRect frame = textField.frame;
frame.size.width += 40;
frame.origin.x -= 40;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[negButton setAlpha:0];
[textField setFrame:frame];
[UIView commitAnimations];
[self performSelector:@selector(removeBtn) withObject:negButton afterDelay:0.3];
}
- (void)removeBtn:(UIButton*)button
{
[button removeFromSuperView];
}
您正在立即从视图中删除按钮,而不是在按钮淡出后将其删除
干杯 你好,大卫,谢谢你的帮助,但是我尝试了这个方法,但是按钮都没有出现。你把所有隐藏的东西都改成了alphas吗?咳嗽,这是我在回答的最后贴的。我知道,但是你的代码不起作用。我在我的项目中发布了完全相同的代码,但什么也没发生。不过,谢谢你指出这个问题!代码中没有添加按钮作为子视图!