当按下UIBarButton时,UIView会快速回到原始帧

时间:2016-01-20 03:55:14

标签: xcode swift animation uibarbuttonitem uitoolbar

在我的UIView的顶部叫做" courseView",我有一个带有条形按钮的工具栏,名为"展开按钮"。当我按下该按钮时,将调用expandCourseView()并且视图框正确动画。但是在动画完成后,我想要" expandButton"改变文字。当它执行此操作时,视图框架会自动捕捉到其原始形状。为什么会这样?似乎工具栏上的按钮没有注册其位置已移位。

@IBOutlet weak var expandButton: UIBarButtonItem!

@IBAction func expandButton(sender: AnyObject) {
    expandCourseView()
}

func expandCourseView(){
    let width = self.courseView.frame.width
    let height = self.courseView.frame.height
    let frameOffset : CGFloat = 200
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        if (height < 211){
            self.courseView.frame = CGRect(x: self.courseView.frame.minX, y: self.courseView.frame.minY - frameOffset, width: width, height: height + frameOffset)
        }
        else {
            self.courseView.frame = CGRect(x: self.courseView.frame.minX, y: self.courseView.frame.minY + frameOffset, width: width, height: height - frameOffset)
        }
        }) { (value: Bool) -> Void in
            if height < 211{
                self.expandButton.title = "Close"
            } else {
                self.expandButton.title = "Expand"
            }
    }
}

1 个答案:

答案 0 :(得分:1)

您可能正在使用自动布局。虽然您可以直接操作帧以设置动画或移动视图,但在某些时候(例如设置标签的文本值)会调用setNeedsLayout()并使视图恢复为约束指定的值。 / p>

您可以按原样保留动画,但在完成时(或在updateConstraints()中),您需要修改约束以将视图保持在新位置。