iOS - 使用滑动W /速度滑动的高级动画

时间:2011-02-01 14:15:11

标签: objective-c ipad ios

基本上我正试图将手指放在地球上并旋转它的类型功能。

所以我真正需要做的就是用一个短暂的计时器(?500ms?)来抓住滑动和速度的方向

类似

While(swiping) {
   Get(pointTouched);
   swipeDirection = Calc(direction);
   swipeSpeed = Calc(speed);

   FramesToPlay = swipeSpeed * ConstantAmount;

   If(Direction == Backwards){
      FramesToPlay = FramesToPlay * -1;
   }

   Play(playAnimation, FramesToPlay);

   wait(500ms);
}

任何人都知道这样的事吗?或者我可以凑齐的任何作品?

我让动画想出了这个刷卡的细节让我感到困惑。

3 个答案:

答案 0 :(得分:9)

您可以使用UIPanGestureRecognizer方法velocityInView:。我没有对此进行过测试,但似乎它应该可行:

- (void)handlePanGesture:(UIPanGestureRecognizer *)pan
{
    if (pan.state == UIGestureRecognizerStateEnded)
    {
        CGPoint vel = [pan velocityInView:self.view];
        [self doSpinAnimationWithVelocity:vel.x];
    }
}

此外,当pan.state == UIGestureRecognizerChanged时,你可以用手指转动地球仪。

答案 1 :(得分:2)

在当前的UIView中使用touchesBegan和touchesMoved委托。这些代表返回xy位置和时间戳。您可以通过将触摸之间的毕达哥拉斯距离除以增量时间来估计触摸或滑动的速度,并从atan2(dy,dx)获得角度。您还可以通过多次触摸事件对平均值或过滤返回的速度进行平均或过滤。

答案 2 :(得分:1)

以下是我将如何做到这一点:创建UISwipeGestureRecognizer的子类。这个子类的目的只是记住它在UITouch方法中收到的第一个和最后一个touchesBegan:withEvent:个对象。其他所有内容都会转发到super

当识别器触发其动作时,识别器将作为sender参数传入。您可以询问初始和最终触摸对象,然后使用locationInView:方法和timestamp属性来计算滑动的速度(速度=距离变化/时间变化)。 / p>

所以它是这样的:

@interface DDSwipeGestureRecognizer : UISwipeGestureRecognizer 

@property (nonatomic, retain) UITouch * firstTouch;
@property (nonatomic, retain) UITouch * lastTouch;

@end

@implementation DDSwipeGestureRecognizer
@synthesize firstTouch, lastTouch;

- (void) dealloc {
  [firstTouch release];
  [lastTouch release];
  [super dealloc];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  [self setFirstTouch:[touches anyObject]];
  [super touchesBegan:touches withEvent:event];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  [self setLastTouch:[touches anyObject]];
  [super touchesEnded:touches withEvent:event];
}

@end

然后你会去其他地方:

DDSwipeGestureRecognizer *swipe = [[DDSwipeGestureRecognizer alloc] init];
[swipe setTarget:self];
[swipe setAction:@selector(swiped:)];
[myView addGestureRecognizer:swipe];
[swipe release];

你的行动将是:

- (void) swiped:(DDSwipeGestureRecognizer *)recognizer {
  CGPoint firstPoint = [[recognizer firstTouch] locationInView:myView];
  CGPoint lastPoint = [[recognizer lastTouch] locationInView:myView];
  CGFloat distance = ...; // the distance between firstPoint and lastPoint
  NSTimeInterval elapsedTime = [[recognizer lastTouch] timestamp] - [[recognizer firstTouch] timestamp];
  CGFloat velocity = distance / elapsedTime;

  NSLog(@"the velocity of the swipe was %f points per second", velocity);
}

警告:在浏览器中键入的代码未编译。警告实施者。