是否可以更改 UIView 的“中心”重写的 TouchsEnded 方法中的属性并重绘视图?
我想基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你知道你希望视图中心的最终位置,你可以用动画来做到这一点:
If you know the final positions you want your view center to have you could do this with an animation:
如果该代码在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