2014-04-18 44 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