У меня есть 8 UIImageView
s, которые были созданы в раскадровке, которую я пытаюсь оживить с помощью анимации на основе изображения, но по какой-то причине изображения не будут анимированы.UIImageView не будет анимироваться с анимацией на основе изображения
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
Выбор случайного изображения действительно работает.
Я попробовать это в моем rollDice
метод и ничего не изменилось:
dispatch_async(dispatch_get_main_queue(), ^{
numberImage.image = [self.randomImages randomNumber];
[numberImage startAnimating];
});
Почему вы установили как '' animationImages' и image' на каждый вид изображения? Вы должны использовать только тот или иной, а не тот и другой. – rmaddy
Неужели я это делаю? Вы говорите о цикле for, были ли ImageViews присвоено имя «dieImages»? (Я правильно это сказал?) –
В 'viewDidLoad' вы устанавливаете' animationImages' для всех видов изображений. Затем в 'rollDice' вы устанавливаете' image' для всех видов изображений. – rmaddy