编辑TextField将Animated UIView移动到其原始位置Swift

时间:2018-09-01 09:30:44

标签: swift swift3

在向上滑动时,我使用以下代码从屏幕底部制作了一个视图

UIView.animate(withDuration: 0.5, delay: 0.0, options: [], animations)

以及

self.view.layoutIfNeeded()

但是由于我的视图包含TextField,因此当我开始键入并按任意键时,视图将返回到开始时的原始位置。简而言之,我想动画视图的更新约束不会保留。

有人可以帮我解决这个问题吗?

@objc func handleSwipe(_ sender : UISwipeGestureRecognizer){
    UIView.animate(withDuration: 0.5, delay: 0.0, options: [], animations: {
        if sender.direction == .up{
            switch self.currentIndex {
            case 0:
                self.assetsView.frame = CGRect(x: self.assetsView.frame.origin.x, y: self.scrollUpY , width: self.assetsView.frame.size.width, height: self.assetsView.frame.size.height)
                self.liabilitiesView.frame = CGRect(x: self.liabilitiesView.frame.origin.x, y: self.scrollDownY , width: self.liabilitiesView.frame.size.width, height: self.liabilitiesView.frame.size.height)
                break
            case 1:
                self.assetsView.frame = CGRect(x: self.assetsView.frame.origin.x, y: self.originalTopY , width: self.assetsView.frame.size.width, height: self.assetsView.frame.size.height)
                self.liabilitiesView.frame = CGRect(x: self.liabilitiesView.frame.origin.x, y: self.scrollUpY , width: self.liabilitiesView.frame.size.width, height: self.liabilitiesView.frame.size.height)
                self.savingsView.frame = CGRect(x: self.savingsView.frame.origin.x, y: self.scrollDownY , width: self.savingsView.frame.size.width, height: self.savingsView.frame.size.height)
                break
            case 2:
                self.liabilitiesView.frame = CGRect(x: self.liabilitiesView.frame.origin.x, y: self.originalTopY , width: self.liabilitiesView.frame.size.width, height: self.liabilitiesView.frame.size.height)
                self.savingsView.frame = CGRect(x: self.savingsView.frame.origin.x, y: self.scrollUpY , width: self.savingsView.frame.size.width, height: self.savingsView.frame.size.height)
                break;
            case 3:
                break;
            default:
                QL1("Success")
            }
        }else if sender.direction == .down{
            switch self.currentIndex {
            case 0:
                self.assetsView.frame = CGRect(x: self.assetsView.frame.origin.x, y: self.scrollDownY , width: self.assetsView.frame.size.width, height: self.assetsView.frame.size.height)
                self.liabilitiesView.frame = CGRect(x: self.liabilitiesView.frame.origin.x, y: self.originalDownY , width: self.liabilitiesView.frame.size.width, height: self.liabilitiesView.frame.size.height)
                break
            case 1:
                self.assetsView.frame = CGRect(x: self.assetsView.frame.origin.x, y: self.scrollUpY , width: self.assetsView.frame.size.width, height: self.assetsView.frame.size.height)
                self.liabilitiesView.frame = CGRect(x: self.liabilitiesView.frame.origin.x, y: self.scrollDownY , width: self.liabilitiesView.frame.size.width, height: self.liabilitiesView.frame.size.height)
                self.savingsView.frame = CGRect(x: self.savingsView.frame.origin.x, y: self.originalDownY , width: self.savingsView.frame.size.width, height: self.savingsView.frame.size.height)
                break
            case 2:
                self.assetsView.frame = CGRect(x: self.assetsView.frame.origin.x, y: self.originalTopY , width: self.assetsView.frame.size.width, height: self.assetsView.frame.size.height)
                self.liabilitiesView.frame = CGRect(x: self.liabilitiesView.frame.origin.x, y: self.scrollUpY , width: self.liabilitiesView.frame.size.width, height: self.liabilitiesView.frame.size.height)
                self.savingsView.frame = CGRect(x: self.savingsView.frame.origin.x, y: self.scrollDownY , width: self.savingsView.frame.size.width, height: self.savingsView.frame.size.height)
                break;
            case 3:
                break;
            default:
                QL1("Success")
            }
        }
        self.view.layoutIfNeeded()
    }, completion: { (finished: Bool) in
        self.updateScreen(sender)
    })
}

谢谢。

Layout Constraints

1 个答案:

答案 0 :(得分:0)

您可以尝试CGAffineTransform。用于翻译;

moz-extension://

并返回到其原始位置;

UIView.animate(withDuration: 0.5, delay: 0.0, options: [], animations: {
    yourView.transform = CGAffineTransform(translationX: xOffset, y: yOffset)
}