无法使UIImageView对象动画化

时间:2013-10-11 05:55:06

标签: ios objective-c uiimageview

我已经设置了一个.h,.m页面来动画一些UIImageView对象,但无法让它们动画化。没有得到任何警告,错误。

你能告诉我什么是错的吗? TIA

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *predictionLabel;
@property (strong, nonatomic) NSArray *predictionArray;
@property (strong, nonatomic) UIImageView *imageView;

- (void) makePrediction;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize predictionArray;
@synthesize imageView;

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

    UIImage *image = [UIImage imageNamed:@"background_s4.png"];
    self.imageView = [[UIImageView alloc]initWithImage:image];
    [self.view insertSubview:self.imageView atIndex:0];
    self.imageView.animationImages = [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"cball00001.png"],
                                        [UIImage imageNamed:@"cball00002.png"],
                                        [UIImage imageNamed:@"cball00003.png"],
                                        [UIImage imageNamed:@"cball00004.png"],
                                        [UIImage imageNamed:@"cball00005.png"],
                                        [UIImage imageNamed:@"cball00006.png"],
                                        [UIImage imageNamed:@"cball00007.png"],
                                        [UIImage imageNamed:@"cball00008.png"],
                                        [UIImage imageNamed:@"cball00009.png"],
                                        [UIImage imageNamed:@"cball00010.png"],
                                        [UIImage imageNamed:@"cball00011.png"],
                                        [UIImage imageNamed:@"cball00012.png"],
                                        [UIImage imageNamed:@"cball00013.png"],
                                        [UIImage imageNamed:@"cball00014.png"],
                                        [UIImage imageNamed:@"cball00015.png"],
                                        [UIImage imageNamed:@"cball00016.png"],
                                        [UIImage imageNamed:@"cball00017.png"],
                                        [UIImage imageNamed:@"cball00018.png"],
                                        [UIImage imageNamed:@"cball00019.png"],
                                        [UIImage imageNamed:@"cball00020.png"],
                                        [UIImage imageNamed:@"cball00021.png"],
                                        [UIImage imageNamed:@"cball00022.png"],
                                        [UIImage imageNamed:@"cball00023.png"],
                                        [UIImage imageNamed:@"cball00024.png"], nil];
    self.imageView.animationDuration = 1.0;
    self.imageView.animationRepeatCount = 1;

    self.predictionArray = [[NSArray alloc]initWithObjects:@"It is certain",
                                @"All signs are YES",
                                @"The stars are not aligned",
                                @"My reply is no",
                                @"It is doubtful",
                                @"Better not tell you now",
                                @"Concentrate and ask again",
                                @"Unable to answer now", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) makePrediction
{
    NSUInteger index = arc4random_uniform(self.predictionArray.count);
    self.predictionLabel.text = [self.predictionArray objectAtIndex:index];

    [self.imageView startAnimating];
}

- (BOOL) canBecomeFirstResponder
{
    return YES;
}

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
   self.predictionLabel.text = @"";
}

- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if  (motion == UIEventSubtypeMotionShake){
        [self makePrediction];
    }
}

- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"motion cancelled");
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.predictionLabel.text = @"";
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   [self makePrediction];
}

@end

1 个答案:

答案 0 :(得分:0)

如果我创建一个新的“单视图”项目,将您的代码粘贴到视图控制器中并添加一些测试图像,您的代码就可以正常使用(Xcode 5,iOs7,在模拟器上)。您遇到的问题必须取决于您设置视图或项目的方式。

一些建议:

  • 检查动画是否实际被调用(log makePrediction()),也许你的故事板中有一些其他对象可以充当第一响应者。
  • 检查项目中是否已导入所有图像。
  • 尝试使用其中一个动画帧初始化图像视图,不使用背景,或者使用两个不同的imageView,一个用于动画,另一个用于背景。