在objective-c中循环移动对象?
我试图在在objective-c中循环移动对象?,objective-c,loops,uiviewanimation,Objective C,Loops,Uiviewanimation,我试图在while循环中移动ui视图。它不会在视觉上移动ui视图,我认为这很奇怪。根据我的经验,在Obj-C中是这样的,为什么在Obj-C中是这样的?它仅在循环完成时可视地移动它。我认为这不需要代码,只需要一行代码就可以移动它 编辑 已请求代码,请执行以下操作: while (!CGPointEqualToPoint(finder.center, endPoint)) { [finder setCenter:CGPointMake(finder.center.x+10, finder.c
while循环中移动ui视图
。它不会在视觉上移动ui视图,我认为这很奇怪。根据我的经验,在Obj-C中是这样的,为什么在Obj-C中是这样的?它仅在循环完成时可视地移动它。我认为这不需要代码,只需要一行代码就可以移动它
编辑
已请求代码,请执行以下操作:
while (!CGPointEqualToPoint(finder.center, endPoint)) {
[finder setCenter:CGPointMake(finder.center.x+10, finder.center.y)];
}
一行
EDOT
好的,这不是全部,这是一个路径查找器。但是那一行是移动UIView的那一行,所以我认为这是我问题中唯一相关的一行。因为当你更新循环中的帧时,屏幕不会更新。iOS自行决定何时更新屏幕。这就是为什么存在像setNeedsDisplay
这样的方法,而不存在像display
这样的方法。所以,若您在while循环中这样做,屏幕将只更新一次。请参阅nsrunlop
文档
好像你不想做那样的事:
- (void)doSomeAnimation {
[UIView animateWithDuration:0.5
animations:^{
self.someView.frame = CGRectMake(self.someView.frame.origin.x + 10,
self.someView.frame.origin.y,
self.someView.frame.size.width,
self.someView.frame.size.height);
}
completion:^(BOOL finished){
if ([self needsDoAnimation]) // while condition analog
{
[self doSomeAnimation];
}
}];
}
- (BOOL)needsDoAnimation {
BOOL needsAnimation = (self.someView.frame.origin.x < 320); // some conditions
return needsAnimation;
}
-(void)doSomeAnimation{
[UIView animateWithDuration:0.5
动画:^{
self.someView.frame=CGRectMake(self.someView.frame.origin.x+10,
self.someView.frame.origin.y,
self.someView.frame.size.width,
self.someView.frame.size.height);
}
完成:^(布尔完成){
if([self-needsDoAnimation])///while条件模拟
{
[自我激励];
}
}];
}
-(BOOL)需要消毒{
BOOL needsAnimation=(self.someView.frame.origin.x<320);//一些条件
回归需要激励;
}
查看什么是主事件循环
我不确定我是否真的理解你的问题。
如果您想在特定的持续时间和特定的声誉中移动循环,为什么不在UIAnimation
块中编写一个方法,在动画之后,当您进入完成博客时,只需再次调用该方法即可
- (void)animateMyView:(UIView *)view
withDuration:(float)duration
andRepeat:(int)repeat{
[UIView animateWithDuration:duration animations:^{
view.frame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y + 20,
self.view.frame.size.width,
self.view.frame.size.height);
} completion:^(BOOL finished) {
if(repeat > 0){
[self animateMyView:view
withDuration:duration
andRepeat:(repeat - 1)];
}
}];
}
这将在y轴上每移动20.0次视图。如果要将视图设置为新位置,可以使用:
[UIView animateWithDuration:2.0 animations:^{
view.frame = finalPosition;
} completion:^(BOOL finished){
// code to run when animation is done
}
参考资料:请展示一些代码[view setNeedsDisplay]
应该如何工作一些代码,我们将为您提供具体的解决方案:)您需要在某种动画中包含这些内容,这就是它无法正常工作的原因。不必费心回答,因为已经给出的答案都很好。完全同意H2CO3,你想要的和我的例子完全一样,但是你应该注意条件。如果将位置增加10,则很可能不会执行CGPointEqualToPoint条件