UImage视图动画它在我的iPhone上不起作用

时间:2015-03-18 15:34:10

标签: ios animation imageview

构建代码后,我的手机上没有任何动画

但代码看起来很好,我不知道如何解决,

我的图片是829 * 445,是否会导致此问题?

有人可以帮我解决这个问题我会非常感激,谢谢

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"],
                                [UIImage imageNamed:@"2.jpg"],
                                [UIImage imageNamed:@"3.jpg"],
                                [UIImage imageNamed:@"4.jpg"],nil];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,150,150)];
    imageView.animationImages = animationImages ;
    imageView.animationRepeatCount = 2;
    imageView.animationDuration= 4.0;
    [imageView startAnimating];

    [NSTimer scheduledTimerWithTimeInterval:4.0 target:self
                                   selector:@selector(animationDone:)
                                   userInfo:nil repeats:NO];
} 

-(void)animationDone:(NSTimer*)inTimer
{
    [inTimer invalidate];
    inTimer = nil;
    NSLog(@"animationDone ");

}

@end

3 个答案:

答案 0 :(得分:0)

您没有将imageView添加到ViewController视图中。

[self.view addSubview:imageView];

中试试-viewDidLoad

答案 1 :(得分:0)

您好像没有将动画UIImageView添加到视图层次结构中。在[imageView startAnimating];行之前添加以下代码行:

[self.view addSubview:imageView];

答案 2 :(得分:0)

请使用动画块代码而不是旧方法。动画会告诉你它什么时候完成。您无需设置计时器。

NSArray *animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"],
                                [UIImage imageNamed:@"2.jpg"],
                                [UIImage imageNamed:@"3.jpg"],
                                [UIImage imageNamed:@"4.jpg"],nil];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,150,150)];

    [UIView animateWithDuration:1.0f animations:^{

        for (int loop = 0; loop < 4; loop++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFornmat:@"%d.jpg", (loop + 1)]];
            [imageView setImage:image];
        }

    } completion:^(BOOL finished) {
         NSLog(@"animationDone");
    }];