【问题标题】:Editing TextField move Animated UIView to its original position Swift编辑 TextField 将 Animated UIView 移动到其原始位置 Swift
【发布时间】:2018-09-02 04:19:24
【问题描述】:

在向上滑动时,我使用以下代码从屏幕底部为视图设置了动画

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)
    })
}

提前致谢。

【问题讨论】:

  • 你能显示所有的代码吗?
  • @RakeshaShastri 我已经添加了我的动画代码。
  • 您在使用自动布局吗?请在视图中显示您的TextField 约束情况
  • 添加了约束,不,我没有使用自动布局
  • 您的文本字段代表中有什么?

标签: swift swift3


【解决方案1】:

你可以试试 CGAffineTransform。用于翻译;

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

并返回到原来的位置;

UIView.animate(withDuration: 0.5, delay: 0.0, options: [], animations: {
        yourView.transform = CGAffineTransform.identity
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2011-12-08
    相关资源
    最近更新 更多