Enable/Disable user interaction when swipe speed is above a threshold

I am having Fortune wheel implemented in my application. Its a UIView subclass. When we swipe on the view, It will start rotating. Now I need to set a minimum swipe speed to start the wheel to rotate. First I need to check the user's swipe speed on the screen, if it is more than a minimum scroll speed, the wheel should start rotating. i.e, the swipe event should pass to the wheel view only if the swipe speed is greater than some pre-configured threshold value. Any way to accomplish this? Any help highly appreciated!

Answers


Try this code snippet :

 (void)rotateAccordingToAngle:(float)angle
{
   [spinWheel setTransform:CGAffineTransformRotate(spinWheel.transform, angle)];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{    
   [spinWheel.layer removeAllAnimations];
   previousTimestamp = event.timestamp;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];
  if (touch.view==spinWheel)
  {
    UITouch *touch = [touches anyObject];
    CGPoint center = CGPointMake(CGRectGetMidX([spinWheel bounds]), CGRectGetMidY([spinWheel bounds]));
    CGPoint currentTouchPoint = [touch locationInView:spinWheel];
    CGPoint previousTouchPoint = [touch previousLocationInView:spinWheel];
    CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);

    [self rotateAccordingToAngle:angleInRadians];

    CGFloat angleInDegree = RADIANS_TO_DEGREES(angleInRadians);
    revolutions+= (angleInDegree/360.0f);
  }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];
  if (touch.view==spinWheel) 
  {
    NSTimeInterval timeSincePrevious = event.timestamp - previousTimestamp;
    CGFloat revolutionsPerSecond = revolutions/timeSincePrevious;
    NSLog(@"%.3f",revolutionsPerSecond);
    [self startAnimationWithRevolutions:revolutionsPerSecond forTime:5.0f];
  }
  revolutions = 0;
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
  spinWheel.userInteractionEnabled = TRUE;
  if (timerUpdate) {
    [timerUpdate invalidate];
    timerUpdate = nil;
  }
}
-(void)updateTransform{
  spinWheel.transform = [[spinWheel.layer presentationLayer] affineTransform];
}
-(void)startAnimationWithRevolutions:(float)revPerSecond forTime:(float)time
{
  spinWheel.userInteractionEnabled = FALSE;
  float totalRevolutions = revPerSecond * time;

  timerUpdate = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateTransform) userInfo:nil repeats:YES];

  [CATransaction begin];
  [CATransaction setValue:[NSNumber numberWithFloat:time] forKey:kCATransactionAnimationDuration];

  CABasicAnimation* spinAnimation = [CABasicAnimation
                                   animationWithKeyPath:@"transform.rotation"];
  CGAffineTransform transform = spinWheel.transform;
  float fromAngle = atan2(transform.b, transform.a);
  float toAngle = fromAngle + (totalRevolutions*4*M_PI);
  spinAnimation.fromValue = [NSNumber numberWithFloat:fromAngle];
  spinAnimation.toValue = [NSNumber numberWithFloat:toAngle];
  spinAnimation.repeatCount = 0;
  spinAnimation.removedOnCompletion = NO;
  spinAnimation.delegate = self;
  spinAnimation.timingFunction = [CAMediaTimingFunction functionWithName:
                                kCAMediaTimingFunctionEaseOut];
  [spinWheel.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
  [CATransaction commit];
}

Hope it helps.


Need Your Help

DocumentFragment browser support

javascript cross-browser

Today I stumbled on createDocumentFragment. I was wondering if the DocumementFragment is supported, and how, on the different browsers, expecially IE series.

Comparison of Lat, Long Coordinates

sorting comparison

I have a list of more than 15 thousand latitude and longitude coordinates. Given any X,Y coordinates, what is the fastest way to find the closest coordinates on the list?