ios - UIView Animate 只有在 IBAction 中时才会动画。我如何让它在我的情况下动画?

标签 ios swift

func pickWash() {

    bottomChange = self.mapView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -350.0)
    bottomChange.isActive = true

    UIView.animate(withDuration: 10.0, animations: {
        self.view.layoutIfNeeded()
        self.waterlessLabel.isHidden = false
        self.exteriorInterior.isHidden = false
        self.exteriorOnly.isHidden = false
        self.info.isHidden = false
        self.blackLine.isHidden = false
        self.extIntPrice.isHidden = false
        self.extPrice.isHidden = false
        self.confirmWash.isHidden = false
        self.when.isHidden = false
        self.timeChoice.isHidden = false

    }, completion: nil)

}

func tester(){
   self.pickWash()
}

实际上,我代码中的测试方法使用的是 Google 的 Place 自动完成 iOS,但我不想用无用的自动完成代码淹没我的代码。因此,当用户完成在 Google 的自动完成中输入他们的位置时,函数 pickwash() 被调用并且动画不起作用。只有当我在带有按钮的 IBAction 中使用它时,它才对我有用。有什么想法吗?

最佳答案

pickWash() 正在主线程以外的线程上(如 OP 评论中所确认)并且由于主线程是唯一允许在 UI 上工作的线程,因此行为未定义(这里没有任何反应)。您必须使用

将代码执行移动到主线程
func pickWash() {
    // Code here is on a non-main thread
    DispatchQueue.main.async {
        // Code here is executed on the main thread 
        bottomChange = self.mapView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -350.0)
        bottomChange.isActive = true

        UIView.animate(withDuration: 10.0, animations: {
            self.view.layoutIfNeeded()
            self.waterlessLabel.isHidden = false
            self.exteriorInterior.isHidden = false
            self.exteriorOnly.isHidden = false
            self.info.isHidden = false
            self.blackLine.isHidden = false
            self.extIntPrice.isHidden = false
            self.extPrice.isHidden = false
            self.confirmWash.isHidden = false
            self.when.isHidden = false
            self.timeChoice.isHidden = false

        }, completion: nil)
    }
}

func tester(){
   self.pickWash()
}

关于ios - UIView Animate 只有在 IBAction 中时才会动画。我如何让它在我的情况下动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50070081/

相关文章:

ios - ld : framework not found FileProvider for architecture x86_64 in Xcode 8. 3.2

ios - 无法使用 Int.random(in : x. ..y) 在 Swift 中随机化数组

objective-c - 容器 View Controller 示例

ios - 访问多维数组的项目缓慢

ios - 为什么我的 datePicker 没有显示?

ios - 使用 AFNetworking 的 iOS 应用程序的 MVC 结构

ios - Swft3 (RxSwift, RxCocoa) - 使用响应式(Reactive)编程的 TableView 展开和折叠概念

swift - 尝试在 macOS 中从粘贴板获取文本时出现错误

ios - OpenGL ES 2.0 损失图像质量

iOS 表格 View 单元格随机为空