我的动画无法启动

时间:2013-01-14 22:50:17

标签: objective-c animation uikit

当我点击一个按钮时,我已经完成了这个代码来启动动画,当我点击按钮时应该改变按钮所说的内容并启动动画,但它只会改变按钮所说的内容?我的代码有什么问题吗?

@synthesize SwishImage;
@synthesize SwishButton;

- (IBAction) Swish {
if (SwishImage.isAnimating) {
    [SwishImage stopAnimating];
    [SwishButton setTitle:@"Swish" forState:UIControlStateNormal];
} else {
    [SwishImage startAnimating];
    [SwishButton setTitle:@"Searching" forState:UIControlStateNormal];
}
}
- (void)viewdidload
{

NSArray * SwishArray;
SwishArray = [[NSArray alloc] initWithObjects:
                   [UIImage imageNamed:@"Orange_dot.png"],
                   [UIImage imageNamed:@"1.png"],
                  [UIImage imageNamed:@"2.png"],
                   [UIImage imageNamed:@"3.png"],
                   [UIImage imageNamed:@"4.png"],
                   nil];
    SwishImage.animationImages=SwishArray;
    SwishImage.animationDuration = 1;
    SwishImage.animationRepeatCount = 0;


[super viewDidLoad];
}

1 个答案:

答案 0 :(得分:2)

当我将其复制到一个空的Xcode项目并在Interface Builder中正确链接时,图像视图开始动画化。我假设Interface Builder中没有正确链接。

话虽如此,从标准的角度来看,您的代码有点尴尬。变量名称应以小写字母开头(包括属性)。你不再需要@synthesize了,NSArray有一个新的快捷方式。以下是您的课程的外观:

ViewController.h

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *swishImage;
@property (strong, nonatomic) IBOutlet UIButton *swishButton;
@end

ViewController.m

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *swishArray = @[
               [UIImage imageNamed:@"Orange_dot.png"],
               [UIImage imageNamed:@"1.png"],
               [UIImage imageNamed:@"2.png"],
               [UIImage imageNamed:@"3.png"],
               [UIImage imageNamed:@"4.png"],
    ];
    self.swishImage.animationImages=swishArray;
    self.swishImage.animationDuration = 1;
    self.swishImage.animationRepeatCount = 0;
}

- (IBAction)swish:(id)sender {
    if (self.swishImage.isAnimating) {
        [self.swishImage stopAnimating];
        [self.swishButton setTitle:@"Swish" forState:UIControlStateNormal];
    } else {
        [self.swishImage startAnimating];
        [self.swishButton setTitle:@"Searching" forState:UIControlStateNormal];
    }
}

@end

然后只需选择图像视图并转到Connections Inspector。您应该看到标题为Referencing Outlets的部分,确保swishImage已连接到您的视图控制器。