UIView动画立即发生

时间:2015-06-03 20:00:55

标签: ios objective-c animation uiview

我有一个三个按钮的动画,这些按钮曾经正常工作,直到我为按钮添加了不同的可能位置。现在动画立即发生,但我不确定原因。

这里有一些代码:

defaults=[NSUserDefaults standardUserDefaults];
_btnCuenta3.hidden=NO;
CGPoint newPosition2;
CGPoint newPosition3;
CGPoint newPositionAgregar;
if([defaults objectForKey:@"cuenta3"]){
    newPosition2=CGPointMake(200.0f + _btnCuenta2.frame.size.width/2.0f, _btnCuenta2.center.y);
    newPosition3=CGPointMake(165.0f + _btnCuenta3.frame.size.width/2.0f, _btnCuenta3.center.y);
    newPositionAgregar=CGPointMake(130.0f + _btnAgregar.frame.size.width/2.0f, _btnAgregar.center.y);
    _btnCuenta2.center=newPosition2;
    _btnCuenta3.center=newPosition3;
    _btnAgregar.center=newPositionAgregar;
}else if([defaults objectForKey:@"cuenta2"]){
    newPosition2=CGPointMake(200.0f + _btnCuenta2.frame.size.width/2.0f, _btnCuenta2.center.y);
    newPositionAgregar=CGPointMake(165.0f + _btnAgregar.frame.size.width/2.0f, _btnAgregar.center.y);
    _btnCuenta2.center=newPosition2;
    _btnAgregar.center=newPositionAgregar;
}else{
    newPositionAgregar=CGPointMake(200.0f + _btnAgregar.frame.size.width/2.0f, _btnAgregar.center.y);
    _btnAgregar.center=newPositionAgregar;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
[UIView commitAnimations];

知道它为什么会发生或我如何解决它?

2 个答案:

答案 0 :(得分:3)

因为你没有动画任何东西。您只需更改即时发生的属性。做这样的事情

[UIView animateWithDuration:1.0 animations:^{
        if([defaults objectForKey:@"cuenta3"]){
    newPosition2=CGPointMake(200.0f + _btnCuenta2.frame.size.width/2.0f, _btnCuenta2.center.y);
    newPosition3=CGPointMake(165.0f + _btnCuenta3.frame.size.width/2.0f, _btnCuenta3.center.y);
    newPositionAgregar=CGPointMake(130.0f + _btnAgregar.frame.size.width/2.0f, _btnAgregar.center.y);
    _btnCuenta2.center=newPosition2;
    _btnCuenta3.center=newPosition3;
    _btnAgregar.center=newPositionAgregar;
}else if([defaults objectForKey:@"cuenta2"]){
    newPosition2=CGPointMake(200.0f + _btnCuenta2.frame.size.width/2.0f, _btnCuenta2.center.y);
    newPositionAgregar=CGPointMake(165.0f + _btnAgregar.frame.size.width/2.0f, _btnAgregar.center.y);
    _btnCuenta2.center=newPosition2;
    _btnAgregar.center=newPositionAgregar;
}else{
    newPositionAgregar=CGPointMake(200.0f + _btnAgregar.frame.size.width/2.0f, _btnAgregar.center.y);
    _btnAgregar.center=newPositionAgregar;
}

}];

答案 1 :(得分:2)

我认为在对uiview内容进行任何更改之前,[UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.0f]; //View changes code [UIView commitAnimations]; 应该是第一行。

代码:

{{1}}