Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Caleb Kleveter
Treehouse Moderator 37,862 PointsUIImageViews won't animate in an image based animation. [Solved]
UIImageViews won't animate in an image based animation. [Objective-C]
I have 8 UIImageViews that were created in the storyboard that I am trying to animate with an image based animation.
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
For some reason the animation doesn't work.
Note: I have done this in Swift, and I do have another class for the dice image randomization, if you don't find a bug in here then I can give you that code.
2 Answers
Gabe Nadel
Treehouse Guest TeacherDon't you need to call:
[dieImages startAnimating]
..to actually begin the animation?
I think it should be the last line of your For-In Loop, no?
Caleb Kleveter
Treehouse Moderator 37,862 PointsI finally got the answer on this SO post. Problem solved!