Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

How get current ring value? #14

Closed
SeRG1k17 opened this issue Feb 14, 2017 · 12 comments
Closed

How get current ring value? #14

SeRG1k17 opened this issue Feb 14, 2017 · 12 comments

Comments

@SeRG1k17
Copy link

My view contains a button, after buttonTap I want to speed up the ring starting now like as

ring.setProgress(value: ring.currentValue, animationDuration: 5 , completion: nil)

@luispadron
Copy link
Owner

Confused about what you mean by this, could you elaborate a bit more?

Why would you want to set the progress of the ring to its current value, that wouldn't animate or cause any changes.

To get the current value you just need to do ring.value

@SeRG1k17
Copy link
Author

SeRG1k17 commented Feb 14, 2017

after click, ring.value return 0.0 all time
2017-02-14 23 28 17
2017-02-14 23 29 07

@SeRG1k17
Copy link
Author

SeRG1k17 commented Feb 14, 2017

I found a solution, but in my opinion not very nice

var startTime: TimeInterval!
var ringDuration: TimeInterval = 60

//...

func setRingProgress(withDuration duration: TimeInterval, andValue value: CGFloat) {
        startTime = Date.timeIntervalSinceReferenceDate
        
        ring.value = value
        ring.setProgress(value: 0, animationDuration: duration, completion: nil)
    }

//...
//button Action method

let elapsedTime = Date.timeIntervalSinceReferenceDate - startTime
let newValue = ringDuration - elapsedTime
let newDuration = (newValue + ringDuration) / ringDuration
setRingProgress(withDuration: TimeInterval(newDuration), andValue: CGFloat(newValue))

@luispadron
Copy link
Owner

luispadron commented Feb 14, 2017

The reason you're getting the wrong value is because you are not using the completion handler...

Animations take time, unless you use a duration of 0 which will update it instantly. In order to find value AFTER animation you must get the ring value in the completion handler.

Here is an example:

    override func viewDidLoad() {
        super.viewDidLoad()
        print("Value before: \(ring.value)") // OUTPUT: "Value before: 0.0"
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        ring.setProgress(value: 9, animationDuration: 5) {
            print("Value after: \(self.ring.value)") // OUTPUT: "Value after: 9.0"
        }
    }

There is no need to use time intervals or logic for that. Just set a completion handler, this will get called when the ring is done animating.

Please look at the documentation if you have some more questions or let me know if this helps.

@SeRG1k17
Copy link
Author

Your example does not include pressing the button.
Please see this, sorry for the bad quality.
https://vimeo.com/204075530

@luispadron
Copy link
Owner

I'm not going to write a full example as I don't have the time currently. But this should work as intended. If you're using an IBAction then you would call the set progress in there.

If you would like more help, please either post your project on here or send me an email with the project if you prefer to keep it private and I can take a look at it.

@SeRG1k17
Copy link
Author

SeRG1k17 commented Feb 14, 2017

testProject

@luispadron
Copy link
Owner

I see what you're saying now. You would like to get the value and have it be an updating value as the progress ring is animating.

This, however, is not a feature that was originally expected to be needed, so was never added and is not currently something I have the time to add. When I get a chance to look at it again I may be able to add this. However, you could also fork the repo and send a pull request and I'll gladly accept any changes after review!

luispadron pushed a commit that referenced this issue Mar 2, 2017
- Add a property for accessing the current value of the progress ring while animating, closes [issue #14](#14)
- Add fix for removing a currently running animation when calling `setProgress(:)` while ring is animating, closes [issue #19](#19)
- Fixed access levels for variables and functions, changed from `public` to `open` to allow subclassing.
- Updated `docs` by running Jazzy
@luispadron
Copy link
Owner

Got around to fixing this in version 1.3.0.

You can now access the current value by using currentValue. This will give you the exact value when called, even if ring is animating.

Thanks for posting your issue!

Read the upated docs here

@SeRG1k17
Copy link
Author

SeRG1k17 commented Mar 4, 2017

Nice work dude!

@NickAwesome
Copy link

screen shot 2017-05-20 at 6 04 06 pm
hi , currentvalue doesnt seems like working

@luispadron
Copy link
Owner

@NickAwesome, please read the error message Xcode is giving you. currentValue is a get only property, thus cannot be set.

If you want to set the value then call

.setProgress(value:duration)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants