2011-05-04 87 views
0

我正在嘗試開發一個應用程序給Iphone;但我是新的蜜蜂,所以我有很多問題。用手勢滑動視圖

我的應用程序有一個默認視圖和主視圖中名稱爲flashcard的另一個小視圖。

我想增加這個視圖在Iphone中像照片一樣滑動(或滑動)的功能。例如,如果用戶滑動視圖直到內部視圖的中心到達主視圖的邊界,則將有兩種可能性,1 - 如果用戶在到達內部視圖之前完成滑動,則會返回原始位置。 012-2-如果用戶不停止滑動,內部視圖將從滑動方向在屏幕上熄滅,並從相反方向返回屏幕。

所以我宣佈UIPanGestureRecognizer在viewDidLoad中像下面

UIPanGestureRecognizer *panFlashCard = [[UIPanGestureRecognizer alloc] 
             initWithTarget:self action:@selector(handlePanFlashCard:)]; 

[flashcard addGestureRecognizer:panFlashCard]; 
[panFlashCard release]; 

handPanFlashCard行動:

UIView *piece = [recognizer view]; 
    CGPoint center = piece.center; 
    CGFloat x = 160; // the original x axis of inner view 

    if ([recognizer state] == UIGestureRecognizerStateChanged) { 
     CGPoint translation = [recognizer translationInView:piece ]; 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.55]; 
     [piece setCenter:CGPointMake(piece.center.x + translation.x, center.y)]; 
     [UIView commitAnimations]; 
     CGFloat totalX; 
     totalX= x + translation.x; 
     if(translation.x <0) 
     {    
      if (totalX <= 0) 
      { 
       [self showNextCard]; 
      } 
      else 
      { 
       [UIView beginAnimations:nil context:NULL]; 
          [UIView setAnimationDuration:0.55]; 
          [piece setCenter:CGPointMake(self.view.center.x, center.y)]; 
          [UIView commitAnimations]; 
      } 
     } 
     else 
     { 
      if (totalX >= self.view.bounds.size.width) 
      { 
       [self showPreviousCard]; 
      } 
      else 
      { 
       [UIView beginAnimations:nil context:NULL]; 
       [UIView setAnimationDuration:0.55]; 
       [piece setCenter:CGPointMake(self.view.center.x, center.y)]; 
       [UIView commitAnimations]; 
      } 
     } 
    } 

showNextCard行動:

[flashcard setCenter:CGPointMake(self.view.bounds.size.width + flashcard.bounds.size.width, flashcard.center.y)]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.55]; 
    [flashcard setCenter:CGPointMake(self.view.center.x, flashcard.center.y)]; 
    [UIView commitAnimations]; 

showPreviousCard行動:

[flashcard setCenter:CGPointMake(0 - flashcard.bounds.size.width, flashcard.center.y)]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.55]; 
    [flashcard setCenter:CGPointMake(self.view.center.x, flashcard.center.y)]; 
    [UIView commitAnimations]; 

當我運行這個應用程序,它建立並運行成功,但有動畫錯誤,我認爲一些框架錯誤。

請幫我改正這段代碼,並在滑動時運行流暢的動畫。

回答

0

如果我明白你正在嘗試做什麼,那麼你可以使用內置的控件。查看UIScrollView類的pagingEnabled屬性。還有一個名爲PhotoScroller的示例項目可能有所幫助。

http://developer.apple.com/library/ios/#samplecode/PhotoScroller/Introduction/Intro.html

+0

我會看樣例代碼;但我不確定這些控件適合我。因爲我想在內部視圖中添加一些標籤並從sqlite數據庫中獲取新數據每次完整刷卡(NextCard或PreviousCard操作) – 2011-05-05 06:10:25

+0

您可以將所需的任何視圖添加到滾動視圖。所以你可以將你的數組與每個卡作爲一個擁有多個標籤的UIView。然後,你只需要弄清楚哪張卡片正在顯示,並進行加載。 – jaminguy 2011-05-05 21:59:47