iOS translation and scale animation

前端 未结 4 926
日久生厌
日久生厌 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: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.

提交回复
热议问题