0

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

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

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

2 回答 2

0

最简单的方法是在 a 上使用touchesBegan:andtouchesMoved:方法UIView来确定它是否越过中线。然后,您只需在此视图之上添加一个视图(您的线),并且您可能希望禁用“线”视图上的用户交互。顺便说一句,创建线条的一种快速肮脏的方法是创建一个 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...

}

于 2013-04-24T20:16:19.237 回答
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");
        }
    }
}

使用平移手势,您可以根据自己的意愿对其进行自定义(例如,只要用户手指交叉就识别手势,而不是等待他们结束手势等)。

于 2013-04-24T20:50:40.563 回答