繁体   English   中英

使图像从屏幕左右移动

[英]Making Images move left to right out of the screen

我试图使我的顶部和底部图像移出屏幕,以及当它们移出屏幕时; 我的图像徽标出现。 这很正常,但是当我限制我的图像时,他们做的却相反。

因此,它们不是移出屏幕,而是从屏幕外部进入屏幕。

这是我的代码,在此先感谢。

func dismissImages(){
        imageLogo.isHidden = true

        UIView.animate(withDuration: 17) {
            //self.topImage.frame.origin.x
            self.topImage.center.x -= 400

        }


        UIView.animate(withDuration: 17, animations: {
            self.bottomImage.center.x += 400
        }) { (sucess) in
            if sucess {
                self.imageLogo.isHidden = false
            }
        }

我建议设置动画层而不是视图本身

func dismissImages(){
    imageLogo.isHidden = true

    UIView.animate(withDuration: 17) {
        self.topImage.layer.frame.origin.x -= self.view.frame.width
    }

    UIView.animate(withDuration: 17, animations: {
        self.bottomImage.layer.frame.origin.x += self.view.frame.width
    }) { guard $0 else { return }
        self.imageLogo.isHidden = false
    }
}

为每个图像设置一个NSLayoutConstraint并为该约束设置动画。

// SETUP INITIAL POSITION
let topImageHorizontalConstraint = NSLayoutConstraint(item: topImage, attribute: NSLayoutAttribute.centerX, relatedBy: .equal, toItem: parentView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0) // Note: Change the constant from 0 to whatever you need it to be initially.

// ACTIVATE CONSTRAINT
topImageHorizontalConstraint.isActive = true

// ANIMATE THE CONSTANT ON THE CONSTRAINT
// Note: This part will be in dismissImages().
UIView.animate(withDuration: 17, animations: {
    topImageHorizontalConstraint.constant -= 400                
},
completion: { (animationFinished: Bool) in
    if animationFinished {
            // Show logo or whatever
    }
})

从iOS 9+开始,您可以使用NSLayoutAnchor创建约束并稍微简化一下事情。

// SETUP INITIAL POSITION
let topImageHorizontalConstraint = topImage.centerXAnchor.constraint(equalTo: parentView.centerXAnchor, constant: 0)

// ACTIVATE CONSTRAINT
topImageHorizontalConstraint.isActive = true

// ANIMATE THE CONSTANT ON THE CONSTRAINT
// Note: This part will be in dismissImages().
UIView.animate(withDuration: 17, animations: {
    topImageHorizontalConstraint.constant -= 400                
},
completion: { (animationFinished: Bool) in
    if animationFinished {
            // Show logo or whatever
    }
})

我喜欢锚点,因为看起来关系很容易,但是在这种情况下却非常相似。

注意:您需要在dismissImages()之外具有用于约束的变量。 然后在init()或viewDidLoad()或最初的某个地方创建约束,然后调用dismissImages()对更改进行动画处理。

我想到了。 在调用约束函数之前,我先调用了动画函数。 当我在ViewDidAppear中添加mu动画功能时,我的工作非常完美。 这样,仅当视图出现并且设置了约束时,动画才会发生。

谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM