Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculus to convert Y coordinates for purpose of updating bpm in a metronome
    text
    copied!<p>I'm in the course of developing a metronome for iPad. I'm using <strong>CGAffineTransformRotate</strong> for the metronomeArm animation, <strong>NSTimer(I'm not interested in great precision)</strong> for sound and a <strong>UIPanGestureRecognizer</strong> for dragging the metronomeWeight on the metronomeArm.<img src="https://i.stack.imgur.com/tCY0o.png" alt="enter image description here"></p> <p>My problem is that I don't know how to update the bpm by dragging the weight using the pan. For now I have this : <code>metronomeWeight.center.y</code> is 240 and the default bpm for this position is 80.The weight goes from top 140 to a maximum of 450. I have implemented this method but it is not correct :</p> <pre><code>-(void)updateBPM { CGFloat weightYPosition = metronomeWeight.center.y; NSUInteger newBPM = (weightYPosition/3); self.bpm = newBPM; } </code></pre> <p><strong>and the selector for the pan is this :</strong></p> <pre><code>-(void)handlePan:(UIPanGestureRecognizer*)gesture { CGPoint translation = [gesture translationInView:metronomeArm]; CGPoint location = [gesture locationInView:metronomeArm]; NSLog(@"miscarea pe oy are valoare de: %f", location.y); CGPoint newCenter = CGPointMake(metronomeArm.frame.size.width/2, gesture.view.center.y + translation.y ); if (newCenter.y &gt;= 140 &amp;&amp; newCenter.y &lt;= 450) { gesture.view.center = newCenter; [gesture setTranslation:CGPointZero inView:metronomeArm]; [self updateBPMFromWeightLocation]; tempoLabel.text = [NSString stringWithFormat:@"%d", self.bpm]; NSLog(@"metronomeWeight position : %f ",metronomeWeight.center.y); } } </code></pre> <p>The sound and animation update but not as desired, meaning that the lower limit bpm should be 225 and the upper one should be 1. In my case they are 150 and 46 respectively.</p> <p>My calculations are not good, so it will be fantastic if you can help me solve this problem... I have looked at apple's metronome project for days and can't understand how they do this...</p> <p>Thanks</p> <p><strong>The new updateBPM method thanks to @zimmryan suggestion</strong></p> <pre><code>-(void)updateBPMFromWeightLocation { CGFloat weightYPosition = metronomeWeight.center.y; float lengthInM = ((weightYPosition - 140) * 0.00041333); float time = 2 * M_PI * sqrt(lengthInM / 9.8); NSUInteger newBPM = floor(60.0 / time); self.bpm = newBPM; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload