值的变量增加就好像在循环中一样

时间:2014-11-19 01:15:03

标签: ios objective-c uipinchgesturerecognizer

编辑:对不起,我发现了这个问题,这是另一个在这里没有的课程中发生的事情。感谢所有考虑过这个问题的人。 原文:

这可能是我忽视的noob程序员元素,但我有一个变量值增加,好像在循环中,我不希望它增加。

我有一个可以放大的波形,我有一个trimControl用于编辑,我想放大波形。因此,您可以放大波形,从而缩放或扩展trimControl。

所以我在波形上设置了捏手势识别器。波形由样本组成。我采用起始放大的样本(一开始就为零),然后从缩小期间发生的放大样本中减去它们。然后我将它们除以波形的总样本,乘以100,瞧,你有trimControl一侧(在这种情况下是左侧)应该移动的百分比。

这是我的代码

- (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];

}
}

问题在于,每次我进行放大时,原始的左侧值(就像startsSamples一样浮动)会自行增长。这是NSLog,编辑后显示识别出新夹点的点。

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

所以你看到虽然startingSamples和newSamples都是0,但originalLeft自己增长。我该怎么做才能阻止这个?

感谢。

1 个答案:

答案 0 :(得分:0)

您在trimControl.leftValue时设置UIGestureRecognizerStateChanged,然后在捏合手势结束时不重置。如果您开始捏合,originalLeft = trimControl.leftValue;将被设置为您上次收缩时设置的leftValue。这就是它成长的原因。我认为你想在结束时将它重置为零。如果有效,请告诉我。