动画不会第一次运行

时间:2013-02-13 18:55:28

标签: ios xcode animation

我有一个奇怪的错误。当我的应用程序加载并点击按钮进行动画时,它不会提交动画,但是当我第二次尝试时它完美运行。继承我的代码:

-(void)Revert
{

    firstTable.hidden = FALSE;
    self.navigationItem.leftBarButtonItem = NO;
    [self returnanimation];
}

-(void)returnanimation
{
    CGRect labelframe = childnamelabel.frame;
    labelframe.origin.x = 493.5; // new x coordinate
    labelframe.origin.y = 359; // new y coordinate
    CGRect textframe = passwordfield.frame;
    textframe.origin.x = 454; // new x coordinate
    textframe.origin.y = 388; // new y coordinate
    CGRect submitframe = submit.frame;
    submitframe.origin.x = 640; // new x coordinate
    submitframe.origin.y = 387; // new y coordinate

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration: 0.25];
    childnamelabel.frame = labelframe;
    passwordfield.frame = textframe;
    submit.frame = submitframe;
    [UIView commitAnimations];
}

-(void)animation
{


    CGRect labelframe = childnamelabel.frame;
    labelframe.origin.x = 335.5; // new x coordinate
    labelframe.origin.y = 359; // new y coordinate
    CGRect textframe = passwordfield.frame;
    textframe.origin.x = 294; // new x coordinate
    textframe.origin.y = 388; // new y coordinate
    CGRect submitframe = submit.frame;
    submitframe.origin.x = 480; // new x coordinate
    submitframe.origin.y = 387; // new y coordinate
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration: 0.25];

    childnamelabel.frame = labelframe;
    passwordfield.frame = textframe;
    submit.frame = submitframe;
    [UIView commitAnimations];
}

-(IBAction)PressedTeacherButton: (UIBarButtonItem*) Teacherbutton
{
    [self setNameLabel: @"Mrs Hayward" Child:[NSNumber numberWithInt:0]];
    firstTable.hidden = TRUE;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(Revert)];
    [self animation];

    //Pass on admins name and also bool whether they are a child.
}

尝试寻找解决方案但找不到任何东西。谢谢你的帮助:D

* UPDATE

将它缩小到[self setNameLabel: @"Mrs Hayward" Child:[NSNumber numberWithInt:0]];似乎动画不喜欢在动画之前或之后更改标签。我已经尝试了完成功能,它会动画,然后在更改标签后立即重置为旧位置。

请求BUT提出setnamelabel方法:

- (void) setNameLabel:(NSString *)sender Child:(NSNumber *)Child
{
    self.childnamelabel.text = sender;
    IsAChild = Child;
}

仍然没有答案:(

1 个答案:

答案 0 :(得分:0)

虽然你的代码看起来不错,但我有一段时间也遇到了类似的问题。我转而使用了一个块,它起作用了。

尝试使用

-(void)animation
{
    CGRect labelframe = childnamelabel.frame;
    labelframe.origin.x = 335.5; // new x coordinate
    labelframe.origin.y = 359; // new y coordinate
    CGRect textframe = passwordfield.frame;
    textframe.origin.x = 294; // new x coordinate
    textframe.origin.y = 388; // new y coordinate
    CGRect submitframe = submit.frame;
    submitframe.origin.x = 480; // new x coordinate
    submitframe.origin.y = 387; // new y coordinate

    [UIView animateWithDuration:0.25 animations:^{
        childnamelabel.frame = labelframe;
        passwordfield.frame = textframe;
        submit.frame = submitframe;
    }];
}