使用for循环移动UIView [英] Moving UIView with a for loop
问题描述
我正在尝试移动一个很小的UIView.我的for循环只是将UIView向下移动了100点.
I am trying to move a UIView, which is small. My for loop just moves the UIView down 100 points.
除了在循环完成之前它不会移动之外,这是可行的.
This works except it doesn't move until the loop is finished.
我尝试设置
[self.view setNeedsDisplay]和[myView setNeedsDisplay],我还将该函数放在了nstimer和nsthread中,如下所示
[self.view setNeedsDisplay] and [myView setNeedsDisplay], I have also put the function inside nstimer and nsthread, like seen below
[NSThread detachNewThreadSelector:@selector(doStuff) toTarget:self withObject:nil];
和
[NSTimer scheduledTimerWithTimeInterval:1/15
target:self selector:@selector(doStuff)
userInfo:nil
repeats:NO] ;
但是它只是不动画".
But it just does not "animate".
每次移动视图后,我都会进行sleep(1);但还是一无所有.
After each move of the view, I do a sleep(1); but still nothing.
希望任何人都可以帮助我.
Hope anyone can help me.
推荐答案
您的for循环会阻止视图的实际重绘,该重绘发生在运行循环中,当视图的drawRect被调用时.您有三个选择(还有更多选择).
Your for loop blocks the actual redrawing of your view, which happens in the runloop, when your view's drawRect gets called. You have three options (and many more).
@Darren建议了两个.另一个方法是调用performSelector:afterDelay,并传入将视图移动100像素(for循环的内部)的例程.在该例程的结尾,调用performSelector:afterDelay:重新排队下一个调用.我更喜欢使用计时器.
Two were suggested by @Darren. Another is to call performSelector:afterDelay passing in the routine that moves the view 100 pixels (the innards of your for loop). At the end of that routine, call performSelector:afterDelay: to requeue the next call. I prefer this to using a timer.
这篇关于使用for循环移动UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!