2014-04-18 43 views
0

我想做平滑滑動動畫。我只想僅當用戶從右側或左側邊框滑動頁面時纔可以進行滑動。頁面中間滑動不可能。滑動應該可以從左到右和從右到左。如何在ios中平滑滑動動畫

我已經試過很多刷卡動畫示例代碼或演示代碼。但它不是我想要的。我想這樣的https://itunes.apple.com/in/app/clear-tasks-to-do-list/id493136154?mt=8

在這個動畫中應用程式它就像當我們觸摸它的刷卡smoothly.Please指導我的右邊界做這個動畫。提前致謝。

+0

您可以使用滑動手勢......它會讓您滑動左右和上下滑動。 –

回答

2

很抱歉這麼晚纔回復。剛剛看到這個問題。

如果你希望你的刷卡操作,從邊緣發生,創建主視圖的遠端處2子視圖(左,右),並給予隨後的30寬或40

enter image description here

我相信你有2個其他的意見從左到右。所以爲了做到這一點,你需要在你的主視圖上添加2個視圖。 現在對於左視圖,將連接到主視圖的右側水平空間約束設置爲小於主視圖的(-1)x寬度的值。用於右視圖設置其右側horizo​​ndal空間約束連接到主視圖,以比主視圖的寬度更大的值,使得兩個視圖都主視圖

X stands for a value greater than or equal to the mainview's width

X代表外大於或等於主視圖寬度的值

將兩個NSLayoutConstraint變量添加爲包含這兩個值的IBOutlet。

NSLayoutConstraint *leftViewHorizondalRightPadding; 
NSLayoutConstraint *rightViewHorizondalRightPadding; 

現在將UISwipeGestures添加到這些子視圖(用橙色表示)。

UISwipeGestureRecognizer *leftToRightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
    [leftToRightSwipe setDirection:UISwipeGestureRecognizerDirectionRight]; 
    [self.leftSubview addGestureRecognizer:leftToRightSwipe]; 

UISwipeGestureRecognizer *rightToLeftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
    [rightToLeftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft]; 
    [self.rightSubview addGestureRecognizer:rightToLeftSwipe]; 

///Now in the swipe handler distinguish the swipe actions 
-(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer { 
    NSLog(@"Swipe received."); 
    if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) { 
    //It's leftToRight 
     leftViewHorizondalRightPadding.constant = 0; 
     [UIView animateWithDuration:1 
      animations:^{ 
      [self.view layoutIfNeeded]; 
     }]; 

    } 
    else { 
    //It's rightToLeft 
     rightViewHorizondalRightPadding.constant = 0; 
     [UIView animateWithDuration:1 
      animations:^{ 
      [self.view layoutIfNeeded]; 
     }]; 

    } 
} 

}

這將使刷卡動畫從左至右,從右到左。

希望這會有所幫助..

0

後創建2滑動手勢識別器,你應該設置他們的代表。然後用這個委託方法:

UISwipeGestureRecognizer *_swipeLeft; 
UISwipeGestureRecognizer *_swipeRight; 
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { 
    static const CGFloat borderWidth = 50.0f; 
    if(gestureRecognizer == _swipeLeft) { 
     return [gestureRecognizer locationInView:self].x > self.frame.size.width - borderWidth; 
    } 
    else if(gestureRecognizer == _swipeRight) { 
     return [gestureRecognizer locationInView:self].x < borderWidth; 
    } 
    return YES; 
} 

請注意,順利刷卡/拖你可能會需要使用全景手勢,甚至長按手勢識別器而不是滑動手勢。它們非常相似,除了長按需要一點時間才能開始(可設置)。如果你使用它們,你可能仍然想使用相同的委託方法。或者,您可以簡單地執行手勢目標方法中的所有代碼。嘗試這樣的:

CGPoint gestureStartPoint; 
- (void)dragFromBoreder:(UIGestureRecognizer *)sender { 
    static const CGFloat borderWidth = 50.0f; 
    switch (sender.state) { 
     case UIGestureRecognizerStateBegan: { 
      CGPoint location = [sender locationInView:self]; 
      if(location.x > borderWidth || location.x < self.frame.size.width-borderWidth) { 
       //break the gesture 
       sender.enabled = NO; 
       sender.enabled = YES; 
      } 
      else { 
       gestureStartPoint = location; 
      } 
      break; 
     } 
     case UIGestureRecognizerStateChanged: { 
      CGPoint location = [sender locationInView:self]; 
      CGFloat deltaX = location.x - gestureStartPoint.x; 
      UIView *viewToMove; 
      CGPoint defaultCenter; 
      viewToMove.center = CGPointMake(defaultCenter.x+deltaX, defaultCenter.y); 
      break; 
     } 
     case UIGestureRecognizerStateEnded: 
     case UIGestureRecognizerStateCancelled: { 
      CGPoint location = [sender locationInView:self]; 
      CGFloat deltaX = location.x - gestureStartPoint.x; 
      /* 
      if(deltaX > someWidth) { 
       show the left view 
      } 
      else if(deltaX < -someWidth) { 
       show the right view 
      } 
      else { 
       put everything back the way it was 
      } 
      */ 
      break; 
     } 
     default: 
      break; 
    } 
} 
0

在ios7中有一個手勢識別器specifically for gestures beginning from the edge of the screen。你應該使用這個。

我不能和你的「平穩」的問題有所幫助,因爲你還沒說你當前的動畫看起來像什麼或你是如何做的。但是,像直接更新視圖位置的鏈接手勢,平移手勢將比滑動更順暢地跟蹤用戶的移動。

+0

感謝您的細節。我會檢查它並告訴你是否需要任何幫助。 – NSP