0 Replies - 583 Views - Last Post: 30 November 2016 - 06:29 PM

#1 hyesuchung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-November 16

Can't convert UIView to String when calling it

Posted 30 November 2016 - 06:29 PM

Hi, so I'm making a little timer (code shown below) and I'm trying to calling it at the very end:

 self.countDownTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: Selector(ClockView()), userInfo: nil, repeats: true) 


Swift 3.0 gives me an error saying that it can't convert the 'ClockView' to a String. How do I do that? Thank in advance.

Here's the code for context:
import UIKit

class ClockView: UIView {
    
    private var shapeLayer = CAShapeLayer()
    private var countDownTimer = Timer()
    private var timerValue = 900
    private var label = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createLabel()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    private func addCircle() {
        let circlePath = UIBezierPath(arcCenter: CGPoint(x: 160,y: 240), radius: CGFloat(100), startAngle: CGFloat(-M_PI_2), endAngle:CGFloat(2*M_PI-M_PI_2), clockwise: true)
        
        self.shapeLayer.path = circlePath.cgPath
        self.shapeLayer.fillColor = UIColor.clear.cgColor
        self.shapeLayer.strokeColor = UIColor.red.cgColor
        self.shapeLayer.lineWidth = 1.0
        
        self.layer.addSublayer(self.shapeLayer)
    }
    
    private func createLabel() {
        self.label = UILabel(frame: CGRect(x: 72, y: 220, width: 200, height: 40))
        
        self.label.font = UIFont(name: self.label.font.fontName, size: 40)
        self.label.textColor = UIColor.red
        
        self.addSubview(self.label)
    }
    
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "strokeEnd")
        animation.fromValue = 0
        animation.toValue = 1
        animation.duration = Double(self.timerValue)
        animation.fillMode = kCAFillModeForwards
        animation.isRemovedOnCompletion = false
        
        self.shapeLayer.add(animation, forKey: "ani")
    }
    
    private func updateLabel(value: Int) {
        self.setLabelText(value: self.timeFormatted(totalSeconds: value))
        self.addCircle()
    }
    
    private func setLabelText(value: String) {
        self.label.text = value
    }
    
    private func timeFormatted(totalSeconds: Int) -> String {
        let seconds: Int = totalSeconds % 60
        let minutes: Int = (totalSeconds / 60) % 60
        let hours: Int = totalSeconds / 3600
        return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
    }
    
    // Needs @objc to be able to call private function in NSTimer.
    @objc private func countdown(dt: Timer) {
        self.timerValue = self.timerValue - 1
        if self.timerValue < 0 {
            self.countDownTimer.invalidate()
        }
        else {
            self.setLabelText(value: self.timeFormatted(totalSeconds: self.timerValue))
        }
    }
    
    func setTimer(value: Int) {
        self.timerValue = value
        self.updateLabel(value: value)
    }
    
    func startClockTimer() {
        //self.countDownTimer.invalidate()
        self.countDownTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: Selector(ClockView()), userInfo: nil, repeats: true)
        self.startAnimation()
        
    }
    
    
}




Is This A Good Question/Topic? 0
  • +

Page 1 of 1