我在x = 0.0位置有一个UIButton。现在,当我按下该按钮时,该按钮正在移动并移至x = 131.0的位置。现在,当我再次按下该按钮时,它应该回到x = 0.0的位置。但是它不会到来。它停留在x = 131.0的位置。

这是我写的代码

- (IBAction)showMenuViewButton:(id)sender {

_horizontalConstraintsOfColorViewToMainView.constant = 0.0;
_buttonOfColorViewHoriozontalLeadingConstraints.constant = 131.0;

counter=counter+1;

if (counter%2 !=0) {
    [UIView animateWithDuration:0.4
                     animations:^
     {
         _menuView.hidden = NO;
         _menuView.frame = CGRectMake(0.0, _menuView.frame.origin.y, _menuView.frame.size.width, _menuView.frame.size.height);
         _showMenuViewButton.frame = CGRectMake(_showMenuViewButton.frame.origin.x+_menuView.frame.size.width+_showMenuViewButton.frame.size.width,_showMenuViewButton.frame.origin.y, _showMenuViewButton.frame.size.width, _showMenuViewButton.frame.size.width);


     }];
}
else{

    [UIView animateWithDuration:0.4
                     animations:^
     {
         _menuView.hidden = NO;

         _menuView.frame = CGRectMake(-102.0, _menuView.frame.origin.y, _menuView.frame.size.width, _menuView.frame.size.height);
         _showMenuViewButton.frame =    CGRectMake(0.0,_showMenuViewButton.frame.origin.y,     _showMenuViewButton.frame.size.width, _showMenuViewButton.frame.size.width);

     }];

}
}

最佳答案

在这里,我更改了动画的按钮框架,但未更改任何约束

 - (IBAction)clickButton:(id)sender {
        if (!isChangedPlace) {
            isChangedPlace = true;
            [UIView animateWithDuration:1.0
                                      animations:^{

 btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y+100, btn.frame.size.width, btn.frame.size[self.height);view layoutIfNeeded];
                                      }];
        }
        else
        {
            isChangedPlace = false;
            [UIView animateWithDuration:1.0
                             animations:^{
btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y-100, btn.frame.size[self.width,view btn.frame.size.height);layoutIfNeeded];
                             }];
        }

    }

在这里,我为动画设置了更改按钮Y约束
我已经使用IBOutlet设置了Y约束,并如下更改该值。

    - (IBAction)clickButton:(id)sender {
        if (!isChangedPlace) {

            isChangedPlace = true;
             buttonYPointConstraint.constant += 50;

        }
        else
        {
            isChangedPlace = false;
            buttonYPointConstraint.constant -= 50;

        }
 [UIView animateWithDuration:1.0  animations:^{
   [self.view layoutIfNeeded];
                                      }];
    }

希望对您有帮助!

关于ios - 按钮的框架没有变化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31246414/

10-12 00:34
查看更多