开发者

how to control the animationImages?

i use the following code

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;
    [imgvie开发者_如何学运维w startAnimating];
    [self.view addSubview:imgview];

here imgview is UIImageView, but now i want to use a IBAction of UIButton for control this. The default image is 1.png, when i click the button, the imgview will show the next image. such as when the current image is 1.png, it will show 2.png when i clicked the button, it will show 3.png when i clicked the button again, and go on. if the current image is 5.png, it will show 1.png when i clicked the button, so how to do this?


You can save the the current image in a property in your viewController such as currentImage, this can be an int with the position on the array, then you can simply get the currentImage + 1 from the array. Something like this:

// 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++;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜