Variable increases in value as though in a loop

61 views Asked by At

EDIT: Sorry, I found the issue, which was something happening in another class that wasn't available here. Thanks to all who considered the question. Original:

This may be a noob programmer element I'm overlooking, but I've got a variable increasing in value as though in a loop and I would not like it to increase.

I have a waveform that you can zoom in on, and I have a trimControl for editing that I would like to zoom in along with the waveform. So you zoom into the waveform and you thereby zoom or expand the trimControl as well.

So I set a pinch gesture recognizer on the waveform. The waveform is composed of samples. I take the starting zoomed in samples (which is zero at the very beginning) and I subtract them from the zoomed in samples that occurs during pinching. Then I divide those by the total samples of the waveform, multiply by 100, and voila, you have the percentage by which one side of the trimControl (in this case the left side) should move.

Here is my code

- (void)handleWaveformPinchGesture:(UIPinchGestureRecognizer *)recognizer {

if (recognizer.state ==UIGestureRecognizerStateBegan) {

   startingSamples = self.waveform.zoomStartSamples;
    originalLeft = trimControl.leftValue;
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
    float newSamples = self.waveform.zoomStartSamples;

    trimControl.leftValue = (startingSamples - newSamples)*100/self.waveform.totalSamples + originalLeft;

    NSLog(@"starting sampels are %f and newSamples are %f and originalLeft is %f", startingSamples, newSamples, originalLeft);

    NSLog(@"Left = %f, right = %f", trimControl.leftValue, trimControl.rightValue);

    [self.trimControl setNeedsLayout];

}
}

The problem is that every time I go to zoom in, the original left value (which is a float like startingSamples) grows on its own. Here is the NSLog, edited to show the point at which there is a new pinch recognized.

starting sampels are 0.000000 and newSamples are 0.000000 and originalLeft is 3.864734
Left = 4.908213, right = 127.000000
starting sampels are 0.000000 and newSamples are 0.000000 and originalLeft is 6.280193
Left = 7.975845, right = 127.000000
starting sampels are 0.000000 and newSamples are 0.000000 and originalLeft is 8.454106
Left = 10.736715, right = 127.000000

So you see that although the startingSamples and newSamples are both 0, the originalLeft grows on its own. What can I do to stop this?

Thanks.

1

There are 1 answers

0
Yan On

You are setting trimControl.leftValue when UIGestureRecognizerStateChanged and then not resetting when pinch gesture ends. When ever you start pinching originalLeft = trimControl.leftValue; will be set to the leftValue that was set from last time you pinched. That is why it is growing. I think you want to reset it to zero when pinch ends. Let me know if that works.