1

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

你能告诉我有什么问题吗?TIA

视图控制器.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

视图控制器.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
4

1 回答 1

0

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

几点建议:

  • 检查动画是否真的被调用(记录 makePrediction()),也许你的故事板中有一些其他对象可以充当 First Responder。
  • 检查您的项目中是否已导入所有图像。
  • 尝试使用其中一个动画帧而不是背景来初始化图像视图,或者使用两个不同的 imageView,一个用于动画,另一个用于背景。
于 2013-10-11T07:03:40.080 回答