在 UIView 之间滑动手势 [英] Swipe Gesture between UIView

查看:34
本文介绍了在 UIView 之间滑动手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ViewController 类,我在 self.view 上有一个名为 templateView 的 UIView,它由一个名为 gridView 的 UIView 组成,这里我需要在 <代码>模板视图,为此我添加了滑动手势,

I have an ViewController class, where i have an UIView called templateView on self.view, which consists of an UIView called gridView, here i need to swipe on templateView, for that i have added swipegesture like,

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 左侧和右侧.. 我需要在这些方法中实现什么..?

In the swipeRight and swipeLeft i need to move the gridView left side and right side.. What i need to implement in these methods..?

推荐答案

我建议

  1. 使用带有参数的手势处理程序(以防您向多个视图添加手势);

  1. Use a gesture handler that takes a parameter (in case you ever add gestures to multiple views);

确保相关视图已打开 userInteractionEnabled.

Make sure the view in question has userInteractionEnabled turned on.

您不需要设置手势的 delegate 除非您正在实施 UIGestureRecognizerDelegate 方法.

You don't need to set the gesture's delegate unless you're implementing one of the UIGestureRecognizerDelegate methods.

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

Thus, the configuration might look like:

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

然后手势处理程序可能看起来像:

And then the gesture handler might look like:

- (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 定义的,则此处理程序代码必须适当更改.但希望这能给你基本的想法.

Note, if you're using auto layout and if the view is defined by constraints rather than translatesAutoresizingMaskIntoConstraints, then this handler code would have to change appropriately. But hopefully this gives you the basic idea.

这篇关于在 UIView 之间滑动手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆