2011-02-01 41 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