Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking use of velocityInView with UIPanGestureRecognizer
    text
    copied!<p>I have a custom slider-type object, that I wish to make more usable. Currently I use <code>UIPanGestureRecognizer</code> and <code>translationInView</code> to make it work. It works pretty well but I'd like some sort of velocity in there to make it feel a bit more useful. I've tried a few things but cant quite figure out how to properly implement velocity <code>changedLevel</code> equation.</p> <pre><code>- (void)panDetected:(UIPanGestureRecognizer *)gesture { CGPoint swipeLocation = [gesture locationInView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:swipeLocation]; LevelCounterTableCell *swipedCell = (LevelCounterTableCell *)[self.tableView cellForRowAtIndexPath:indexPath]; if([gesture state] == UIGestureRecognizerStateBegan) { NSString *originalLevelString = swipedCell.levelNumber.text; originalLevel = [originalLevelString intValue]; // int originalLevel } if ([gesture state] == UIGestureRecognizerStateChanged) { CGFloat xTranslation = [gesture translationInView:[[gesture view] superview]].x; CGFloat xVelocity = [gesture velocityInView: [[gesture view] superview]].x; // Pan threshold is currently set to 8.0. // 8.0 is a decent level for slow panning // for fast panning 2.0 is more reasonable changedLevel = ceilf((xTranslation / panThreshold) + originalLevel); // int changedLevel // Raw velocity seems to go from around 3 (slow) // to over 200 (fast) NSLog(@"raw velocity = %f", xVelocity); if (changedLevel &gt;= 15 &amp;&amp; changedLevel &lt;= 100) { swipedCell.levelNumber.text = [NSString stringWithFormat:@"%i", changedLevel]; swipedCell.meter.frame = [self updateMeter: changedLevel]; } } if ([gesture state] == UIGestureRecognizerStateEnded || [gesture state] == UIGestureRecognizerStateCancelled) { if (changedLevel &gt;= 15 &amp;&amp; changedLevel &lt;= 100) { //... Save the values... } } } </code></pre> <p>Any help would be greatly appreciated. Thank you.</p>
 

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