自定义UIImage序列

时间:2015-02-20 02:09:39

标签: ios objective-c iphone

有人可以帮我格式化一些代码吗?我想在我的故事板上显示一个图像,并在5秒后将其更改为屏幕上的新图像。这是我到目前为止所拥有的。此代码在成功5秒后更改字符串。

double delayInSeconds1 = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds1 * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    hello.text = [NSString stringWithFormat:@"welcome"];
});
double delayInSeconds2 = 5.0;
dispatch_time_t popTime1 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds2 * NSEC_PER_SEC));
dispatch_after(popTime1, dispatch_get_main_queue(), ^(void){
    hello.text = [NSString stringWithFormat:@"hello there"];
});

如何设置我的.h文件,以便hello.text类似于image=[UIImage imageNamed:@"image1"]我应该使用UIImageView插座还是UIImage?到目前为止,没有任何出现或改变。如何轻松启动自定义图像序列?有趣的是我之前已经开始工作,但忘了我做了什么。 始终保存您的项目,即使是愚蠢的项目

1 个答案:

答案 0 :(得分:2)

您可以使用UIImageView的animationImages属性:

UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 141, 219)];
image.animationImages = [NSArray arrayWithObjects:
                              [UIImage imageNamed:@"image1.png"],
                              [UIImage imageNamed:@"image2.png"],
                              nil];
image.animationDuration = 5;
image.animationRepeatCount = 0; // infinitely
[image startAnimating];

空白XCode项目的示例:

在ViewController.h中:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *image;

在ViewController.m中:

@implementation ViewController

@synthesize image = _image;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _image.animationImages = [NSArray arrayWithObjects:
                         [UIImage imageNamed:@"jeu0_icon1.png"],
                         [UIImage imageNamed:@"jeu0_icon2.png"],
                         nil];
    _image.animationDuration = 5;
    _image.animationRepeatCount = 0; // infinitely
    [_image startAnimating];
}

它的工作原理(我的2个png资产很好地添加到项目中,当然在stb中添加了UIImageView ......