通过睡眠时间在两个图像之间切换
按下按钮后,我需要在两张(或稍后更多)图片之间切换特定的次数,然后等待一两秒钟进行更改。当任何时候按下停止按钮时,切换应该停止。我的代码现在看起来像这个
IBOutlet UIImageView *exerciseView;
- (void) repetitionCycle {
stopButtonPressed = NO;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (NSInteger counter = kRepetitions; counter >=0; counter--) {
exerciseView.image = [UIImage imageNamed:@"blue.jpg"];
[NSThread sleepForTimeInterval:kSleepDuration];
if (stopButtonPressed) {break;}
image = [UIImage imageNamed:kExerciseEndingPosition];
exerciseView.image = [UIImage imageNamed:@"image1.jpg"];
[NSThread sleepForTimeInterval:kSleepDuration];
if (stopButtonPressed) {break;}
}
self.stopRepetitionCycle;
[pool release];
}
executionView 是 除此之外,在 stopRepetitionCycle 中,我只是将 stopButtonPressed 设置为 YES,因此它在第一次完成“for”后停止。 它确实倒计时,在一个周期后确实停止,但它不会改变图片。
在摆弄时,我通过 IB 设置了初始图片,所以它最终显示了任何东西。有趣的部分是,当我在正确的时刻按下停止按钮时,会显示第二张图片。所以我想每次图像切换时我都需要手动设置视图。但
[self.exerciseView addSubview:image];
给了我错误
Incompatible Objective-C types "struct UIImage *", expected "struct UIView *" when passing argument 1 of "addSubview:" from distinct Objective-C type
也
[self.exerciseView.image addSubview:image];
没有完成这项工作并给了我一个
UIImage may not respond to addSubview
知道我必须在这里做什么吗?
非常感谢!
I need to toggle a specific amount of times between two (or maybe later on more) pictures after a button was pressed, and wait a second or two for the change. When a stop-button is pressed at any time, the toggling should stop. My code by now looks like this
IBOutlet UIImageView *exerciseView;
- (void) repetitionCycle {
stopButtonPressed = NO;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (NSInteger counter = kRepetitions; counter >=0; counter--) {
exerciseView.image = [UIImage imageNamed:@"blue.jpg"];
[NSThread sleepForTimeInterval:kSleepDuration];
if (stopButtonPressed) {break;}
image = [UIImage imageNamed:kExerciseEndingPosition];
exerciseView.image = [UIImage imageNamed:@"image1.jpg"];
[NSThread sleepForTimeInterval:kSleepDuration];
if (stopButtonPressed) {break;}
}
self.stopRepetitionCycle;
[pool release];
}
exerciseView is
Besides other things, in stopRepetitionCycle I just set stopButtonPressed to YES, so it stops after it finished the "for" for the first time.
It does count down, it does stop after one cycle, but it doesn't change the picture.
While fiddling around, I set the initial picture via IB, so it finally displayed ANYTHING.. The fun part, when I hit the stop button in the right moment, the second picture is shown. So I guessed I need to set the view manually every time the image should toggle. But
[self.exerciseView addSubview:image];
gives me the error
Incompatible Objective-C types "struct UIImage *", expected "struct UIView *" when passing argument 1 of "addSubview:" from distinct Objective-C type
Also
[self.exerciseView.image addSubview:image];
doesn't do the job and gives me a
UIImage may not respond to addSubview
Any idea what I have to do here?
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
uuhm...
您对
[NSThread sleep...]
的使用让我感到困惑...事实上:如果您在辅助线程上(意思是,不是主线程),那么您正在做某事不允许,这是尝试从辅助线程。
这可以解释您所看到的奇怪行为。
另一方面,如果这是主线程,调用 sleep... 可能不是一个好主意,因为这样您将完全冻结 UI,并且您不可能拦截单击按钮...
无论如何,我建议,使用 NSTimer 以一定的时间间隔从一张图像移动到下一张图像。当停止按钮隐藏时,您只需取消计时器,幻灯片就会结束。很干净。
至于您的图像出现的错误消息,事实是
UIImage
不是UIView
,因此您无法将其添加为子视图,但这不是在这里不起作用...uuhm...
your usage of
[NSThread sleep...]
puzzles my...in fact: if you are on a secondary thread (meaning, not the main thread), then you are doing something not allowed, which is trying to access the UI from a secondary thread.
this could explain the strange behavior you are seeing.
on the other hand, if this is the main thread, possibly is not a good idea to call
sleep...
because that way you will freeze entirely the UI, and you could not possibly intercept the click on the button...anyhow, what I would suggest, is using
NSTimer
s to move from one image to the next one at certain intervals of time. when the stop button is hidden you would simply cancel the timer and the slideshow would end. pretty clean.as to the error messages you are having with your image, the fact is that
UIImage
is not aUIView
, so you cannot add it as a subview, but this is not what does not work here...使用 UIImageView。它对此有内置支持。
将此功能连接到开始按钮
,并将其连接到停止按钮
Use UIImageView. It has built-in support for this.
Wire this function to the start button
and this to the stop button
在循环内更新 UI 几乎肯定会失败。您很可能应该使用 NSTimer 以给定的时间间隔交换图像。
此外, [self.exerciseView addSubview:image] 失败,因为您传递的是 UIImage 而不是 UIView。使用您的 UIImage 创建一个 UIImageView (它是 UIView 的子类)并传递它。
Updating the UI within a loop is almost guaranteed to fail. You should most likely be using an NSTimer to swap the image at a given interval instead.
Furthermore, [self.exerciseView addSubview:image] is failing because you're passing a UIImage and not a UIView. Create a UIImageView (which is a subclass of UIView) with your UIImage and pass that instead.
addSubview 方法仅用于添加
UIViews
。您不能将它与 UIImage 类一起使用。一般语法是:替换
为
出于同样的原因
也不起作用。
The method addSubview is used to add only
UIViews
. You can't use it with a UIImage class. The general syntax is :replace
with
For the same reason
won't work either.