亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

畫圓的動畫

畫圓的動畫

慕仙森 2020-02-04 16:15:02
我正在尋找一種動畫制作圓圖的方法。我已經能夠創建圓,但是將其全部繪制在一起。這是我的CircleView課:import UIKitclass CircleView: UIView {  override init(frame: CGRect) {    super.init(frame: frame)    self.backgroundColor = UIColor.clearColor()  }  required init(coder aDecoder: NSCoder) {    fatalError("init(coder:) has not been implemented")  }  override func drawRect(rect: CGRect) {    // Get the Graphics Context    var context = UIGraphicsGetCurrentContext();    // Set the circle outerline-width    CGContextSetLineWidth(context, 5.0);    // Set the circle outerline-colour    UIColor.redColor().set()    // Create Circle    CGContextAddArc(context, (frame.size.width)/2, frame.size.height/2, (frame.size.width - 10)/2, 0.0, CGFloat(M_PI * 2.0), 1)    // Draw    CGContextStrokePath(context);  }}這是我將其添加到視圖控制器中的視圖層次結構中的方法:func addCircleView() {    let diceRoll = CGFloat(Int(arc4random_uniform(7))*50)    var circleWidth = CGFloat(200)    var circleHeight = circleWidth    // Create a new CircleView    var circleView = CircleView(frame: CGRectMake(diceRoll, 0, circleWidth, circleHeight))    view.addSubview(circleView)}是否可以在1秒鐘內使圓的繪制動起來?
查看完整描述

2 回答

?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

Mikes答案已更新為Swift 3.0


var circleLayer: CAShapeLayer!


override init(frame: CGRect) {

    super.init(frame: frame)

    self.backgroundColor = UIColor.clear


    // Use UIBezierPath as an easy way to create the CGPath for the layer.

    // The path should be the entire circle.

    let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: (frame.size.width - 10)/2, startAngle: 0.0, endAngle: CGFloat(M_PI * 2.0), clockwise: true)


    // Setup the CAShapeLayer with the path, colors, and line width

    circleLayer = CAShapeLayer()

    circleLayer.path = circlePath.cgPath

    circleLayer.fillColor = UIColor.clear.cgColor

    circleLayer.strokeColor = UIColor.red.cgColor

    circleLayer.lineWidth = 5.0;


    // Don't draw the circle initially

    circleLayer.strokeEnd = 0.0


    // Add the circleLayer to the view's layer's sublayers

    layer.addSublayer(circleLayer)


required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

}


func animateCircle(duration: TimeInterval) {

    // We want to animate the strokeEnd property of the circleLayer

    let animation = CABasicAnimation(keyPath: "strokeEnd")


    // Set the animation duration appropriately

    animation.duration = duration


    // Animate from 0 (no circle) to 1 (full circle)

    animation.fromValue = 0

    animation.toValue = 1


    // Do a linear animation (i.e The speed of the animation stays the same)

    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)


    // Set the circleLayer's strokeEnd property to 1.0 now so that it's the

    // Right value when the animation ends

    circleLayer.strokeEnd = 1.0


    // Do the actual animation

    circleLayer.add(animation, forKey: "animateCircle")

}

調用函數:


func addCircleView() {

    let diceRoll = CGFloat(Int(arc4random_uniform(7))*50)

    var circleWidth = CGFloat(200)

    var circleHeight = circleWidth


    // Create a new CircleView

    let circleView = CircleView(frame: CGRect(x: diceRoll, y: 0, width: circleWidth, height: circleHeight))

    //let test = CircleView(frame: CGRect(x: diceRoll, y: 0, width: circleWidth, height: circleHeight))


    view.addSubview(circleView)


    // Animate the drawing of the circle over the course of 1 second

    circleView.animateCircle(duration: 1.0)

}


查看完整回答
反對 回復 2020-02-04
  • 2 回答
  • 0 關注
  • 659 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號