iOS - 逐帧更改图像

标签 ios objective-c

我正在尝试逐帧更改 UIImageView 的图像,我有几个应该通过按钮更改的以下图像。这是我的代码:

- (IBAction)next:(id)sender {

    for( i = 0; i <= 31; i++ ) {
        NSString* filename = [NSString stringWithFormat:@"pic%d.jpeg",i];
               _image.image = [UIImage imageNamed:filename];
    }

}

问题是当我点击按钮时,我的代码将图像更改为仅第一帧

最佳答案

因为变量 i 的值始终为 0

尝试以下解决方案:

在.h文件中创建实例变量

int imgCount;

在ViewDidLoad方法中初始化它

imgCount = 0;
NSString* filename = [NSString stringWithFormat:@"pic%d.jpeg",imgCount];  // img0 is displayed
_image.image = [UIImage imageNamed:filename];


- (IBAction)next:(id)sender {
     //first (by default) img0 is display
     imgCount++;

     if (imgCount > 30) {  //suppose total image is 31 means 0 to 30
         imgCount = 0;
     }
     NSString* filename = [NSString stringWithFormat:@"pic%d.jpeg",imgCount];
     _image.image = [UIImage imageNamed:filename];
}

- (IBAction)previous:(id)sender {
     imgCount--;

     if (imgCount < 0) {  //suppose total image is 31 means 0 to 30
         imgCount = 30;
     }
     NSString* filename = [NSString stringWithFormat:@"pic%d.jpeg",imgCount];
     _image.image = [UIImage imageNamed:filename];
}

关于iOS - 逐帧更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16250467/

相关文章:

ios - 在不改变框架的情况下旋转 UIView

objective-c - 在基于文档的应用程序中打开无标题文件

ios - 在 Xcode "XPC connection interrupted"中获取此消息

ios - Xcode 仅重新加载 UiCollectionView 中的一个单元格

ios - 在现有的 tableview 中使用 Objective-C 实现 UISearchController

objective-c - 将文件从 NSTableView 拖到其他 osx 应用程序

ios - UITableView:删除不可见的行

ios - Swift:同时平移和长按识别器

ios - 如何在 Objective C 中对齐内存?

objective-c - libxml2 是否支持 XPath 2.0?