简体   繁体   English

使图像从屏幕左右移动

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

I am trying to make my top and bottom image to move out of the screen, and when they are out of the screen; 我试图使我的顶部和底部图像移出屏幕,以及当它们移出屏幕时; my image logo appears. 我的图像徽标出现。 This was working perfectly but when I constrained my images they did the opposite. 这很正常,但是当我限制我的图像时,他们做的却相反。

So Instead of moving out of the screen, they come from outside the screen and move into the screen. 因此,它们不是移出屏幕,而是从屏幕外部进入屏幕。

This is my code, thanks in advance. 这是我的代码,在此先感谢。

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

I would suggest animating the layer instead of the view itself 我建议设置动画层而不是视图本身

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

Setup an NSLayoutConstraint for each image and animate the constant for that constraint. 为每个图像设置一个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
    }
})

As of iOS 9+ you can use NSLayoutAnchor to create the constraints and simplify things a bit. 从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
    }
})

I like anchors because it seems easier to visualize the relationship but it's pretty similar in this case. 我喜欢锚点,因为看起来关系很容易,但是在这种情况下却非常相似。

Note: You'll need to have variables for your constraints outside of dismissImages(). 注意:您需要在dismissImages()之外具有用于约束的变量。 Then create the constraints in init() or viewDidLoad() or somewhere initially, then call dismissImages() to animate the change. 然后在init()或viewDidLoad()或最初的某个地方创建约束,然后调用dismissImages()对更改进行动画处理。

I Figured it out. 我想到了。 I called my animation function before I called my constraints functions. 在调用约束函数之前,我先调用了动画函数。 I worked perfectly when I put mu animation function in ViewDidAppear. 当我在ViewDidAppear中添加mu动画功能时,我的工作非常完美。 That way the animations will only occur when the view appears and constraints are set. 这样,仅当视图出现并且设置了约束时,动画才会发生。

thanks. 谢谢。

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

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