如何为uiview.himate()使用完成块?

发布于 2025-01-26 14:35:55 字数 1671 浏览 2 评论 0原文

我正在研究一个学习动画的项目,并使用完成uiview.animate(disturation:) func的完成块遇到麻烦。我的动画是一个从屏幕顶部掉落的鞋盒,降落在基座上,然后打开。盒子打开后,我希望一个uiimageview从框中出来,成长为屏幕的全尺寸,然后segue到下一页,但是在我的动画完成和uiimageview do not'''''''''''''''' T根本出现。

这是我的代码:

override func viewDidAppear(_ animated: Bool) {
            
    UIView.animate(withDuration: 1.5, delay: 0.0, options: .curveEaseInOut,
                   animations: {
                    //Opening the box
                    self.shoeBoxImage.shoeBox.animationImages = self.boxOpeningAnimation
                    self.shoeBoxImage.shoeBox.animationDuration = 1.5
                    self.shoeBoxImage.shoeBox.animationRepeatCount = 1
                    self.shoeBoxImage.shoeBox.contentMode = .scaleAspectFill
        
                    self.shoeBoxImage.shoeBox.startAnimating()
        
                    //set to the final image
                    self.shoeBoxImage.shoeBox.image = UIImage(named: "frame13")
    },completion: {_ in
        let nextPage = UIImageView()
        nextPage.frame = CGRect(origin: self.shoeBoxImage.center, size: CGSize(width: 0.0, height: 0.0))
        nextPage.image = UIImage(named: "FirstLoadBackgroundImg.jpeg")
        nextPage.autoresizesSubviews = true
        self.view.addSubview(nextPage)
        self.view.bringSubviewToFront(nextPage)
        UIView.animate(withDuration: 5.0, animations: {
            nextPage.transform = CGAffineTransform(scaleX: 428, y: 926)
        })
        
        self.performSegue(withIdentifier: "FinishedLoading", sender: self)
    })
}

这是我第一次使用动画并在编程中创建视图,因此如果有人可以解释如何使完成块等待动画完成。为了使UIImageView出现并进行动画,然后将其全屏幕介绍到下一页,这将是非常感谢的。

I'm working on a project to learn animations and am having trouble using the completion block for the UIView.animate(withDuration:) func. MY animation is a shoebox that falls from the top of the screen, lands on a pedestal, then opens. Once the box opens I want a UIImageView to come out of the box, grow to full size of the screen and then segue to the next page, but the completion handler that the code for segueing is called before my animation completes and the UIImageView doesn't appear at all.

Here's my code:

override func viewDidAppear(_ animated: Bool) {
            
    UIView.animate(withDuration: 1.5, delay: 0.0, options: .curveEaseInOut,
                   animations: {
                    //Opening the box
                    self.shoeBoxImage.shoeBox.animationImages = self.boxOpeningAnimation
                    self.shoeBoxImage.shoeBox.animationDuration = 1.5
                    self.shoeBoxImage.shoeBox.animationRepeatCount = 1
                    self.shoeBoxImage.shoeBox.contentMode = .scaleAspectFill
        
                    self.shoeBoxImage.shoeBox.startAnimating()
        
                    //set to the final image
                    self.shoeBoxImage.shoeBox.image = UIImage(named: "frame13")
    },completion: {_ in
        let nextPage = UIImageView()
        nextPage.frame = CGRect(origin: self.shoeBoxImage.center, size: CGSize(width: 0.0, height: 0.0))
        nextPage.image = UIImage(named: "FirstLoadBackgroundImg.jpeg")
        nextPage.autoresizesSubviews = true
        self.view.addSubview(nextPage)
        self.view.bringSubviewToFront(nextPage)
        UIView.animate(withDuration: 5.0, animations: {
            nextPage.transform = CGAffineTransform(scaleX: 428, y: 926)
        })
        
        self.performSegue(withIdentifier: "FinishedLoading", sender: self)
    })
}

This is my first time working with animations and programatically creating views so if someone could explain how to make the completion block wait for the animation to complete. In order to make the UIImageView appear and animate then once it's full screen, segue to the next page it would be very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2025-02-02 14:35:55

大小为0,0。将零变换零仍然为零。我建议您完全不要使用变换,而只是将最终的frame设置为您想要的。

例如,

let startFrame = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
let endFrame = view.bounds

let imageView = UIImageView(image: ...)
imageView.contentMode = .scaleAspectFill
view.addSubview(imageView)
imageView.frame = startFrame
UIView.animate(withDuration: 3, delay: 0, options: .curveEaseInOut) {
    imageView.frame = endFrame
} completion: { _ in
    // do something here
}

产生:

“在此处输入图像描述”


顺便说一下,perfermegue可能应该在完成内部 lose 内部animate> and code> and code> and code >致电。

The size is 0, 0. Transforming zero by any scale is still zero. I would advise you to not use transform at all, but rather just set the final frame to be what you want.

E.g.,

let startFrame = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
let endFrame = view.bounds

let imageView = UIImageView(image: ...)
imageView.contentMode = .scaleAspectFill
view.addSubview(imageView)
imageView.frame = startFrame
UIView.animate(withDuration: 3, delay: 0, options: .curveEaseInOut) {
    imageView.frame = endFrame
} completion: { _ in
    // do something here
}

That yields:

enter image description here


By the way, the performSegue probably should be inside a completion closure of the inner animate call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文