移动对象而不在动画块中对其进行动画处理

发布于 2024-10-02 20:38:11 字数 1032 浏览 0 评论 0原文

我正在为一堆按钮设置动画,以便它们移动到屏幕左侧,然后它们应该神奇地移动到屏幕右侧。我通过调用:

[NSTimer scheduledTimerWithTimeInterval:0.20 target:self selector:@selector(moveTheButtons) userInfo:nil repeats:YES];

in viewDidLoad 来做到这一点。

它们非常愉快地向左移动,但随后又又向右移动。我只想让它们消失并重新出现在屏幕右侧。因为我是新人,所以我认为既然是在 commitAnimations 调用之后,它就不会动画。我认为问题在于动画实际上是在 moveTheButtons 函数返回后“提交”的,但我想不出一种优雅(或标准)的方法来解决这个问题。

如何将 UIButton 移出屏幕而不对其进行动画处理,最好还是在 moveTheButtons 函数中?

正如您可能推断的那样,我对 iPhone 上的动画很陌生,所以如果您看到任何其他错误,我请随时给我一些指示。

-(void)moveTheButtons{
NSLog(@"moveTheButtons");

[UIView beginAnimations:@"mov-ey" context:cloudA];
[UIView setAnimationDuration: .20];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
cloudA.center = CGPointMake(cloudA.center.x-pos.x,cloudA.center.y);
[UIView commitAnimations];


if(cloudA.center.x < -100){
    //I don't want to animate this bit. 
    cloudA.center = CGPointMake(550,cloudA.center.y);
}

//NSLog(@"cloudA.center.x %f", cloudA.center.x);
}

I'm animating a bunch of buttons so they move to the left of the screen then they should magically move to the right of the screen. I do this by calling:

[NSTimer scheduledTimerWithTimeInterval:0.20 target:self selector:@selector(moveTheButtons) userInfo:nil repeats:YES];

in viewDidLoad.

They animate quite happily to the left, but then they animate back to the right. I just want them to disappear and reappear to the right-off-screen. Because I'm new I assumed that since it was after the commitAnimations call that it wouldn't animate. I think the problem is that the animations actually 'commit' after the moveTheButtons function returns, but I can't think of an elegant (or standard) way around this.

How do I move the UIButton off screen without animating it, preferably still in the moveTheButtons function?

As you can probably infer, I'm new to animations on the iPhone, so if you see any other mistakes I'm making feel free to give me some pointers.

-(void)moveTheButtons{
NSLog(@"moveTheButtons");

[UIView beginAnimations:@"mov-ey" context:cloudA];
[UIView setAnimationDuration: .20];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
cloudA.center = CGPointMake(cloudA.center.x-pos.x,cloudA.center.y);
[UIView commitAnimations];


if(cloudA.center.x < -100){
    //I don't want to animate this bit. 
    cloudA.center = CGPointMake(550,cloudA.center.y);
}

//NSLog(@"cloudA.center.x %f", cloudA.center.x);
}

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

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

发布评论

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

评论(2

陌路黄昏 2024-10-09 20:38:12

您可以使用 +[UIView setAnimationsEnabled:] 方法暂时关闭动画块内属性的隐式动画。这在许多用例中非常有用。

做这样的事情:

-(void)moveTheButtons {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration: .20];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    CGPoint center = CGPointMake(cloudA.center.x - pos.x, cloudA.center.y);
    if (center.x < -100) {
        [UIView setAnimationsEnabled:NO];
        cloudA.center = CGPointMake(550, center.y);
        [UIView setAnimationsEnabled:YES];
    } else {
        cloudA.center = center;
    }
    [UIView commitAnimations];
}

作为旁注;无需为动画指定名称或上下文,除非您实际使用响应委托方法的委托。只需传递 nilNULL,就像我在本例中所做的那样。

You can temporarily turn off the implicit animations of properties within an animation block using the +[UIView setAnimationsEnabled:] method. This can be very useful in many use cases.

Do something like this:

-(void)moveTheButtons {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration: .20];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    CGPoint center = CGPointMake(cloudA.center.x - pos.x, cloudA.center.y);
    if (center.x < -100) {
        [UIView setAnimationsEnabled:NO];
        cloudA.center = CGPointMake(550, center.y);
        [UIView setAnimationsEnabled:YES];
    } else {
        cloudA.center = center;
    }
    [UIView commitAnimations];
}

As a side note; there is no need to give the animation a name, or a context, unless you actually use a delegate that responds to delegate methods. Just pass nil, and NULL as I have done in this example.

葬花如无物 2024-10-09 20:38:12

在对块进行动画处理之前预先计算点、反转顺序并返回。

在所有情况下,您都会无条件地将动画提交给引擎。

-(void)moveTheButtons{
NSLog(@"moveTheButtons");

CGPoint mypoint = CGPointMake(cloudA.center.x-pos.x,cloudA.center.y);

if(cloudA.center.x < -100){
    //I don't want to animate this bit. 
    cloudA.center = CGPointMake(550,cloudA.center.y);
    return;
}


[UIView beginAnimations:@"mov-ey" context:cloudA];
[UIView setAnimationDuration: .20];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
cloudA.center = mypoint;
[UIView commitAnimations];

}

使用 return 而不是 if/else 有一个风格要点,但您可以形成自己的意见。

干杯

Precalculate the point , invert the order and return before animating the block.

You are unconditionally committing an animation to the engine in all cases.

-(void)moveTheButtons{
NSLog(@"moveTheButtons");

CGPoint mypoint = CGPointMake(cloudA.center.x-pos.x,cloudA.center.y);

if(cloudA.center.x < -100){
    //I don't want to animate this bit. 
    cloudA.center = CGPointMake(550,cloudA.center.y);
    return;
}


[UIView beginAnimations:@"mov-ey" context:cloudA];
[UIView setAnimationDuration: .20];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
cloudA.center = mypoint;
[UIView commitAnimations];

}

Theres a style point about using a return rather than if/else but you can form your own opinion.

Cheers

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