I'm working on UIButton animation where:
The UIButton
is set in the bottom center of the screen and scaled to a small size
_menuBtn.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
When the app starts it should be moving to the bottom left side of the screen as it scales or grow to its original size.
- (void)viewDidLoad { [super viewDidLoad]; _menuBtn.frame = CGRectMake(160, 513, 30, 30); _menuBtn.superview.frame = CGRectMake(160, 513, 30, 30); _menuBtn.transform = CGAffineTransformMakeScale(0.1f, 0.1f); NSLog(@"_menuBtn: %@ ; _menuBtn.superview: %@", _menuBtn, _menuBtn.superview); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:5]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; CGAffineTransform scaleTrans = CGAffineTransformMakeScale(1.0f, 1.0f); CGAffineTransform lefttorightTrans = CGAffineTransformMakeTranslation(-200.0f,0.0f); _menuBtn.transform = CGAffineTransformConcat(scaleTrans, lefttorightTrans); [UIView commitAnimations]; }
problem
When the animation starts the button starts moving from the bottom right side of the screen and not in the bottom center where it is and should be. Any help ?
Log Result
NSLog(@"%@", _myBtn); 2013-08-14 09:22:38.913 GJCoolNavi[339:c07] <UIButton: 0x813ea30; frame = (0 0; 0 0); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x813eaf0>>
thats before doing the animation...and the result after the animation is:
2013-08-14 09:30:25.719 GJCoolNavi[612:c07] <UIButton: 0x71206d0; frame = (160 294; 0 0); opaque = NO; autoresize = TM+BM; animations = { transform=<CABasicAnimation: 0x7536a80>; position=<CABasicAnimation: 0x7537dd0>; }; layer = <CALayer: 0x7120790>>
Animation is a critical part of your iOS user interfaces, giving apps the ability to draw user attention to particular areas. Using the right animation will not only improve user experience, but can also add a ton of fun and polish to your app.
Creating Constructor () in your App class and make a State named as animationValue using Animated.value () method and assign default value as 1. This would used to set our default Scale value as 1, So the View will be same as defined width and height. 4. Create a function named as startScaleAnimation ().
Create a function named as startScaleAnimation (). We would call this function on TouchableWithoutFeedback onPress event to perform animation. We are also calling the animation again so it will first get bigger then again to normal size.
Translation changes the origin, or 0,0point, of the canvas’s coordinate system. Call the context’s translate(x,y)method to make the point x,ythe new origin. One use for translation is to redraw a path at a new location without having to respecify the endpoints.
Why don't you do this...
_menuBtn.transform = CGAffineTransformMakeScale(0.1, 0.1); [UIView animateWithDuration:5.0 options:UIViewAnimationOptionCurveEaseOut animations:^(){ _menuBtn.transform = CGAffineTransformMakeScale(1.0, 1.0); _menuBtn.center = self.view.center; } completion:nil];
I'd avoid moving stuff using a transform. Change the frame instead.
EDIT
- (void)viewDidLoad { [super viewDidLoad]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // for convenience I'm pulling these values out in to variables. float buttonWidth = _menuBtn.frame.size.width; float buttonHeight = _menuBtn.frame.size.height; float viewWidth = self.view.frame.size.width; float viewHeight = self.view.frame.size.height; // set the button frame to be the bottom center // note you shouldn't have to do this as Interface Builder should already place it there. // place the button in the horizontal center and 20 points from the bottom of the view. _menuBtn.frame = CGRectMake((viewWidth - buttonWidth) * 0.5, viewHeight - buttonHeight - 20, buttonWidth, buttonHeight); // scale the button down before the animation... _menuBtn.transform = CGAffineTransformMakeScale(0.1, 0.1); // now animate the view... [UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ _menuBtn.transform = CGAffineTransformIdentity; _menuBtn.frame = CGRectMake(viewWidth - buttonWidth - 20, viewHeight - buttonHeight - 20, buttonWidth, buttonHeight); } completion:nil]; }
Try this and let me know what happens.
This has solved a similar problem.
Apply a (UIButton).imageView Animation.
[UIView beginAnimations:nil context:nil]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:5]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; CGAffineTransform scaleTrans = CGAffineTransformMakeScale(1.0f, 1.0f); CGAffineTransform lefttorightTrans = CGAffineTransformMakeTranslation(-200.0f,0.0f); _menuBtn.imageView.transform = CGAffineTransformConcat(scaleTrans, lefttorightTrans); [UIView commitAnimations];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With