iOS translation and scale animation

前端 未结 4 910
日久生厌
日久生厌 2020-12-08 03:12

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.tra         


        
相关标签:
4条回答
  • 2020-12-08 03:24

    I have used your code and its moving from bottom center to to bottom left perfectly. Might be your superview isn't in proper rect. Please check.

    0 讨论(0)
  • 2020-12-08 03:34

    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.

    0 讨论(0)
  • 2020-12-08 03:39

    This phenomenon indicates that your button's original frame is wrong, probably because of its auto-resizing. Try setting its frame to the bottom center before you start the animation.

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        _menuBtn.superview.frame = CGRectMake(0, 513, 320, 30); // This is a quick and dirty solution to make sure your button's superview is in the right place. You probably don't want to do this and are more likely to review your view hierarchy.
        _menuBtn.frame = CGRectMake(145, 0, 30, 30); // Set the frame to the bottom center, please ensure that _menuBtn's transform hasn't been changed before this step
        _menuBtn.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
        [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];
    }
    
    0 讨论(0)
  • 2020-12-08 03:40

    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];
    
    0 讨论(0)
提交回复
热议问题