2011-02-01 50 views
3

基本上我试图把你的手指放在地球上,并旋转它的类型功能。iOS - 先进的动画与刷卡W /速度的刷卡

所以我真正需要做的是抓住刷卡和速度与短计时器的方向(?500毫秒?)

因此,像

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

    FramesToPlay = swipeSpeed * ConstantAmount; 

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

    Play(playAnimation, FramesToPlay); 

    wait(500ms); 
} 

任何人都知道这样的事?或者我可以拼凑在一起的任何碎片?

我有动画发现它只是这个刷卡的细节让我感到困惑。

+0

我在这里,并试图整合每个人的意见和答案,我非常感谢。至于你之前的评论,我也试着回复一下,所以我可以投票并回馈给那些帮助过的人。 – Mytheral 2011-02-02 12:44:29

回答

9

您或许可以使用UIPanGestureRecognizer,它有一个velocityInView:方法。我没有测试过这一点,但似乎像它应该工作:

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

此外,当pan.state == UIGestureRecognizerChanged,你可以有你的地球与手指右转沿。

2

在当前UIView中使用touchesBegan和touchesMoved委托。这些代表返回xy位置和时间戳。您可以通过将触摸之间的毕达哥拉斯距离除以德尔塔时间来估计触摸或滑动的速度,并从atan2(dy,dx)获取角度。您也可以对返回的速度进行平均或过滤,方法是在多个触摸事件中执行此操作。

1

下面是我该怎么做的:创建一个UISwipeGestureRecognizer的子类。这个子类的目的只是为了记住它在touchesBegan:withEvent:方法中收到的第一个也是最后一个UITouch对象。其他一切都会被转发到super

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

所以它会是这样的:

@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); 
} 

警告:在浏览器中键入的代码,未编译。警告执行者。

+0

我不确定touchesBegan和touchesEnd的超类调用是否有效。我相信UIGestureRecognizers不属于响应者链。我可能错了。 – nico 2011-04-02 16:34:26