views:

28

answers:

1

Hi

I'm trying to represent a view that rotates on the iphone screen. I have a button and when you press it, the view rotates 180 degrees.

My problem is that this only works the first time.

Here is the code:

-(IBAction) flip:(id)sender{

    CGAffineTransform transform; //the transform matrix to be used below

    //BEGIN ANIMATIONS
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:2.0];

    //animate 
    if (flag){
        transform = CGAffineTransformMakeRotation( RADIANS(180) );
    } else {
        transform = CGAffineTransformMakeRotation( RADIANS(-180) );
    }
    flag = !flag;
    transform = CGAffineTransformTranslate(transform, 0, 0);
    self.mySuview.transform = transform;

    //COMMIT ANIMATIONS
    [UIView commitAnimations];

}

The first time you click, the view spins alright, but when you click again NOTHING happens. No errors, no changes on the view.

What am I missing?

Thanks Gonso

A: 

You are basically setting the transform to be the same both times. In your else {} statement, use:

transform = CGAffineTransformMakeRotation( RADIANS(0) );

As is, your code is not taking the 180 and adding -180, it's taking the 180 and setting it to -180.

Matt