Why is there a difference between a progress of 0.2 and 0.20? I'm really missing something here.
I have a dynamic total count of data and each data has a page.
let totalDataCount = 5
self.progressView.progress = Float(1/self.totalDataCount) // quotient: 0.2```
While if I set 0.20, I have this:
self.progressView.progress = 0.20
The division 1/5 returns 0 as a result because 1 is type of Int and the result is also Int. Try to change the 1/self.totalDataCount to 1.0/self.totalDataCount and it will work
Related
In my app, I give the user the option to play a small frame of audio (from a larger audio file)in order to listen over and over to do a manual transcription. AKPlayer makes this trivial. Now, because the frame of audio is pretty small, it's pretty intense to hear this loop over and over (a little maddening in the classical sense of the word). I'd like to either fade it out/fade it back in with the loop OR just inject like 500 ms of silence before the loop starts again. I have no idea where to start, here is the current working code as is:
public func playLoop(start: Double, end: Double) {
self.chordLoopPlayer.isLooping = true
self.chordLoopPlayer.buffering = .always
self.chordLoopPlayer.preroll()
let millisecondsPerSample : Double = 1000 / 44100
let startingDuration : Double = (((start * millisecondsPerSample) / 1000) / 2)
let endingDuration : Double = (((end * millisecondsPerSample) / 1000) / 2)
print("StartinDuration:\(startingDuration) | EndingDuration:\(endingDuration)")
self.chordLoopPlayer.loop.start = startingDuration
self.chordLoopPlayer.loop.end = endingDuration
self.chordLoopPlayer.play(from: startingDuration, to: endingDuration)
Thanks so much <3
You just need to set .fade values for your fade-in/fade-out prior to calling the play() function. AudioKit will execute them each time going in and out of the loop. So assuming you'd like a 2-second fade-out, and a 2-second fade-in (adjust to your taste), your code would look like:
public func playLoop(start: Double, end: Double) {
self.chordLoopPlayer.isLooping = true
self.chordLoopPlayer.buffering = .always
self.chordLoopPlayer.preroll()
let millisecondsPerSample : Double = 1000 / 44100
let startingDuration : Double = (((start * millisecondsPerSample) / 1000) / 2)
let endingDuration : Double = (((end * millisecondsPerSample) / 1000) / 2)
print("StartinDuration:\(startingDuration) | EndingDuration:\(endingDuration)")
self.chordLoopPlayer.loop.start = startingDuration
self.chordLoopPlayer.loop.end = endingDuration
// add fade in/out values to fade in or fade out during playback; reset to 0 to disable.
self.chordLoopPlayer.fade.inTime = 2 // in seconds
self.chordLoopPlayer.fade.outTime = 2 // in seconds
self.chordLoopPlayer.play(from: startingDuration, to: endingDuration)
}
I find the AudioKit documentation a bit frustrating in this respect, as it's not super-easy to find these properties if you don't already know what you're looking for, or to understand how to use them if you haven't already come across sample code, so I hope this is a useful example for others who happen to search on this topic on SO. In any case, the list of sub-properties associated with AudioKit's .fade property is here: https://audiokit.io/docs/Classes/AKPlayer/Fade.html
I want to make Y-axis interval with a certain difference. Let's say the multiple of 100 or any fix value, currently it draws with their own interval.
I am sharing my formatting code here.
let leftAxis = chartView.leftAxis
leftAxis.valueFormatter = IntAxisValueFormatter()
leftAxis.granularityEnabled = true
leftAxis.granularity = 1
I am able to solve my challenge, I am adding my fix here. First I calculated the total range then divided them by interval diff and set the LabelCount.
let totalRange = maxY - minY
let interval = 100 //Let's say 100 in my case
leftAxis.setLabelCount(Int(totalRange/interval) + 1, force: true)
One way is you calculate scale value according to your data. And you can use following function to scale as you need. But exact with value difference I don't think it is possible.
self.lineChart.setScaleMinima(1.0, scaleY: 2.0)
In above value first parameter will zoom in horizontally while second will zoom vertically.
I've tried to make a slider to show a percentage out of 100, but the label doesn't show that value correctly.
A UISlider has a default min and max of 0 and 1. So the slider is sending all kinds of fractional values as you move the slider such as 0.153343 and .53453545, etc. But you convert that number to an Int. That leaves you with only 0 and 1.
Either multiply the sender.value * 100 or change the slider's max value to 100.
Just set the maximumValue of the slider:
slider.maximumValue = 100
This will have the slider go between 0 and 100.
However, if you would not like to do this, try something like this:
#IBAction func valueChanged(sender: UISlider) {
let rounded = round(100 * sender.value) / 100
let final = rounded * 100
sliderLabel.text = "\(final)"
}
Made for Objective C. If anybody need.
float founded = roundf(100*self.volumeViewSlider.value)/100;
float final = founded*100;
I have the following function that doesn't behave as I would have expected.
func dispatchTrouble(startValue:Float, endValue:Float, duration:Float)
{
//100 increment steps per second
let incrementSteps = duration * 100
for(var step = 0.0 as Float; step < incrementSteps; step++)
{
var delayInSeconds = step * (duration / incrementSteps)
let answer = Float(NSEC_PER_SEC) * delayInSeconds
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(answer))
println(step)
//Using GCD to perform this on the main thread.
dispatch_after(popTime, dispatch_get_main_queue()){
println(step)
let fraction = step / incrementSteps
let incrementedValue = startValue + (endValue - startValue) * fraction
println(incrementedValue)
}
}
}
I expected the println(incrementedValue) statement to display a value that incremented from startValue to endValue and to finish in the number of seconds passed in duration.
However the behavior I get is that the code in the dispatch_after closure only prints the final value, it never prints the increments.
The delay occurs as expected, but the values are all calculated as if the for-loop had already completed. The first println(step) does show step incrementing, but the second one only shows the final value.
I clearly have a misunderstanding of how this should work. I expected that the code in the closure would hold the values that existed at the time the dispatch_after method was called, but it acts like it uses whatever the value is at the time it actually executes instead.
How can I capture the values at each iteration of the for-loop and execute the code in the closure using that?
All the closures you send to GDC are pointing to the same step variable. That means that every time one of them executes, it has the value it had when the loop ended.
Try changing your code to this:
func dispatchTrouble(startValue:Float, endValue:Float, duration:Float)
{
//100 increment steps per second
let incrementSteps = duration * 100
for(var step = 0.0 as Float; step < incrementSteps; step++)
{
var delayInSeconds = step * (duration / incrementSteps)
let answer = Float(NSEC_PER_SEC) * delayInSeconds
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(answer))
println(step)
let stepCopy = step
//Using GCD to perform this on the main thread.
dispatch_after(popTime, dispatch_get_main_queue()){
println(stepCopy)
let fraction = stepCopy / incrementSteps
let incrementedValue = startValue + (endValue - startValue) * fraction
println(incrementedValue)
}
}
}
It'll work like that. The closure is doing a retain on step, as explained on the swift reference.
Unlike Objective-C blocks which capture values, Swift closures capture variables. That means where an Objective-C block would have captured 100 different values of the "step" variable, the Swift closure captures the variable itself and prints its value at the time the closure is called.
The best way to fix this is to add a capture list.
dispatch_after(popTime, dispatch_get_main_queue()){
[let stepcopy = step] () -> Void in
println(stepcopy)
let fraction = stepcopy / incrementSteps
let incrementedValue = startValue + (endValue - startValue) * fraction
println(incrementedValue)
}
So a closure starts with {, followed optionally by the capture list in [ brackets], followed optionally by (arguments) -> result in, followed by the code.
BTW by using Float instead of Double you reduce the precision to about 7 digits instead of 15, for no good reason whatsoever.
I have a UISlider with minimumValue 0 and maximumValue. I want to make an interval of 0.5 and display it in a label (it's working).
How can I set the interval of my slider? I tried solution to round the value... but I failed.
Thank you
float RoundValue(UISlider * slider) {
return roundf(slider.value * 2.0) * 0.5;
}
when i do something like that i take an approach like the following:
#define kSTEPFRACTION .5
SliderMin = 0
sliderMax = 20
and get the value withs something like that
value = slider.value*kSTEPFRACTION;
this will give you values between 0 and 10 in steps of 0.5
sebastian