Ios 如何在Swift 4中设置UIImage约束的动画

Ios 如何在Swift 4中设置UIImage约束的动画,ios,animation,uiimage,constraints,swift4,Ios,Animation,Uiimage,Constraints,Swift4,我正在制作一个卡片应用程序,我需要制作一个动画,这样一张卡片就会改变它的约束条件,移动到另一个地方。如何为UIImage执行此操作。钩住UIImageView的前导、尾随、中心或中心约束,然后执行此操作 self.imageLeadCon.constant = // value UIView.animate(withDuration: 3.0 , animations: { self.view.layoutIfNeeded() // animation }, complet

我正在制作一个卡片应用程序,我需要制作一个动画,这样一张卡片就会改变它的约束条件,移动到另一个地方。如何为UIImage执行此操作。

钩住UIImageView的前导、尾随、中心或中心约束,然后执行此操作

self.imageLeadCon.constant = // value

UIView.animate(withDuration: 3.0 ,  animations: {

   self.view.layoutIfNeeded()

    // animation
}, completion: { _ in

    // completion
})

钩住UIImageView的前导、尾随、CenterX或CenterY约束并执行此操作

self.imageLeadCon.constant = // value

UIView.animate(withDuration: 3.0 ,  animations: {

   self.view.layoutIfNeeded()

    // animation
}, completion: { _ in

    // completion
})

我认为你把
UIImage
UIImageView
搞混了。第一个是图像的数据表示,第二个是显示
UIImage
的视图

因此,我猜您希望在
UIImageView
中移动。为此,请获取对约束的引用(例如,通过ctrl键从情节提要中的约束拖动到
UIViewController
实例)

之后,可以更新动画块中的约束,如下所示:

// Make sure the view is laid out as Mischanya Schtrigel stated
self.view.layoutIfNeeded() 
UIView.animate(withDuration: 0.5) { // duration in seconds
    myConstraint.constant = 50 // update your constraint here
    self.view.layoutIfNeeded()
}

与其他答案相反,我喜欢将约束更改放在动画闭包中,以使动画更清晰。

我认为您将
UIImage
UIImageView
混淆了。第一个是图像的数据表示,第二个是显示
UIImage
的视图

因此,我猜您希望在
UIImageView
中移动。为此,请获取对约束的引用(例如,通过ctrl键从情节提要中的约束拖动到
UIViewController
实例)

之后,可以更新动画块中的约束,如下所示:

// Make sure the view is laid out as Mischanya Schtrigel stated
self.view.layoutIfNeeded() 
UIView.animate(withDuration: 0.5) { // duration in seconds
    myConstraint.constant = 50 // update your constraint here
    self.view.layoutIfNeeded()
}

与其他答案相反,我喜欢将约束更改放在动画闭包中,以便更清楚地显示将要设置的动画内容。

首先,您必须将约束拖放到视图控制器中,以创建一个出口。与使用
UIImageView
UITableView
等进行此操作的方式相同

事实上,对于那些看过WWDC(我想是2014年)的人来说,苹果已经解释了如何以正确的方式制作UI动画

首先,您必须调用
layoutifneed()
方法以确保视图中的所有内容都已布置好。可以更改约束后,然后在动画块中调用
layoutifneed()
方法以使用新值布局约束

所以代码应该是这样的:

view.layoutIfNeeded() // to make sure your view have laid out

self.constraint.constant = //change your constant to any value

UIView.animate(withDuration: 1.0) {
   self.view.layoutIfNeeded() //layout your constraint with the new value
}

首先,您必须将约束拖放到视图控制器中,以创建一个出口。与使用
UIImageView
UITableView
等进行此操作的方式相同

事实上,对于那些看过WWDC(我想是2014年)的人来说,苹果已经解释了如何以正确的方式制作UI动画

首先,您必须调用
layoutifneed()
方法以确保视图中的所有内容都已布置好。可以更改约束后,然后在动画块中调用
layoutifneed()
方法以使用新值布局约束

所以代码应该是这样的:

view.layoutIfNeeded() // to make sure your view have laid out

self.constraint.constant = //change your constant to any value

UIView.animate(withDuration: 1.0) {
   self.view.layoutIfNeeded() //layout your constraint with the new value
}

您可以更改UIImage的前导/尾随常量值,然后调用
UIView.animateWithDuration(duration){self.view.layoutIfNeeded()}
。请做些调查。您需要正确地学习它,而不是在这里询问它……您可以更改UIImage的前导/尾随常量值,然后调用
UIView.animateWithDuration(duration){self.view.layoutifneed()}
。请做些调查。您需要正确地学习它,而不是仅仅在这里问它…如果您以编程方式创建约束会怎么样。你怎么能改变它呢。我尝试过这样做:copyImages.imageViewBottom.layoutIfNeeded()self.topAnchor.constraint(equalTo:view.topAnchor,常量:20)。isActive=true,尽管它不起作用。如果以编程方式进行约束,会怎么样。你怎么能改变它呢。我尝试过这样做:copymages.imageViewBottom.layoutIfNeeded()self.topAnchor.constraint(equalTo:view.topAnchor,常量:20)。isActive=true,尽管它不起作用