【问题标题】:Toggle between two images with sleeptime在具有睡眠时间的两个图像之间切换
【发布时间】:2011-05-20 19:08:39
【问题描述】:

按下按钮后,我需要在两张(或以后更多)图片之间切换特定的次数,然后等待一两秒钟进行更改。在任何时候按下停止按钮时,切换应该停止。我的代码现在看起来像这样

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 是 除此之外,在 stopRepetitionCycle 中,我只是将 stopButtonPressed 设置为 YES,因此它在第一次完成“for”后停止。 它确实倒计时,它确实在一个周期后停止,但它不会改变画面。

在摆弄的时候,我通过 IB 设置了初始图片,所以它最终显示了 ANYTHING.. 有趣的部分,当我在正确的时刻按下停止按钮时,显示了第二张图片。所以我猜想每次图像切换时我都需要手动设置视图。但是

        [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

知道我要在这里做什么吗?

非常感谢!

【问题讨论】:

标签: iphone objective-c animation uiimageview


【解决方案1】:

嗯……

你对[NSThread sleep...] 的使用让我感到困惑...

事实上:如果你在辅助线程上(意思是,不是主线程),那么你正在做一些不允许的事情,即trying to access the UI from a secondary thread。

这可以解释你看到的奇怪行为。

另一方面,如果这是主线程,则调用sleep... 可能不是一个好主意,因为这样您将完全冻结用户界面,并且您不可能拦截对按钮的单击...

无论如何,我的建议是使用NSTimers 以特定时间间隔从一张图像移动到下一张图像。当停止按钮被隐藏时,您只需取消计时器,幻灯片就会结束。很干净。

关于您的图像出现的错误消息,事实是UIImage 不是UIView,因此您不能将其添加为子视图,但这不是在这里不起作用的...

【讨论】:

    【解决方案2】:

    使用UIImageView。它对此具有内置支持。

    imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"blue.jpg"], [UIImage imageNamed:@"image1.jpg"], nil];
    imageView.animationDuration = kSleepDuration * [imageView.animationImages count];
    

    将此函数连接到开始按钮

    - (IBAction) startAnimation {
        [imageView startAnimating];
    }
    

    这是停止按钮

    - (IBAction) stopAnimation {
        [imageView stopAnimating];
    }
    

    【讨论】:

    • 谢谢!这太棒了,正是我需要的!感谢一千遍!! ^^
    【解决方案3】:

    在循环中更新 UI 几乎肯定会失败。您很可能应该使用 NSTimer 以给定的时间间隔交换图像。

    此外,[self.exerciseView addSubview:image] 失败,因为您传递的是 UIImage 而不是 UIView。使用您的 UIImage 创建一个 UIImageView(它是 UIView 的子类)并传递它。

    【讨论】:

      【解决方案4】:

      方法 addSubview 仅用于添加UIViews。您不能将它与 UIImage 类一起使用。一般语法是:

      [(UIView) addSubview:(UIView*)];
      

      替换

      [self.exerciseView addSubview:image];  
      

      与

      self.exerciseView.image = image;  
      

      同样的原因

      [self.exerciseView.image addSubview:image];  
      

      也不行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多