繁体   English   中英

在UIView之间滑动手势

[英]Swipe Gesture between UIView

我有一个ViewController类,我在self.view上有一个名为templateView的UIView,它包含一个名为gridView的UIView,这里我需要在templateView上滑动,因为我已经添加了swipegesture,

swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction)];
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction)];

swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
swipeRight.delegate = self;

swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delegate = self;

 [templateView addGestureRecognizer:swipeRight];
 [templateView addGestureRecognizer:swipeLeft];

swipeRightswipeLeft我需要移动gridView左侧和右侧。我需要这些方法来实现什么..?

我建议

  1. 使用带参数的手势处理程序(如果您将手势添加到多个视图中);

  2. 确保有问题的视图已打开userInteractionEnabled

  3. 除非您正在实现其中一个UIGestureRecognizerDelegate方法,否则无需设置手势的delegate

因此,配置可能如下所示:

templateView.userInteractionEnabled = YES;

swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[templateView addGestureRecognizer:swipeRight];

swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[templateView addGestureRecognizer:swipeLeft];

然后手势处理程序可能如下所示:

- (void)handleSwipe:(UISwipeGestureRecognizer *)gesture
{
    CGRect frame = self.gridView.frame;

    // I don't know how far you want to move the grid view.
    // This moves it off screen.
    // Adjust this to move it the appropriate amount for your desired UI

    if (gesture.direction == UISwipeGestureRecognizerDirectionRight)
        frame.origin.x += self.view.bounds.size.width;
    else if (gesture.direction == UISwipeGestureRecognizerDirectionLeft)
        frame.origin.x -= self.view.bounds.size.width;
    else
        NSLog(@"Unrecognized swipe direction");

    // Now animate the changing of the frame

    [UIView animateWithDuration:0.5
                     animations:^{
                         self.gridView.frame = frame;
                     }];
}

注意,如果您正在使用自动布局,并且如果视图是由约束而不是translatesAutoresizingMaskIntoConstraints定义的,则此处理程序代码必须适当更改。 但希望这能为您提供基本的想法。

您可以使用一些UIViewAnimations移动gridView。 创建类似的东西:

-(void)swipeRightAction{
    [UIView setAnimationDuration:1];
    gridView.frame = CGRectMake(320,0);
    [UIView commitAnimations];
}

此代码将更改gridView的框架。 您需要根据要滑动视图的位置更改此参数。 我没有尝试代码,让我知道它是否有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM