ios通过垂直线滑动

时间:2013-04-24 20:12:32

标签: ios swipe gesture

我正在尝试从主视图顶部到底部创建一条垂直线,当有人从任一方向滑动并穿过此线时,它将触发一个动作。我已经尝试过创建一个高大的薄标签并在其中放置一个滑动识别器,但它不能按照我想要的方式工作。它只会在您从标签内部开始滑动时检测到滑动,但如果您从标签外部开始并穿过它,则不会检测到滑动。

到目前为止,这是我的代码,

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
swipe.numberOfTouchesRequired = 1;
[self.myLabel setUserInteractionEnabled:YES];
[self.myLabel addGestureRecognizer:swipe];

2 个答案:

答案 0 :(得分:0)

最简单的方法是在touchesBegan:上使用touchesMoved:UIView方法来确定它是否越过了中间线。然后,您只需在此视图的顶部添加一个视图(您的线),您可能希望在“线”视图上禁用用户交互。 BTW,创建一条线的快速脏方法是创建一个宽2px(或任何你想要的宽度)的视图,并将它的背景颜色设置为你的“线”颜色......

您需要在touchesBegan:中确定他们开始的哪一方,然后在touchesMoved:中执行以下操作:

- (void) touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint tappedPt = [[touches anyObject] locationInView: self];
    if (tappedPt.x > self.frame.width/2 && self.userStartedOnLeftSide)
        // User crossed the line...
    else if (tappedPt.x < self.frame.width/2 && !self.userStartedOnLeftSide)
        // User crossed the line...
    else
        // You probably don't need this else, the user DID NOT cross the line...
}

同样,您可以使用滑动手势识别器在UIViewController中执行此操作:

- (void)swipe:(UISwipeGestureRecognizer *)recognizer{
   CGPoint point = [recognizer locationInView:[recognizer view]];
   if (recognizer.state == UIGestureRecognizerStateBegan)
       // The user began their swipe at point
   else if (recognizer.state == UIGestureRecognizerStateEnded)
       // The user ended their swipe at point
   // Add some logic to decide if they crossed the line...

}

答案 1 :(得分:0)

如果您只是尝试检测左右滑动,可以将其放在主视图上,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UISwipeGestureRecognizer *swipe;

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

    swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipe];
}

- (void)handleSwipe:(UISwipeGestureRecognizer *)gesture
{
    NSLog(@"%d", [gesture numberOfTouches]);

    if (gesture.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        NSLog(@"LEFT");
    }
    if (gesture.direction == UISwipeGestureRecognizerDirectionRight)
    {
        NSLog(@"RIGHT");
    }
}

如果您想真正测试是否越过中点,可以使用UIPanGestureRecognizer

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.view addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]];
}

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    static CGPoint startLocation;
    static BOOL startAtLeft;

    CGPoint location = [gesture locationInView:gesture.view];

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        startLocation = location;
        startAtLeft = (location.x < (gesture.view.frame.size.width / 2.0));
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        BOOL nowAtLeft = (location.x < (gesture.view.frame.size.width / 2.0));
        if (startAtLeft != nowAtLeft)
        {
            if (startAtLeft)
                NSLog(@"swiped across midpoint, left to right");
            else
                NSLog(@"swiped across midpoint, right to left");
        }
    }
}

使用平移手势,您可以根据心脏的内容对其进行自定义(例如,一旦用户的手指交叉,就会识别手势,而不是等待他们结束手势等)。