简体   繁体   中英

UIImageView will not animate with an image based animation

I have 8 UIImageView s that were created in the storyboard that I am trying to animate with an image based animation, but for some reason the images won't animate.

viewController.h

#import <UIKit/UIKit.h>

@class RandomImages;

@interface ViewController : UIViewController

// Outlets foe the dice
@property (weak, nonatomic) IBOutlet UIImageView *dieImage0;
@property (weak, nonatomic) IBOutlet UIImageView *dieImage1;
@property (weak, nonatomic) IBOutlet UIImageView *dieImage2;
@property (weak, nonatomic) IBOutlet UIImageView *dieImage3;
@property (weak, nonatomic) IBOutlet UIImageView *dieImage4;
@property (weak, nonatomic) IBOutlet UIImageView *dieImage5;
@property (weak, nonatomic) IBOutlet UIImageView *dieImage6;
@property (weak, nonatomic) IBOutlet UIImageView *dieImage7;
@property (weak, nonatomic) IBOutlet UIButton *MenuButton;
@property (weak, nonatomic) IBOutlet UIButton *rollTargetButton;

@property (strong, nonatomic) RandomImages *randomImages;
@property (strong, nonatomic) NSArray *diceOutletArray;

- (void) rollDice;

@end

viewController.m

#import "ViewController.h"
#import "RandomImages.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize dieImage0;
@synthesize dieImage1;
@synthesize dieImage2;
@synthesize dieImage3;
@synthesize dieImage4;
@synthesize dieImage5;
@synthesize dieImage6;
@synthesize dieImage7;
@synthesize diceOutletArray;
@synthesize rollTargetButton;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.randomImages = [[RandomImages alloc] init];

    self.rollTargetButton.hidden = YES;

    self.diceOutletArray = [[NSArray alloc] initWithObjects:self.dieImage0, self.dieImage1, self.dieImage2, self.dieImage3, self.dieImage4, self.dieImage5, self.dieImage6, self.dieImage7, nil];

     // Animation for rolling dice
    for (UIImageView *dieImages in self.diceOutletArray) {
        dieImages.animationImages = [[NSArray alloc] initWithObjects:
                                     [UIImage imageNamed:@"dicy-die5"],
                                     [UIImage imageNamed:@"dicy-die6"],
                                     [UIImage imageNamed:@"dicy-die1"],
                                     [UIImage imageNamed:@"dicy-die4"],
                                     [UIImage imageNamed:@"dicy-die3"],
                                     [UIImage imageNamed:@"dicy-die5"],
                                     [UIImage imageNamed:@"dicy-die2"],
                                     [UIImage imageNamed:@"dicy-die1"],
                                     [UIImage imageNamed:@"dicy-die6"],
                                     [UIImage imageNamed:@"dicy-die3"],
                                     [UIImage imageNamed:@"dicy-die5"],
                                     [UIImage imageNamed:@"dicy-die2"],
                                     [UIImage imageNamed:@"dicy-die3"], nil];
        dieImages.animationDuration = 1.0f;
        dieImages.animationRepeatCount = 1;

    }


}

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

- (void) rollDice{
    self.diceOutletArray = [[NSArray alloc] initWithObjects:self.dieImage0, self.dieImage1, self.dieImage2, self.dieImage3, self.dieImage4, self.dieImage5, self.dieImage6, self.dieImage7, nil];

    // Randomly set the image of the dice
    for (UIImageView *numberImage in self.diceOutletArray) {

        numberImage.image = [self.randomImages randomNumber];
        [numberImage startAnimating];

    }

}

/* Motion functions *****************************************************************************************/

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion == UIEventSubtypeMotionShake) {
        [self rollDice];
        NSLog(@"There was a bump!: Line 85");
    }
    NSLog(@"Motion Ended: Line 87");
}

- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}

/* Roll Button **********************************************************************************************/

- (IBAction)rollDiceButton {
    [self rollDice];
    NSLog(@"Ouch! Somebody poked me!: Line 97");
}


/* Segues **************************************************************************************************/



@end

RandomImages.m

#import "RandomImages.h"

@implementation RandomImages
@synthesize diceImages;

- (instancetype)init
{
    self = [super init];
    if (self) {
        // Array of dice for the randomNumber method
        self.diceImages = [[NSArray alloc] initWithObjects:
                       [UIImage imageNamed:@"dicey-die1"],
                       [UIImage imageNamed:@"dicey-die2"],
                       [UIImage imageNamed:@"dicey-die3"],
                       [UIImage imageNamed:@"dicey-die4"],
                       [UIImage imageNamed:@"dicey-die5"],
                       [UIImage imageNamed:@"dicey-die6"], nil];
    }
    return self;
}


// Random dice number method
- (UIImage *)randomNumber {
    int random = arc4random_uniform((int)self.diceImages.count);
    return [self.diceImages objectAtIndex:random];
}

@end

RandomImages.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface RandomImages : NSObject

@property (strong, nonatomic) NSArray *diceImages;

- (UIImage *) randomNumber;

@end

Selecting a random image does work.

I did try this in my rollDice method and nothing changed:

dispatch_async(dispatch_get_main_queue(), ^{
        numberImage.image = [self.randomImages randomNumber];
        [numberImage startAnimating];
    });

I believe it's a typo.
I've created the project and your code working ok.
I'll share my local project if you want.

Look out for image file names.
@"dicey-die..." and @"dicy-die..."

I believe @"dicy-die..." is not added in the project.

Update:
github list for my project
https://github.com/rishi420/TestAnimationImage/tree/master

You should only have ONE UIImageView. Not 8.

The whole point of animated images it that it uses one UI element (a UIImageView) and then loops through the images.

self.myONEImageView.animationImages = [[NSArray alloc] initWithObjects:
                                 [UIImage imageNamed:@"dicy-die5"],
                                 [UIImage imageNamed:@"dicy-die6"],
                                 [UIImage imageNamed:@"dicy-die1"],
                                 [UIImage imageNamed:@"dicy-die4"],
                                 [UIImage imageNamed:@"dicy-die3"],
                                 [UIImage imageNamed:@"dicy-die5"],
                                 [UIImage imageNamed:@"dicy-die2"],
                                 [UIImage imageNamed:@"dicy-die1"],
                                 [UIImage imageNamed:@"dicy-die6"],
                                 [UIImage imageNamed:@"dicy-die3"],
                                 [UIImage imageNamed:@"dicy-die5"],
                                 [UIImage imageNamed:@"dicy-die2"],
                                 [UIImage imageNamed:@"dicy-die3"], nil];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM