2

I wrote this code and made 2 buttons, next and back, to display a picture in an NSMutubleArray using an UIImageView.

-(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--;


}

This works, but is it possible to make a little animation when changing image?

dredful
  • 4,378
  • 2
  • 35
  • 54
user765110
  • 83
  • 1
  • 5

1 Answers1

4

There are a lot of animation options to choose from. Since you have a single UIImageView object, here is a "fade out/in" example (warning: not tested):

-(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];
}

Then in your pictureNext and pictureBack you can change the line:

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

to these 3 lines:

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

Side note:

You could combine your pictureNext and pictureBack by having both buttons call the same selector and determining if you sender is the back or forward button.

-(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
    .
    .
    .
}
dredful
  • 4,378
  • 2
  • 35
  • 54