1

I am trying to animate a UIView here. It looks like a rectangle, and I just want to translate it to my coordinations.

So, how can I animate it? I tried to find some tutorials, but without success.

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Lucas Veiga
  • 1,758
  • 7
  • 27
  • 45
  • Please Refer this link for UIView Animation http://stackoverflow.com/questions/630265/iphone-uiview-animation-best-practice – Dsh Feb 03 '14 at 09:21

5 Answers5

22

In iOS 4, the UIView block animation method is easiest:

[UIView animateWithDuration:1.0 animations:^{
    myView.frame = myNewFrameRect;
}];

http://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/clm/UIView/animateWithDuration:animations:

Stuart
  • 36,683
  • 19
  • 101
  • 139
Tony
  • 411
  • 2
  • 4
15

Here is an example of animating a view to move off screen. You should be able to slightly adjust it for your needs:


[UIView beginAnimations:@"bucketsOff" context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDelegate:self];
//position off screen
[bucketView setCenter:CGPointMake(-160, 377)];
[UIView setAnimationDidStopSelector:@selector(finishAnimation:finished:context:)];
//animate off screen
[UIView commitAnimations];
Will
  • 2,082
  • 2
  • 16
  • 13
  • No luck. I declaring the 'myView' in header file. I put it on viewDidLoad as [myView setCenter:CGPointMake(-160, 377)]; – Lucas Veiga Nov 11 '10 at 16:57
  • If you declare a line outside of an animation block, it will have no effect on the animation whatsoever. – Ken Jun 01 '11 at 23:24
  • This works if you put it within `viewWillAppear`. It also supports auto layout, meaning I had a box centered vertically and horizontally, but changing the center worked perfectly to animate it. – Scott Fister Nov 19 '15 at 05:07
5
//try this AnimationTransitionCurlDown \UP

-(void)pageDown
{
 [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self cache:YES];

        [UIView commitAnimations];
}

-(void)pageUP
{
[UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self cache:YES];

        [UIView commitAnimations];
}

//flip uiviews  using animation



-(void)FlipFromLeft
{
[UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self cache:YES];

        [UIView commitAnimations];
}

-(void)FlipFromRight
{
[UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];

        [UIView commitAnimations];
}
NANNAV
  • 4,875
  • 4
  • 32
  • 50
4

Use "QuartzCore.framework" With these animations.

-(void)viewSlideInFromRightToLeft:(UIView *)views
{
    CATransition *transition = nil;
    transition = [CATransition animation];
    transition.duration = 0.5;//kAnimationDuration
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype =kCATransitionFromRight;
    transition.delegate = self;
    [views.layer addAnimation:transition forKey:nil];
}

-(void)viewSlideInFromLeftToRight:(UIView *)views
{
    CATransition *transition = nil;
    transition = [CATransition animation];
    transition.duration = 0.5;//kAnimationDuration
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype =kCATransitionFromLeft;
    transition.delegate = self;
    [views.layer addAnimation:transition forKey:nil];
}

-(void)viewSlideInFromTopToBottom:(UIView *)views
{
    CATransition *transition = nil;
    transition = [CATransition animation];
    transition.duration = 0.5;//kAnimationDuration
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype =kCATransitionFromBottom ;
    transition.delegate = self;
    [views.layer addAnimation:transition forKey:nil];
}

-(void)viewSlideInFromBottomToTop:(UIView *)views
{
    CATransition *transition = nil;
    transition = [CATransition animation];
    transition.duration = 0.5;//kAnimationDuration
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype =kCATransitionFromTop ;
    transition.delegate = self;
    [views.layer addAnimation:transition forKey:nil];
}
Vaibhav Sharma
  • 1,123
  • 10
  • 22
  • For AnimationSubType you can use following option:-> kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom – Vaibhav Sharma May 10 '14 at 14:33
  • For AnimationType you can use following option:-> kCATransitionMoveIn, kCATransitionPush, kCATransitionReveal, kCATransitionFade – Vaibhav Sharma May 10 '14 at 14:34
0
[UIView beginAnimations:@"your text" context:nil];
[UIView setAnimationDuration:0.4]; //Your animation duration
[UIView setAnimationDelegate:self];
//
write your code here , what you want to animate
//
[UIView commitAnimations];

Working Fine definitely.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Dipak Narigara
  • 1,796
  • 16
  • 18