简体   繁体   中英

ios swipe through vertical line

I'm trying to make a vertical line from the top of my main view to the bottom, and it will trigger an action when someone swipes and crosses this line from either direction. I've already tried creating a tall thin label and putting a swipe recognizer inside of this, but it does not work the way I want. It only detects a swipe when you start inside of the label and swipe but not if you start outside the label and cross through it.

Here is my code so far,

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

The easiest way is to use the touchesBegan: and touchesMoved: method on a UIView to decide if it's crossed the midway line. You would then just add a view (your line) on top of this view, and you may want to disable user interaction on your "line" view. BTW, a quick dirty way to create a line is to create a view that's 2px wide (or whatever width you want) and set it's background color to your "line" color...

You need to decide in touchesBegan: which side they started on, and then do something like this in 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...
}

Similarly, you could do this in a UIViewController with a swipe gesture recognizer something like this:

- (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...

}

If you're just trying to detect left and right swipes, you can put it on your main view like so:

- (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");
    }
}

If you wanted to really test to see if you crossed the midpoint, you could use a 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");
        }
    }
}

With the pan gesture, you can customize this to your heart's content (eg recognize gesture as soon as the user's finger crosses rather than waiting for them to end the gesture, etc.).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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