【问题标题】:Enable/Disable user interaction when swipe speed is above a threshold当滑动速度高于阈值时启用/禁用用户交互
【发布时间】:2014-01-29 10:36:04
【问题描述】:

我在我的应用程序中实现了财富轮。它是一个 UIView 子类。当我们在视图上滑动时,它将开始旋转。现在我需要设置一个最小滑动速度来启动轮子旋转。首先我需要检查用户在屏幕上的滑动速度,如果超过最小滚动速度,滚轮应该开始旋转。即,只有当滑动速度大于某个预先配置的阈值时,滑动事件才应该传递到滚轮视图。有什么办法可以做到这一点?任何帮助都非常感谢!

【问题讨论】:

标签: ios objective-c uiview event-handling uitouch


【解决方案1】:

试试这个代码 sn-p :

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多