如何控制animationImages?

时间:2011-07-05 14:33:37

标签: uiimageview uibutton

我使用以下代码

imgview.animationImages=[NSArray arrayWithObjects:
                                                     [UIImage imageNamed:@"1.png"],
                                                     [UIImage imageNamed:@"2.png"],
                                                     [UIImage imageNamed:@"3.png"],
                                                     [UIImage imageNamed:@"4.png"],
                                                     [UIImage imageNamed:@"5.png"],nil ];

    imgview.animationDuration=1.0;
    imgview.animationRepeatCount=0;
    [imgview startAnimating];
    [self.view addSubview:imgview];

这里的imgview是UIImageView,但现在我想使用UIButton的IBAction来控制它。默认图像是1.png,当我点击按钮时,imgview将显示下一个图像。 例如当前图像是1.png时,当我点击按钮时会显示2.png,当我再次点击按钮时会显示3.png,然后继续。如果当前图像是5.png,当我点击按钮时会显示1.png,那么怎么做呢?

1 个答案:

答案 0 :(得分:0)

您可以将当前图像保存在viewController的属性中,例如currentImage,这可以是一个带有数组位置的int,然后您可以简单地从数组中获取currentImage + 1。像这样:

// On view initialization
currentImage = 0;

NSArray myArray = [NSArray arrayWithObjects:
                                                 [UIImage imageNamed:@"1.png"],
                                                 [UIImage imageNamed:@"2.png"],
                                                 [UIImage imageNamed:@"3.png"],
                                                 [UIImage imageNamed:@"4.png"],
                                                 [UIImage imageNamed:@"5.png"],nil ];

imgview.animationImages = myArray;

//callback from UIButton when pressed.
if (currentImage + 1 >= [myArray count]) {
    currentImage = 0;
}

UIImage myDisplayedImage = [myArray objectAtIndex:currentImage];
currentImage++;