简体   繁体   English

使用圆角半径从上到下创建动画 UIView

[英]Create Animated UIView from top to bottom with corner radius

I developed an iOS app, and I have an animation in it, I would like to create an animation like this .我开发了一个 iOS 应用程序,其中有一个 animation,我想像这样创建一个animation

I tried:我试过了:

blueView.backgroundColor = .blue
let duration = 1
let radius = blueView.frame.height / 2

self.blueView.frame.size = CGSize(width:   50, height:   50)
blueView.layer.cornerRadius = radius
if #available(iOS 11, *) {
    UIView.animate(withDuration: TimeInterval(duration), animations: {
       self.blueView.frame.size = CGSize(width:   self.view.frame.width, height:   self.view.frame.height)    
    }, completion: { _ in
                        UIView.animate(withDuration: TimeInterval(duration), animations: {

                            self.blueView.layer.cornerRadius = 0
                        })
    })
}

But I didn't achieve what I want, any idea or help to get that?但我没有实现我想要的,有什么想法或帮助吗?

Looks like you're missing updating the blueView radius in your animation as a start.看起来您缺少更新 animation 中的 blueView 半径作为开始。 Below are some quick changes/additions to get close to the animation that you mentioned.以下是一些快速更改/添加,以接近您提到的 animation。 I added the view programmatically, and made minimal changes to your code.我以编程方式添加了视图,并对您的代码进行了最小的更改。

let blueView: UIView = {
    // set starting position of view here
    let view = UIView(frame: CGRect(x: UIScreen.main.bounds.size.width , y: 0, width: 50, height: 50))
    view.backgroundColor = .blue
    view.layer.cornerRadius = view.frame.height / 2
    return view
}()

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.addSubview(blueView)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let duration: TimeInterval = 1

    if #available(iOS 11, *) {
        UIView.animate(withDuration: duration, animations: {
            // set blue view size to double the screen so it can cover full screen with corner radius
            self.blueView.frame.size = CGSize(width: self.view.frame.height * 2, height:   self.view.frame.height * 2)
            // update corder radius to equal new size
            self.blueView.layer.cornerRadius = self.view.frame.height
            // Set ending position of view here to offset new size and radius
            self.blueView.center = CGPoint(x: UIScreen.main.bounds.width, y: 0)
        }, completion: { _ in
            UIView.animate(withDuration: duration, animations: {
                self.blueView.frame.size = CGSize(width: self.view.frame.width, height:   self.view.frame.height)
                self.blueView.layer.cornerRadius = 0
                self.blueView.center = self.view.center
            })
        })
    }
}

I'm sure this can be done a different way, but I wanted to keep code close to what you started with in hopes that it helps you understand what I changed.我确信这可以通过不同的方式完成,但我想让代码与您开始时的代码接近,希望它可以帮助您了解我所做的更改。

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

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