【问题标题】:How to do smooth swipe animation in ios如何在 ios 中制作平滑的滑动动画
【发布时间】:2015-05-28 01:08:51
【问题描述】:

我想做流畅的滑动动画。我只想当用户仅从右边框或左边框滑动页面时才能进行滑动。不能在页面中间滑动。从左到右和从右到左都应该可以滑动。

我尝试了很多滑动动画示例代码或演示代码。但这不是我想要的。我想要这样的动画https://itunes.apple.com/in/app/clear-tasks-to-do-list/id493136154?mt=8

在这个应用程序中,当我们触摸右边框时,它会平滑滑动。请指导我做这个动画。提前致谢。

【问题讨论】:

  • 您可以使用滑动手势...它可以让您左右滑动和上下滑动。

标签: ios objective-c animation swipe gesture


【解决方案1】:

抱歉回复晚了。刚看到这个问题。

如果您希望从边缘进行滑动操作,请在主视图的远端(左侧和右侧)创建 2 个子视图,然后将宽度设置为 30 或 40。

我相信您还有 2 个其他视图从左侧和右侧弹出。因此,为了做到这一点,您需要在主视图的顶部添加 2 个视图。 现在对于左视图,将连接到主视图的右侧水平空间约束设置为小于主视图的 (-1)x 宽度的值。对于右视图,将其连接到主视图的右侧水平空间约束设置为大于主视图宽度的值,以便两个视图都在主视图之外

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]; 
       }];

    }
}

}

这将制作从左到右和从右到左的滑动动画。

希望这会有所帮助..

【讨论】:

    【解决方案2】:

    创建 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;
        }
    }
    

    【讨论】:

      【解决方案3】:

      在 ios7 中有一个手势识别器specifically for gestures beginning from the edge of the screen。你应该使用这个。

      我无法解决您的“流畅”问题,因为您还没有说明您当前的动画是什么样子或者您是如何制作的。但是平移手势,例如直接更新视图位置的链接手势,将比滑动更顺畅地跟踪用户的移动。

      【讨论】:

      • 感谢您提供详细信息。我会检查并告诉你是否需要更多帮助。
      猜你喜欢
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多