当我执行segue到ViewController圈已经填充

时间:2016-03-27 18:57:35

标签: ios swift

我有两个ViewController。

  1. 带一个按钮的基本视图控制器,此按钮执行segue(模态化)到TimerViewController
  2. TimerViewController - 此视图控制器为圆圈中的倒计时设置动画。
  3. 如果我将TimerViewController设置为初始视图控制器,一切都像魅力,计时器倒计时和圆圈填充红色作为时间倒计时。请参阅屏幕截图:http://i.imgur.com/TiBdUUS.png

    但是如果我从第一个View Controller执行segue到TimerViewController圈已经填充了红色。请参阅屏幕截图:http://i.imgur.com/8D0IWiJ.png

    我无法理解这段代码有什么问题以及为什么圈子被填满了? 这是我的代码:

    class TimerViewController: UIViewController {
    
    // View 500x500
    @IBOutlet weak var plView: UIView!
    
    // define the time left
    let timeLeftShapeLayer = CAShapeLayer()
    let bgShapeLayer = CAShapeLayer()
    var timeLeft: NSTimeInterval = 60.0
    var endTime: NSDate!
    var timeLabel =  UILabel()
    var timer = NSTimer()
    
    // here you create your basic animation object to animate the strokeEnd
    let strokeIt = CABasicAnimation(keyPath: "strokeEnd")
    
    
    // at viewDidload set the endTime and add your CAShapeLayer to your view:
    override func viewDidLoad() {
        super.viewDidLoad()
        plView.backgroundColor = UIColor(white: 0.94, alpha: 1.0)
    
        drawBgShape()
        drawTimeLeftShape()
        addTimeLabel()
    
        // here you define the fromValue, toValue and duration of your animation
        strokeIt.fromValue = 0.0
        strokeIt.toValue = 1.0
        strokeIt.duration = 60.0
    
        // add the animation to your timeLeftShapeLayer
        timeLeftShapeLayer.addAnimation(strokeIt, forKey: nil)
    
        // define the future end time by adding the timeLeft to now NSDate()
        endTime = NSDate().dateByAddingTimeInterval(timeLeft)
        timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(TimerViewController.updateTime), userInfo: nil, repeats: true)
    }
    
    
    // define a method to create UIBezierPath
    // startAngle at -90˚ and endAngle 270
    func drawBgShape() {
        bgShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: plView.frame.midX , y: plView.frame.midY), radius:
            100, startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).CGPath
        bgShapeLayer.strokeColor = UIColor.whiteColor().CGColor
        bgShapeLayer.fillColor = UIColor.clearColor().CGColor
        bgShapeLayer.lineWidth = 15
        plView.layer.addSublayer(bgShapeLayer)
    }
    func drawTimeLeftShape() {
        timeLeftShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: plView.frame.midX , y: plView.frame.midY), radius:
            100, startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).CGPath
        timeLeftShapeLayer.strokeColor = UIColor.redColor().CGColor
        timeLeftShapeLayer.fillColor = UIColor.clearColor().CGColor
        timeLeftShapeLayer.lineWidth = 15
        plView.layer.addSublayer(timeLeftShapeLayer)
    }
    
    // add your Label
    func addTimeLabel() {
        timeLabel = UILabel(frame: CGRectMake(plView.frame.midX-50 , plView.frame.midY-25, 100, 50))
        timeLabel.textAlignment = .Center
        timeLabel.text = timeLeft.time
        plView.addSubview(timeLabel)
    }
    
    
    // when updating the time
    func updateTime() {
        if timeLeft > 0 {
            timeLeft = endTime.timeIntervalSinceNow
            timeLabel.text = timeLeft.time
        } else {
            timeLabel.text = "00:00"
            timer.invalidate()
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    
    }
    
    }
    
    // extension convert the degrees to radians and display time
    extension Double {
    var degreesToRadians : CGFloat {
        return CGFloat(self) * CGFloat(M_PI) / 180.0
    }
    }
    
    extension NSTimeInterval {
    var time:String {
        return String(format:"%02d:%02d", Int(self/60.0),        Int(ceil(self%60)) )
    }
    }
    

1 个答案:

答案 0 :(得分:0)

尝试将圈子初始化代码放入viewDidAppear方法而不是viewDidLoad