是否可以更改 UIView 的“中心”重写的 TouchsEnded 方法中的属性并重绘视图?

发布于 2024-11-05 04:48:19 字数 492 浏览 1 评论 0原文

我想基于 UIView 的 center 属性实现一个简单的动画。我有一个简单的视图,我可以拖动它(UIView TouchsMoved 被覆盖)。动画应该慢慢淡出,就像用户释放视图后视图在其自身惯性作用下移动一段时间一样。但现在我只想在触摸结束后移动视图。这是我在 TouchsEnded 中的代码:

int i;
    for (i=1;i<4;i++)
    {
        self.center = CGPointMake(10*i, 12*i);
        [self setNeedsDisplay];
        usleep(100000);
    }

问题是当我运行此代码时,代码执行得很好,但“UIView”更改为较晚。我改变了usleep时间和其他参数但结果是一样的。看起来视图中的所有“挂起的更改”仅在覆盖的touchesEnded完成后才执行。

这是实现此类用户界面功能的正确方法还是我应该寻找其他方法?

提前致谢。

I want to implement a simple animation based on UIView's center property. I have a simple view and I can drag it (UIView touchesMoved is overriden). The animation should fade slowly like the view is moved under it's own inertia for some time after the user releases it. But for now I want simply to move the view after touch ends. Here is the code I have in touchesEnded:

int i;
    for (i=1;i<4;i++)
    {
        self.center = CGPointMake(10*i, 12*i);
        [self setNeedsDisplay];
        usleep(100000);
    }

The problem is when I run this, the code is executed nicely but "UIView" changes to late. I changed usleep time and other parameters but the result is the same. It looks like all the "pending changes" in the view are performed only after the overriden touchesEnded is finished.

Is this the right way of implementing such user interface feature or should I look for some other approaches?

Thanks in advance.

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

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

发布评论

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

评论(2

想念有你 2024-11-12 04:48:19

如果你知道你希望视图中心的最终位置,你可以用动画来做到这一点:

[UIView animateWithDuration:20
                      delay:0
                    options: UIViewAnimationOptionCurveEaseIn
                 animations:^{
                         youView.center = CGPointMake(newCenterX, newCenterY);
                       }
                       completion:^(BOOL finished){ 
                       }];

If you know the final positions you want your view center to have you could do this with an animation:

[UIView animateWithDuration:20
                      delay:0
                    options: UIViewAnimationOptionCurveEaseIn
                 animations:^{
                         youView.center = CGPointMake(newCenterX, newCenterY);
                       }
                       completion:^(BOOL finished){ 
                       }];
清泪尽 2024-11-12 04:48:19

如果该代码在touchesEnded内部运行,您将阻塞主线程,这就是动画在完成后立即发生的原因。您将需要在后台运行此循环以随着时间的推移更新视图。当您更新 UI 时,请记住始终回调到主线程。

一些线程选项:

performSelectorInBackground:withObject:

performSelectorOnMainThread:withObject:waitUntilDone:

Grand Central Dispatch

线程

If that code is being run inside of touchesEnded you are blocking the main thread and that is why the animation happens all at once when its done. You will need run this loop in the background to update the view over time. When you update the UI remember to always call back to the main thread.

Some Threading Options:

performSelectorInBackground:withObject:

performSelectorOnMainThread:withObject:waitUntilDone:

Grand Central Dispatch

Threading

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