Change TimeInterval and update the CAbasicAnimation












0















I want make a timer. I've problem with CABasicAnimation and TimeInterval

they are not accurate. if I add 30 second to TimeInterval. Animation dosen't change. See on screenshoot.



My questions:



1.Adding +30 second to TimeInterval but CABasicAnimation don`t follow the added value



2.When circle complete one turn , for the second turn set new time Value example to 120 second.



https://imgur.com/Ps0sTiK



import UIKit


let timeLeftShapeLayer = CAShapeLayer()
let bgShapeLayer = CAShapeLayer()
var timeLeft: TimeInterval = 10

var endTime: Date?
var timeLabel = UILabel()
var timer = Timer()


let strokeIt = CABasicAnimation(keyPath: "strokeEnd")


class ViewController: UIViewController {

func drawBgShape() {
bgShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: view.frame.midX , y: view.frame.midY), radius:
100, startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
bgShapeLayer.strokeColor = UIColor.white.cgColor
bgShapeLayer.fillColor = UIColor.clear.cgColor
bgShapeLayer.lineWidth = 15
view.layer.addSublayer(bgShapeLayer)
}
func drawTimeLeftShape() {
timeLeftShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: view.frame.midX , y: view.frame.midY), radius:
100, startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
timeLeftShapeLayer.strokeColor = UIColor.green.cgColor
timeLeftShapeLayer.fillColor = UIColor.clear.cgColor
timeLeftShapeLayer.lineWidth = 15
timeLeftShapeLayer.lineCap = .round
view.layer.addSublayer(timeLeftShapeLayer)
}

func addTimeLabel() {
timeLabel = UILabel(frame: CGRect(x: view.frame.midX-50 ,y: view.frame.midY-25, width: 100, height: 50))
timeLabel.textAlignment = .center
timeLabel.text = timeLeft.time
view.addSubview(timeLabel)
}


override func viewDidLoad() {


super.viewDidLoad()
view.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
strokeIt.toValue = 1
strokeIt.duration = timeLeft
// add the animation to your timeLeftShapeLayer
timeLeftShapeLayer.add(strokeIt, forKey: nil)
// define the future end time by adding the timeLeft to now Date()
endTime = Date().addingTimeInterval(timeLeft)
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}


@objc func updateTime() {
if timeLeft > 0 {
timeLeft = endTime?.timeIntervalSinceNow ?? 0
timeLabel.text = timeLeft.time
} else {
timeLabel.text = "00:00"

timer.invalidate()
}
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

endTime = Date().addingTimeInterval(0.5 * 60)
}


}










share|improve this question























  • “Adding +30 second to TimeInterval but CABasicAnimation don`t follow the added value” Makes no sense. Where in your code do you add 30 seconds to anything?

    – matt
    Jan 1 at 19:17











  • On touchesBegan, endTime = Date().addingTimeInterval(0.5 * 60)

    – Emin Mustafa Dağcı
    Jan 1 at 19:29
















0















I want make a timer. I've problem with CABasicAnimation and TimeInterval

they are not accurate. if I add 30 second to TimeInterval. Animation dosen't change. See on screenshoot.



My questions:



1.Adding +30 second to TimeInterval but CABasicAnimation don`t follow the added value



2.When circle complete one turn , for the second turn set new time Value example to 120 second.



https://imgur.com/Ps0sTiK



import UIKit


let timeLeftShapeLayer = CAShapeLayer()
let bgShapeLayer = CAShapeLayer()
var timeLeft: TimeInterval = 10

var endTime: Date?
var timeLabel = UILabel()
var timer = Timer()


let strokeIt = CABasicAnimation(keyPath: "strokeEnd")


class ViewController: UIViewController {

func drawBgShape() {
bgShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: view.frame.midX , y: view.frame.midY), radius:
100, startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
bgShapeLayer.strokeColor = UIColor.white.cgColor
bgShapeLayer.fillColor = UIColor.clear.cgColor
bgShapeLayer.lineWidth = 15
view.layer.addSublayer(bgShapeLayer)
}
func drawTimeLeftShape() {
timeLeftShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: view.frame.midX , y: view.frame.midY), radius:
100, startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
timeLeftShapeLayer.strokeColor = UIColor.green.cgColor
timeLeftShapeLayer.fillColor = UIColor.clear.cgColor
timeLeftShapeLayer.lineWidth = 15
timeLeftShapeLayer.lineCap = .round
view.layer.addSublayer(timeLeftShapeLayer)
}

func addTimeLabel() {
timeLabel = UILabel(frame: CGRect(x: view.frame.midX-50 ,y: view.frame.midY-25, width: 100, height: 50))
timeLabel.textAlignment = .center
timeLabel.text = timeLeft.time
view.addSubview(timeLabel)
}


override func viewDidLoad() {


super.viewDidLoad()
view.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
strokeIt.toValue = 1
strokeIt.duration = timeLeft
// add the animation to your timeLeftShapeLayer
timeLeftShapeLayer.add(strokeIt, forKey: nil)
// define the future end time by adding the timeLeft to now Date()
endTime = Date().addingTimeInterval(timeLeft)
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}


@objc func updateTime() {
if timeLeft > 0 {
timeLeft = endTime?.timeIntervalSinceNow ?? 0
timeLabel.text = timeLeft.time
} else {
timeLabel.text = "00:00"

timer.invalidate()
}
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

endTime = Date().addingTimeInterval(0.5 * 60)
}


}










share|improve this question























  • “Adding +30 second to TimeInterval but CABasicAnimation don`t follow the added value” Makes no sense. Where in your code do you add 30 seconds to anything?

    – matt
    Jan 1 at 19:17











  • On touchesBegan, endTime = Date().addingTimeInterval(0.5 * 60)

    – Emin Mustafa Dağcı
    Jan 1 at 19:29














0












0








0








I want make a timer. I've problem with CABasicAnimation and TimeInterval

they are not accurate. if I add 30 second to TimeInterval. Animation dosen't change. See on screenshoot.



My questions:



1.Adding +30 second to TimeInterval but CABasicAnimation don`t follow the added value



2.When circle complete one turn , for the second turn set new time Value example to 120 second.



https://imgur.com/Ps0sTiK



import UIKit


let timeLeftShapeLayer = CAShapeLayer()
let bgShapeLayer = CAShapeLayer()
var timeLeft: TimeInterval = 10

var endTime: Date?
var timeLabel = UILabel()
var timer = Timer()


let strokeIt = CABasicAnimation(keyPath: "strokeEnd")


class ViewController: UIViewController {

func drawBgShape() {
bgShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: view.frame.midX , y: view.frame.midY), radius:
100, startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
bgShapeLayer.strokeColor = UIColor.white.cgColor
bgShapeLayer.fillColor = UIColor.clear.cgColor
bgShapeLayer.lineWidth = 15
view.layer.addSublayer(bgShapeLayer)
}
func drawTimeLeftShape() {
timeLeftShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: view.frame.midX , y: view.frame.midY), radius:
100, startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
timeLeftShapeLayer.strokeColor = UIColor.green.cgColor
timeLeftShapeLayer.fillColor = UIColor.clear.cgColor
timeLeftShapeLayer.lineWidth = 15
timeLeftShapeLayer.lineCap = .round
view.layer.addSublayer(timeLeftShapeLayer)
}

func addTimeLabel() {
timeLabel = UILabel(frame: CGRect(x: view.frame.midX-50 ,y: view.frame.midY-25, width: 100, height: 50))
timeLabel.textAlignment = .center
timeLabel.text = timeLeft.time
view.addSubview(timeLabel)
}


override func viewDidLoad() {


super.viewDidLoad()
view.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
strokeIt.toValue = 1
strokeIt.duration = timeLeft
// add the animation to your timeLeftShapeLayer
timeLeftShapeLayer.add(strokeIt, forKey: nil)
// define the future end time by adding the timeLeft to now Date()
endTime = Date().addingTimeInterval(timeLeft)
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}


@objc func updateTime() {
if timeLeft > 0 {
timeLeft = endTime?.timeIntervalSinceNow ?? 0
timeLabel.text = timeLeft.time
} else {
timeLabel.text = "00:00"

timer.invalidate()
}
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

endTime = Date().addingTimeInterval(0.5 * 60)
}


}










share|improve this question














I want make a timer. I've problem with CABasicAnimation and TimeInterval

they are not accurate. if I add 30 second to TimeInterval. Animation dosen't change. See on screenshoot.



My questions:



1.Adding +30 second to TimeInterval but CABasicAnimation don`t follow the added value



2.When circle complete one turn , for the second turn set new time Value example to 120 second.



https://imgur.com/Ps0sTiK



import UIKit


let timeLeftShapeLayer = CAShapeLayer()
let bgShapeLayer = CAShapeLayer()
var timeLeft: TimeInterval = 10

var endTime: Date?
var timeLabel = UILabel()
var timer = Timer()


let strokeIt = CABasicAnimation(keyPath: "strokeEnd")


class ViewController: UIViewController {

func drawBgShape() {
bgShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: view.frame.midX , y: view.frame.midY), radius:
100, startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
bgShapeLayer.strokeColor = UIColor.white.cgColor
bgShapeLayer.fillColor = UIColor.clear.cgColor
bgShapeLayer.lineWidth = 15
view.layer.addSublayer(bgShapeLayer)
}
func drawTimeLeftShape() {
timeLeftShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: view.frame.midX , y: view.frame.midY), radius:
100, startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
timeLeftShapeLayer.strokeColor = UIColor.green.cgColor
timeLeftShapeLayer.fillColor = UIColor.clear.cgColor
timeLeftShapeLayer.lineWidth = 15
timeLeftShapeLayer.lineCap = .round
view.layer.addSublayer(timeLeftShapeLayer)
}

func addTimeLabel() {
timeLabel = UILabel(frame: CGRect(x: view.frame.midX-50 ,y: view.frame.midY-25, width: 100, height: 50))
timeLabel.textAlignment = .center
timeLabel.text = timeLeft.time
view.addSubview(timeLabel)
}


override func viewDidLoad() {


super.viewDidLoad()
view.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
strokeIt.toValue = 1
strokeIt.duration = timeLeft
// add the animation to your timeLeftShapeLayer
timeLeftShapeLayer.add(strokeIt, forKey: nil)
// define the future end time by adding the timeLeft to now Date()
endTime = Date().addingTimeInterval(timeLeft)
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}


@objc func updateTime() {
if timeLeft > 0 {
timeLeft = endTime?.timeIntervalSinceNow ?? 0
timeLabel.text = timeLeft.time
} else {
timeLabel.text = "00:00"

timer.invalidate()
}
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

endTime = Date().addingTimeInterval(0.5 * 60)
}


}







swift cabasicanimation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 19:03









Emin Mustafa DağcıEmin Mustafa Dağcı

12




12













  • “Adding +30 second to TimeInterval but CABasicAnimation don`t follow the added value” Makes no sense. Where in your code do you add 30 seconds to anything?

    – matt
    Jan 1 at 19:17











  • On touchesBegan, endTime = Date().addingTimeInterval(0.5 * 60)

    – Emin Mustafa Dağcı
    Jan 1 at 19:29



















  • “Adding +30 second to TimeInterval but CABasicAnimation don`t follow the added value” Makes no sense. Where in your code do you add 30 seconds to anything?

    – matt
    Jan 1 at 19:17











  • On touchesBegan, endTime = Date().addingTimeInterval(0.5 * 60)

    – Emin Mustafa Dağcı
    Jan 1 at 19:29

















“Adding +30 second to TimeInterval but CABasicAnimation don`t follow the added value” Makes no sense. Where in your code do you add 30 seconds to anything?

– matt
Jan 1 at 19:17





“Adding +30 second to TimeInterval but CABasicAnimation don`t follow the added value” Makes no sense. Where in your code do you add 30 seconds to anything?

– matt
Jan 1 at 19:17













On touchesBegan, endTime = Date().addingTimeInterval(0.5 * 60)

– Emin Mustafa Dağcı
Jan 1 at 19:29





On touchesBegan, endTime = Date().addingTimeInterval(0.5 * 60)

– Emin Mustafa Dağcı
Jan 1 at 19:29












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998158%2fchange-timeinterval-and-update-the-cabasicanimation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998158%2fchange-timeinterval-and-update-the-cabasicanimation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith