Circle UIView使用保持圆形的动画调整大小

时间:2017-08-05 20:20:22

标签: ios swift uiview

我有一个UIView,我正在使背景为圆形:

 self.colourView.layer.cornerRadius = 350
 self.colourView.clipsToBounds = true

视图为700乘700平方。

然后我尝试使用以下方法更改此视图的大小:

UIView.animate(withDuration: zoomLength,
                                   delay: 0,
                                   options: .curveEaseIn,
                                   animations: {
                                    self.colorViewWidth.constant = 100
                                    self.colorViewHeight.constant = 100

                                    self.colourView.layer.cornerRadius = 50
                                    self.colourView.clipsToBounds = true
                                    self.view.layoutIfNeeded()
                                    },
                                   completion: { finished in

                                        })

                                    })

这个问题是视图的角半径立即切换到50。因此,当视图缩小时,它看起来不像一个圆形收缩,而是一个圆角的正方形,直到它达到100×100的大小。

如何在动画期间保持圆圈形状的同时拍摄出圆形并收缩的视图。

3 个答案:

答案 0 :(得分:3)

您可以随时使用UIView动画旁边的CABasicAnimation为角半径设置动画。见下面的例子。

import UIKit

class ViewController: UIViewController {

    //cause 'merica
    var colorView = UIView()
    var button = UIButton()

    var colorViewWidth : NSLayoutConstraint!
    var colorViewHeight : NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        colorView.frame = CGRect(x: 0, y: 0, width: 750, height: 750)
        colorView.center = self.view.center
        colorView.backgroundColor = .blue
        colorView.layer.cornerRadius = 350
        colorView.clipsToBounds = true
        colorView.layer.allowsEdgeAntialiasing = true
        colorView.translatesAutoresizingMaskIntoConstraints = false

        //set up constraints

        colorViewWidth = NSLayoutConstraint(item: colorView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 750)
        colorViewWidth.isActive = true

        colorViewHeight = NSLayoutConstraint(item: colorView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 750)
        colorViewHeight.isActive = true

        self.view.addSubview(colorView)
        colorView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        colorView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true

        //button for action
        button = UIButton(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width - 20, height: 50))
        button.center = self.view.center
        button.center.y = self.view.bounds.height - 70
        button.addTarget(self, action: #selector(ViewController.pressed(sender:)), for: .touchUpInside)
        button.setTitle("Animate", for: .normal)
        button.setTitleColor(.blue, for: .normal)

        self.view.addSubview(button)

    }

    func pressed(sender:UIButton) {

        self.colorViewWidth.constant = 100
        self.colorViewHeight.constant = 100

        UIView.animate(withDuration: 1.0,
                       delay: 0,
                       options: .curveEaseIn,
                       animations: {
                        self.view.layoutIfNeeded()

        },

                       completion: { finished in

        })

        //animate cornerRadius and update model
        let animation = CABasicAnimation(keyPath: "cornerRadius")
        animation.duration = 1.0
        //super important must match
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
        animation.fromValue = colorView.layer.cornerRadius
        animation.toValue = 50
        colorView.layer.add(animation, forKey: nil)
        //update model important
        colorView.layer.cornerRadius = 50

    }
}

结果: Result

答案 1 :(得分:0)

尝试将您的图片放入viewDidLayoutSubviews()

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

 colourView.layer.cornerRadius  = colourView.frame.size.width/2
 colourView.layer.masksToBounds = true
 colourView.layer.borderWidth = 3.0
 colourView.layer.borderColor = UIColor(red: 142.0/255.5, green: 142.0/255.5, blue: 142.0/255.5, alpha: 1.0).cgColor

}

让我知道它是怎么回事

干杯

答案 2 :(得分:0)

关于动画,您需要创建function charChange(char){ for (var i=0; i<char.length; i++){ var char2=charCodeAt(char[i])+32; var char3=String.fromCharCode(char2); if (char3 !== charCodeAt(97-122){ alert("Please enter only letters in the function") } } return char; } 对象,然后设置图层对象的CAShapeLayer

cgpath

以上将为您绘制圆圈。之后,您可以在let circleLayer = CAShapeLayer() let radius = view.bounds.width * 0.5 //Value of startAngle and endAngle should be in the radian. let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true) circleLayer.path = path.cgPath 对象上应用动画。

layer
相关问题