Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何设置视图到X和Y中心的动画?_Ios_Swift_Uiviewanimation - Fatal编程技术网

Ios 如何设置视图到X和Y中心的动画?

Ios 如何设置视图到X和Y中心的动画?,ios,swift,uiviewanimation,Ios,Swift,Uiviewanimation,我有一个ImageView,它对助手标题视图具有centerX和centerY约束,如下图所示 我想设置徽标imageView的动画,使其位于屏幕中心,如下图所示: 我尝试了一些代码,但它不起作用,老实说,我不确定下面的代码,这里是我尝试的。也许你有更好的方法。这是我试过的 首先,我尝试为这两个约束提供标识符: 然后我移除约束并创建一个新的约束,如下面的代码所示: UIView.animate(withDuration: 1.1, delay: 0.5, usingSpringWithD

我有一个ImageView,它对
助手标题视图具有centerX和centerY约束,如下图所示

我想设置徽标imageView的动画,使其位于屏幕中心,如下图所示:

我尝试了一些代码,但它不起作用,老实说,我不确定下面的代码,这里是我尝试的。也许你有更好的方法。这是我试过的

首先,我尝试为这两个约束提供标识符:

然后我移除约束并创建一个新的约束,如下面的代码所示:

 UIView.animate(withDuration: 1.1, delay: 0.5, usingSpringWithDamping:  0.4, initialSpringVelocity: 0.0, animations: {

            // remove the initial constraint
            self.mainLogoCenterXConstraint.isActive = false
            self.mainLogoCenterYConstraint.isActive = false

            // create new constraint
            let newMainLogoCenterXConstraint = NSLayoutConstraint(
                item: self.mainLogoImageView,
                attribute: .centerX,
                relatedBy: .equal,
                toItem: self.view,
                attribute: .centerY,
                multiplier: 1.0,
                constant: 0)
            newMainLogoCenterXConstraint.identifier = "mainLogoCenterXConstraint"
            newMainLogoCenterXConstraint.isActive = true

            let newMainLogoCenterYConstraint = NSLayoutConstraint(
                item: self.mainLogoImageView,
                attribute: .centerY,
                relatedBy: .equal,
                toItem: self.view,
                attribute: .centerY,
                multiplier: 1.0,
                constant: 0)
            newMainLogoCenterYConstraint.identifier = "mainLogoCenterYConstraint"
            newMainLogoCenterYConstraint.isActive = true


            self.view.layoutIfNeeded()

        }, completion: nil)
但它崩溃并给出错误:

“NSInvalidLayoutConstraintException”,原因:“约束不正确” 关联不兼容类型的锚:
也许您有更好的方法来实现这一点……

您在创建将
self.mainlogimageview.centerX
关联到
self.view.centerY
的第一个约束时出错-将第二个约束更改为
self.view.centerX
。不能将x轴定位绑定到y轴定位

此外,请确保正确设置动画,检查我关于如何使用autolayout进行设置的答案

就你而言:

// remove the initial constraint
self.mainLogoCenterXConstraint.isActive = false
self.mainLogoCenterYConstraint.isActive = false

// create new constraint
let newMainLogoCenterXConstraint = NSLayoutConstraint(
            item: self.mainLogoImageView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: self.view,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0)
newMainLogoCenterXConstraint.identifier = "mainLogoCenterXConstraint"
newMainLogoCenterXConstraint.isActive = true

let newMainLogoCenterYConstraint = NSLayoutConstraint(
            item: self.mainLogoImageView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: self.view,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0)
newMainLogoCenterYConstraint.identifier = "mainLogoCenterYConstraint"
newMainLogoCenterYConstraint.isActive = true

self.view.setNeedsLayout()

UIView.animate(withDuration: 1.1, delay: 0.5, usingSpringWithDamping:  0.4, initialSpringVelocity: 0.0, animations: {

    self.view.layoutIfNeeded()

}, completion: nil)

您在创建将
self.mainlogimageview.centerX
self.view.centerY
关联的第一个约束时出错-将第二个约束更改为
self.view.centerX
。不能将x轴定位绑定到y轴定位

此外,请确保正确设置动画,检查我关于如何使用autolayout进行设置的答案

就你而言:

// remove the initial constraint
self.mainLogoCenterXConstraint.isActive = false
self.mainLogoCenterYConstraint.isActive = false

// create new constraint
let newMainLogoCenterXConstraint = NSLayoutConstraint(
            item: self.mainLogoImageView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: self.view,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0)
newMainLogoCenterXConstraint.identifier = "mainLogoCenterXConstraint"
newMainLogoCenterXConstraint.isActive = true

let newMainLogoCenterYConstraint = NSLayoutConstraint(
            item: self.mainLogoImageView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: self.view,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0)
newMainLogoCenterYConstraint.identifier = "mainLogoCenterYConstraint"
newMainLogoCenterYConstraint.isActive = true

self.view.setNeedsLayout()

UIView.animate(withDuration: 1.1, delay: 0.5, usingSpringWithDamping:  0.4, initialSpringVelocity: 0.0, animations: {

    self.view.layoutIfNeeded()

}, completion: nil)
问题就在这里

     // create new constraint
        let newMainLogoCenterXConstraint = NSLayoutConstraint(
            item: self.mainLogoImageView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: self.view,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0)
您提供了兼容类型的中心X和y,但两者都应为中心X

此外,这还不足以在视图中心显示图像,因为它是helperView的子视图,所以需要设置

 self.helperView.clipsToBounds = false // if it's true 
问题就在这里

     // create new constraint
        let newMainLogoCenterXConstraint = NSLayoutConstraint(
            item: self.mainLogoImageView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: self.view,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0)
您提供了兼容类型的中心X和y,但两者都应为中心X

此外,这还不足以在视图中心显示图像,因为它是helperView的子视图,所以需要设置

 self.helperView.clipsToBounds = false // if it's true 
不要仅为动画更改约束!使用:首先设置偏移,然后设置为
.identity
。不要仅为动画更改约束!使用设置动画:首先设置偏移,然后设置为
.identity