简体   繁体   English

UIView.animate视图框架问题

[英]UIView.animate problems with view frame

I'm struggling trying to make an animation. 我正在努力制作动画。 This is what's happening: video 这是正在发生的事情: 视频

The back card was supposed to do the same animation as in the beginning of the video, but it's doing a completely different thing. 背面卡本来应该像视频开头那样做相同的动画,但是它做的是完全不同的事情。 I'm checking the UIView.frame at the beginning of the animation and it's the same as the first time the card enters, but obviously something is wrong... Here is the code: 我正在动画开始时检查UIView.frame,它与卡第一次进入时相同,但是显然出了点问题……代码如下:

func cardIn() {
    let xPosition = (self.darkCardView?.frame.origin.x)! - 300
    let yPosition = (self.darkCardView?.frame.origin.y)!

    self.initialPos = self.darkCardView.frame

    let height = self.darkCardView?.frame.size.height
    let width = self.darkCardView?.frame.size.width

    self.darkCardView?.transform = (self.darkCardImageView?.transform.rotated(by: CGFloat(Double.pi/4)))!

    UIView.animate(withDuration: 1.1, animations: {
        self.darkCardView?.frame = CGRect(x: xPosition, y: yPosition, width: width!, height: height!)
        self.darkCardView?.transform = (self.darkCardImageView?.transform.rotated(by: CGFloat(0)))!
    })
}

func cardOut() {
    let xPosition = (self.darkCardView?.frame.origin.x)! - 600
    let yPosition = (self.darkCardView?.frame.origin.y)!

    let height = self.darkCardView?.frame.size.height
    let width = self.darkCardView?.frame.size.width

    self.darkCardView?.transform = (self.darkCardImageView?.transform.rotated(by: CGFloat(0)))!

    UIView.animate(withDuration: 1.0, delay: 0, options: .allowAnimatedContent, animations: {
        self.darkCardView?.frame = CGRect(x: xPosition, y: yPosition, width: width!, height: height!)
        self.darkCardView?.transform = (self.darkCardImageView?.transform.rotated(by: CGFloat(-Double.pi/4)))!
    }) { (true) in
        self.darkCardView?.transform = (self.darkCardImageView?.transform.rotated(by: CGFloat(0)))!
        self.darkCardView?.frame = self.initialPos

        self.cardIn()
    }
}

Does somebody know how can I repeat the same animation that's in the beginning of the video after cardOut function is called? 有人知道在调用cardOut函数后如何重复视频开头的相同动画吗?

Given that we do not know where/when you are calling the above two functions I can only take a guess at what is happening. 鉴于我们不知道您在何处/何时调用上述两个函数,我只能猜测正在发生什么。

So you have an animate in and an animate out, but what about a reset function? 因此,您有一个动画输入和一个动画输出,但是重置功能呢?

Timeline: Animate the card in. ... Eventually animate the card out ... Reset the cards location to off screen to the right (location before animating in for the first time) ... animate the card in ... Repeat, ect.... 时间轴:将卡片动画化。...最终将卡片动画化...将卡片位置重设为屏幕外向右(第一次动画之前的位置)...将卡片动画化...重复,等等...。

Note that if you set a view's transform to anything other than the identity transform then the view's frame rect becomes "undefined". 请注意,如果将视图的变换设置为除Identity变换以外的任何其他值,则视图的帧rect变为“未定义”。 You can neither set it to a new value nor read it's value reliably. 您既不能将其设置为新值,也不能可靠地读取其值。

You should change your code to use the view's center property if you're changing the transform. 如果要更改转换,则应更改代码以使用视图的center属性。

As Jerland points out in their post, you also probably need to reset your back card to it's starting position before beginning the next animation cycle. 正如Jerland在他们的帖子中指出的那样,您可能还需要在开始下一个动画周期之前将背卡重置到其起始位置。 (Set it to hidden, put it's center back to the starting position and set it's transform back to the identity transform. (将其设置为隐藏,将其中心放回到起始位置,然后将其转换回身份转换。

EDIT: 编辑:

This code: 这段代码:

UIView.animate(withDuration: 1.0, delay: 0, options: .allowAnimatedContent, animations: {
    self.darkCardView?.frame = CGRect(x: xPosition, y: yPosition, width: width!, height: height!)
    self.darkCardView?.transform = (self.darkCardImageView?.transform.rotated(by: CGFloat(-Double.pi/4)))!
}) { (true) in
    self.darkCardView?.transform = (self.darkCardImageView?.transform.rotated(by: CGFloat(0)))!
    self.darkCardView?.frame = self.initialPos

    self.cardIn()
}

Does not set the transform to identity. 不将转换设置为身份。 Try changing that line to 尝试将该行更改为

    self.darkCardView?.transform = .identity

I managed to solve the animation problem by calling viewDidLoad() instead of calling self.cardIn() in the end o cardOut completion block. 我设法通过在完成cardOut完成块中调用viewDidLoad()而不是调用self.cardIn()来解决动画问题。 But I don't want to depend on calling viewDidLoad every time a new card has to enter the screen... 但是我不想每次新卡都必须进入屏幕时才调用viewDidLoad ...

Also, I didn't set any properties of the back card in viewDidLoad . 另外,我没有在viewDidLoad中设置背面卡的任何属性。 I only have it in the .xib file. 我只在.xib文件中有它。 Any ideas? 有任何想法吗?

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

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