简单动画的问题

发布于 2024-10-21 22:06:34 字数 479 浏览 5 评论 0原文

我在按钮上使用动画,单击第一次显示视图,第二次隐藏视图。 这是我用于隐藏视图的代码,

-(IBAction)clickme
{
[UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];


    [view1 setAlpha:0.0];
[UIView commitAnimations];
}

类似的代码用于显示视图。

但是,当用户一次又一次单击按钮多次时,就会出现问题......意味着我的动画使用了 2 秒,但如果用户在动画期间按下相同的按钮,则输出结果非常糟糕。

我不想在动画期间禁用该按钮。

还有其他办法吗?

I am using animation on a button click first time show a view and second ti me hide a view.
here is my code for hiding a view

-(IBAction)clickme
{
[UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];


    [view1 setAlpha:0.0];
[UIView commitAnimations];
}

similar code is there for showing the view.

But the problem arises when user click the button many times again and again....means i am using 2 seconds for my animation but if user presses the same button in during the animation then the output result is very bad.

I don't want to disable that button during the period of animation.

Is there any other way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左岸枫 2024-10-28 22:06:34

您需要跟踪是否有动画正在进行,如果有则忽略点击。

在类头中声明一个实例变量 BOOL animating;,并在 init 中将其初始化为 NO

然后,

-(IBAction)clickme
{
    if (animating) return;
    animating = YES;

[UIView beginAnimations:nil context:self];
    [UIView setAnimationDuration:0.3];

    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];

    [view1 setAlpha:0.0];
[UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    if (context == self)
        animating = NO;
}

You need to keep track of whether there's an animation going on, and ignore the click if it is.

Declare an instance variable BOOL animating; in your class header, and initialise it to NO in your init.

Then,

-(IBAction)clickme
{
    if (animating) return;
    animating = YES;

[UIView beginAnimations:nil context:self];
    [UIView setAnimationDuration:0.3];

    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];

    [view1 setAlpha:0.0];
[UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    if (context == self)
        animating = NO;
}
悟红尘 2024-10-28 22:06:34

尝试使用 + (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState:

-(IBAction)clickme
{
[UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];


    [view1 setAlpha:0.0];
[UIView commitAnimations];
}

try to use + (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState:

-(IBAction)clickme
{
[UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];


    [view1 setAlpha:0.0];
[UIView commitAnimations];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文