7

I've been searching for hours trying to find a way to animate/rotate a UIView 90 degrees from the upper right corner.

The effect should almost work like a swinging door from the top of the screen.

Hope someone can help!

Andrew
  • 3,874
  • 5
  • 39
  • 67
dandax
  • 1,267
  • 3
  • 15
  • 22

3 Answers3

25

So right after I pressed enter I suddenly put two and two together and figured the Metronome sample worked kind of like a swinging door and that led me to a few other possibilities.

Here's my solution:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Set the anchor point and center so the view swings from the upper right
    swingView.layer.anchorPoint = CGPointMake(1.0, 0.0);
    swingView.center = CGPointMake(CGRectGetWidth(self.view.bounds), 0.0);

    // Rotate 90 degrees to hide it off screen
    CGAffineTransform rotationTransform = CGAffineTransformIdentity;
    rotationTransform = CGAffineTransformRotate(rotationTransform, DegreesToRadians(90));
    swingView.transform = rotationTransform;
}

...

- (void)animateSwing {

    CGAffineTransform swingTransform = CGAffineTransformIdentity;
    swingTransform = CGAffineTransformRotate(swingTransform, DegreesToRadians(0));

    [UIView beginAnimations:@"swing" context:swingView];
    [UIView setAnimationDuration:0.25];

    swingView.transform = swingTransform;

    [UIView commitAnimations];
}

Hope this helps someone else too!

dandax
  • 1,267
  • 3
  • 15
  • 22
  • BTW, this answer is what led me to a different animation from the Metronome sample. I think it's a little cleaner. http://stackoverflow.com/questions/929364/how-to-create-iphones-wobbling-icon-effect/930101#930101 – dandax Nov 02 '10 at 13:10
  • 2
    #define DEGREES_RADIANS(angle) ((angle) / 180.0 * M_PI) – jeet.chanchawat Sep 04 '14 at 11:47
0

You should try setting the anchor point of the layer to (0,1), and than animate the layer.

tadejsv
  • 2,085
  • 1
  • 18
  • 20
  • I figured out that layer.anchorPoint and view.center were the culprits shortly after I posted my question. Thanks for the quick response! – dandax Nov 02 '10 at 13:06
-4

You should try this code:

-(void)doRotationView{
[UIView beginAnimations:@"flipping view" context:nil];  
    [UIView setAnimationDuration:1];    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];   
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft    
                           forView:self cache:YES];
    if (flagFront == 1) {
        flagFront =0;
        [self.viewSecond setHidden:NO];
        [self.viewFirst setHidden:YES];
    }
    else{
        flagFront =1;
        [self.viewSecond setHidden:YES];
        [self.viewFirst setHidden:NO];        
    }
    [UIView commitAnimations];

}

zachjs
  • 1,738
  • 1
  • 11
  • 22
nghiamas
  • 1
  • 3