【问题标题】:UIButton animation is skippedUIButton 动画被跳过
【发布时间】:2013-05-26 14:40:39
【问题描述】:

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

但是第一个动画(当按钮已经滑入屏幕时)被跳过,以便按钮立即跳出屏幕 然后向后滑动。

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

有点像 if 子句中的 setAnimationDuration 被忽略:

[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];

【问题讨论】:

  • 被选中的呢?一个布尔值、一个字符串等?
  • slideAnimation 是从 5 个不同的按钮调用的......它只是一个按下哪个按钮的标识符......如果在标识符为空之前没有按下任何按钮,所以我不必运行第一个动画。

标签: ios objective-c uibutton uiviewanimation


【解决方案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];
        }];
}

【讨论】:

  • 两件事:1.第一个动画的完成块? 2. 第二个动画并不总是秒...有时它是第一个也是唯一的....
【解决方案2】:

让我们简化代码:

[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);
}

【讨论】:

    【解决方案3】:
    - (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];
    }
    }
    

    【讨论】:

    • 请检查我的回答。它展示了如何删除重复的代码。
    猜你喜欢
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多