繁体   English   中英

动画框架偶尔会起作用

[英]Animating frame occasionally works

这是我的功能,动画我的观点

func animateViewMoving (up:Bool, moveValue :CGFloat) {

    let movementDuration:NSTimeInterval = 0.3
    let movement:CGFloat = ( up ? -moveValue : moveValue)
    self.view.setNeedsLayout()
    UIView.animateWithDuration(movementDuration, animations: {
    self.view.layoutIfNeeded()
    self.view.frame.origin.y += movement
    })

}

我在UITextFieldDelegate这两个函数上调用了这个函数

func textFieldDidBeginEditing(textField: UITextField) {

        animateViewMoving(true, moveValue: 200)

}

func textFieldDidEndEditing(textField: UITextField) {

        animateViewMoving(false, moveValue: 200)

}

问题在于 - 每当我第一次运行构建并打开视图时,视图会在按下textField时向上移动,并在编辑textField结束时向下移动。 但是,如果我返回上一个视图并再次进入视图,则按下textField时视图不会向上移动。 为什么animateWithDuration不规则地工作?

对不起,我不知道swift,所以我只想在Objective-C中编写我的解决方案。

我假设您正在使用AutoLayout。 如果是这种情况,我建议您在为视图设置动画时,为其设置约束,而不是视图的框架。 在我为视图框架设置动画之前,我遇到了很多问题。

这是一个示例:

- (void)moveSubview
{
    _subviewLeadingSuperLeading.constant = 20; //just a sample to move the subview horizontally.

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

     completion:^(BOOL isFinished)
     {
         //Do whatever you want
     }];
}

我希望这能帮到您。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM