0

我为我的按钮实现了一个动画以滑入屏幕。如果按钮已经在屏幕内,我希望我的按钮先滑到外面,然后再回来。

然而,第一个动画(当按钮已经滑入屏幕时)被跳过,因此按钮只是立即跳出屏幕而不是向后滑动。

我希望他们慢慢地滑到外面,然后再慢慢地滑回来。

它有点像setAnimationDurationif 子句的内部被忽略:

[self.buttonInfoFrame setAlpha:1.0];
[self.buttonSetCourse setAlpha:1.0];
[self.buttonSetCourse setEnabled:YES];

if(![self.selected isEqual: @""])
{
    CGRect frame = self.buttonInfoFrame.frame;
    frame.origin.x = 1050;
    frame.origin.y = 150;

    CGRect frame2 = self.buttonSetCourse.frame;
    frame2.origin.x = 1050;
    frame2.origin.y = 526;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];

    self.buttonInfoFrame.frame = frame;
    self.buttonSetCourse.frame = frame2;

    [UIView commitAnimations];
}

CGRect frame = self.buttonInfoFrame.frame;
frame.origin.x = 757;
frame.origin.y = 150;

CGRect frame2 = self.buttonSetCourse.frame;
frame2.origin.x = 747;
frame2.origin.y = 526;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];

self.buttonInfoFrame.frame = frame;
self.buttonSetCourse.frame = frame2;

[UIView commitAnimations];
4

3 回答 3

1

让我们简化代码:

[self.buttonInfoFrame setAlpha:1.0];
[self.buttonSetCourse setAlpha:1.0];
[self.buttonSetCourse setEnabled:YES];

CGRect frame = self.buttonInfoFrame.frame;
CGRect frame2 = self.buttonSetCourse.frame;

void (^firstAnimation)() = ^{
   frame.origin.x = 1050;
   frame.origin.y = 150;
   self.buttonInfoFrame.frame = frame;

   frame2.origin.x = 1050;
   frame2.origin.y = 526;
   self.buttonSetCourse.frame = frame2;
};

void (^secondAnimation)() = ^{
   frame.origin.x = 757;
   frame.origin.y = 150;
   self.buttonInfoFrame.frame = frame;

   frame2.origin.x = 747;
   frame2.origin.y = 526;
   self.buttonSetCourse.frame = frame2;
};

void (^onCompletion)(BOOL) = ^(BOOL finished) {
    [UIView animateWithDuration:1.0f
                     animations:secondAnimation];
};

if(![self.selected isEqual: @""]) {
    [UIView animateWithDuration:1.0f
                     animations:firstAnimation
                     completion:onCompletion];

}
else {
    onCompletion(YES);
}
于 2013-05-26T15:08:33.033 回答
1

在第一个动画的完成块中编写您的第二个动画!

编辑

   if(![self.selected isEqual: @""])
    {
        CGRect frame = self.buttonInfoFrame.frame;
        frame.origin.x = 1050;
        frame.origin.y = 150;

        CGRect frame2 = self.buttonSetCourse.frame;
        frame2.origin.x = 1050;
        frame2.origin.y = 526;

        [UIView animateWithDuration:1,0 delay:0.0 options:option
            animations:^{
                self.buttonInfoFrame.frame = frame;
                self.buttonSetCourse.frame = frame2;
            }
            completion:^(BOOL finished){
                CGRect frame = self.buttonInfoFrame.frame;
                frame.origin.x = 757;
                frame.origin.y = 150;

                CGRect frame2 = self.buttonSetCourse.frame;
                frame2.origin.x = 747;
                frame2.origin.y = 526;

                [UIView animateWithDuration:1.0 delay:0.0 options:option
                    animations:^{
                        self.buttonInfoFrame.frame = frame;
                        self.buttonSetCourse.frame = frame2;
                    }
                    completion:nil];
        }];

} else {

    CGRect frame = self.buttonInfoFrame.frame;
    frame.origin.x = 757;
    frame.origin.y = 150;

    CGRect frame2 = self.buttonSetCourse.frame;
    frame2.origin.x = 747;
    frame2.origin.y = 526;

    [UIView animateWithDuration:1.0 delay:0.0 options:option
        animations:^{
            self.buttonInfoFrame.frame = frame;
            self.buttonSetCourse.frame = frame2;
        }
        completion:nil];
        }];
}
于 2013-05-26T14:44:38.917 回答
0
- (void)slideAnimation
{
[self.buttonInfoFrame setAlpha:1.0];
[self.buttonSetCourse setAlpha:1.0];
[self.buttonSetCourse setEnabled:YES];

if(![self.selected isEqual: @""])
{
    [UIView animateWithDuration:1.0 animations:^{
        CGRect frame = self.buttonInfoFrame.frame;
        frame.origin.x = 1050;
        frame.origin.y = 150;

        CGRect frame2 = self.buttonSetCourse.frame;
        frame2.origin.x = 1050;
        frame2.origin.y = 526;

        self.buttonInfoFrame.frame = frame;
        self.buttonSetCourse.frame = frame2;
    }
    completion:^(BOOL finished){
        [UIView animateWithDuration:1.0 animations:^{
            CGRect frame = self.buttonInfoFrame.frame;
            frame.origin.x = 757;
            frame.origin.y = 150;

            CGRect frame2 = self.buttonSetCourse.frame;
            frame2.origin.x = 747;
            frame2.origin.y = 526;

            self.buttonInfoFrame.frame = frame;
            self.buttonSetCourse.frame = frame2;
        }];
    }];
}
else
{
    CGRect frame = self.buttonInfoFrame.frame;
    frame.origin.x = 757;
    frame.origin.y = 150;

    CGRect frame2 = self.buttonSetCourse.frame;
    frame2.origin.x = 747;
    frame2.origin.y = 526;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];

    self.buttonInfoFrame.frame = frame;
    self.buttonSetCourse.frame = frame2;

    [UIView commitAnimations];
}
}
于 2013-05-26T15:06:20.607 回答