如何在更改UIImageView时制作动画

时间:2011-05-23 15:33:56

标签: iphone objective-c uiimageview

我编写了这段代码,并使用UIImageView在NSMutubleArray中显示了一个图片,下一页和后面两个按钮。

-(IBAction) pictureNext:(id)sender;
{
    if (pictureIndice==[promMUAraay count]) 
    {
        pictureIndice=0;
    }

    NSLog(@"indice : %d ",pictureIndice);
    Promotion *prom = [[Promotion alloc]init];
    prom =[promMUAraay objectAtIndex:pictureIndice];
    promotionPicture.image = [UIImage imageWithData:prom.pPicture];
    //promLabel.text=prom.pName;
    NSLog(@"label : %@",prom.pName);
    pictureIndice++;
    }

-(IBAction) pictureBack:(id)sender;
{
    if (pictureIndice==[promMUAraay count]) 
    {
        pictureIndice=0;
    }

    NSLog(@"indice : %d ",pictureIndice);
    Promotion *prom = [[Promotion alloc]init];
    prom =[promMUAraay objectAtIndex:pictureIndice];
    promotionPicture.image = [UIImage imageWithData:prom.pPicture];
    //promLabel.text=prom.pName;
    if (pictureIndice==0) 
    {
        pictureIndice=[promMUAraay count];
    }
    pictureIndice--;


}

这有效,但是在更改图像时是否可以制作一些动画?

1 个答案:

答案 0 :(得分:4)

有很多动画选项可供选择。由于您有一个UIImageView对象,这里有一个“淡出/进入”示例(警告:未经测试):

-(void)fadeView:(UIView *)thisView fadeOut:(BOOL)fadeOut {
    //   self.ReviewViewController.view.alpha = 1;

    [UIView beginAnimations:@"fadeOut" context:nil]; 
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];

    if (fadeOut) {
        thisView.alpha = 0;
    } else {
        thisView.alpha = 1;
    }
    [UIView commitAnimations];
}

然后在pictureNextpictureBack中,您可以更改该行:

   promotionPicture.image = [UIImage imageWithData:prom.pPicture];

到这3行:

   [self fadeView:promotionPicture fadeOut:YES];
   promotionPicture.image = [UIImage imageWithData:prom.pPicture];
   [self fadeView:promotionPicture fadeOut:NO];

旁注:

您可以将pictureNextpictureBack合并,让两个按钮调用相同的选择器,并确定sender是后退还是前进按钮。

-(IBAction)pictureMove:(id)sender {
    if (sender == yourNextButton) {
        //do next index math here
    } else if (sender == yourBackButton) {
        //do back index math here
    }
    .
    .
    .
    //you image change code goes here
    .
    .
    .
}