【问题标题】:NSTimer change image iPhone ProgrammingNSTimer 更改图像 iPhone 编程
【发布时间】:2011-12-02 22:51:22
【问题描述】:

如何在 iPhone 编程中使用 NSTimer 定期更改图像?

我创建了一个用于加载图像的图像视图。

我想在 imageview 中显示图像并使用 NSTimer 定期更改图像。

【问题讨论】:

    标签: iphone ios nstimer


    【解决方案1】:

    我不会使用 NSTimer,而是使用一组 UIImage 并为其设置动画。请参阅文档from here

    【讨论】:

    • 这种方法的结果是,带有许多大图像的动画更流畅。
    • 如何使用这个淡入淡出图像?让时间间隔更长一点,但哪个方法调用淡入淡出/alpha更改为uiimageview?
    【解决方案2】:

    将您喜欢的图像导入您的捆绑应用程序 称它们为 image1.png、image2.png image3.png 等等....

    在界面生成器中添加 UIImageview 调用它(参考)“theImage”

    在H文件中:

    @interface delme3ViewController : UIViewController {
    
        UIImageView *theImage;
        int imageCount;
    }
    @property (nonatomic, retain) IBOutlet UIImageView *theImage;
    
    @end
    

    在M文件中:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [NSTimer scheduledTimerWithTimeInterval:1 
                                         target:self 
                                       selector:@selector(animateFunction) 
                                       userInfo:nil 
                                        repeats:YES];
        imageCount = 1;
    
    }
    
    -(void)animateFunction
    {
        NSLog(@"Timer heartbeat %i",imageCount);
        imageCount = imageCount + 1;
        NSString *imageName = [NSString stringWithFormat:@"image%i.png"];
        [theImage setImage:[UIImage imageNamed:imageName]];
    }
    

    【讨论】:

      【解决方案3】:

      您还可以使用图像名称数组,而不是重命名捆绑包中的图像。 :)

      【讨论】:

      • 或者可以使用一组 UIImage 对象。 (尽管由于系统缓存图像的方式,这通常不会带来任何好处。)
      猜你喜欢
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多