我在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);
}];
}
}
答案 0 :(得分:1)
这里我更改了动画的按钮框架而不更改任何约束
- (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];
}];
}
我希望它会对你有所帮助!