【问题标题】:Smooth expanding UIView animation in Swift在 Swift 中平滑扩展 UIView 动画
【发布时间】:2017-07-05 07:05:03
【问题描述】:

目标:

如何为我想要展开以填满整个屏幕的UIView 设置动画。 UIView 需要在动画时以平滑、均匀和平衡的方式展开。

我在屏幕上放置了一个红色方块,从小开始,然后扩展以适应屏幕。 (图 1。)

let subview = UIView()
subview.backgroundColor = .red
subview.frame = CGRect(x: 60, y: 200, width: 50, height: 50)
self.view.addSubview(subview)

问题:

在 Swift 2 和 Swift 3 中,使用 animateWithDuration,我该怎么做 动画红色方块UIView 以平衡且均匀的方式扩展 以四面八方的方式填满整个屏幕?

UIView.animateWithDuration(1.0, delay: 0, options: nil, animations: {

    ?????

}, completion: nil)

图片 1:

【问题讨论】:

    标签: ios swift animation uiview animatewithduration


    【解决方案1】:

    你可以试试这个,我将持续时间设置为 5 秒,但你明白了。

    let viewWidth = view.frame.width
    let viewHeight = view.frame.height
    UIView.animate(withDuration: 5) { 
        subview.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight)
    }
    

    【讨论】:

    • 这很简单。谢谢。
    【解决方案2】:

    看起来你需要玩规模。

    class ViewController: UIViewController {
    
    var subview:UIView!
    
    @IBOutlet weak var animateButton:UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        subview = UIView()
        subview.backgroundColor = .red
        subview.frame = CGRect(x: 60, y: 200, width: 50, height: 50)
        self.view.addSubview(subview)
    
        self.view.bringSubview(toFront: animateButton)
    }
    
    @IBAction func animateButtonPressed(sender:UIButton) {
        if(sender.tag == 0) {
    
            let screenCenter = CGPoint(x:UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY)
            let subviewCenter = self.view.convert(self.subview.center, to: self.view)
            let offset = UIOffset(horizontal: screenCenter.x-subviewCenter.x, vertical: screenCenter.y-subviewCenter.y)
    
            let widthScale = UIScreen.main.bounds.size.width/subview.frame.size.width
            let heightScale = UIScreen.main.bounds.size.height/subview.frame.size.height
            UIView.animate(withDuration: 1.0, animations: {
                let scaleTransform = CGAffineTransform(scaleX: widthScale, y: heightScale)
                let translateTransform = CGAffineTransform(translationX: offset.horizontal, y: offset.vertical)
                self.subview.transform = scaleTransform.concatenating(translateTransform)
            }, completion: { (finished) in
                sender.tag = 1;
            })
    
        } else {
            UIView.animate(withDuration: 1.0, animations: {
                self.subview.transform = CGAffineTransform.identity
            }, completion: { (finished) in
                sender.tag = 0;
            })
        }
    }
    }
    

    【讨论】:

    • 感谢您的建议。
    猜你喜欢
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多