简体   繁体   English

iPhone UIPickerView手势

[英]iPhone UIPickerView Gestures

How would I go about adding gesture events to uipickerview to change tabs? 我将如何向uipickerview添加手势事件以更改选项卡? I have to create a custom class, however, I don't know how to handle the uipickerview. 我必须创建一个自定义类,但是,我不知道如何处理uipickerview。 I current have gestures present in uiviews to do this, but I'm having trouble with the uipickerview. 我目前在uiviews中有手势可以做到这一点,但是我在使用uipickerview时遇到了麻烦。

My code for the views: 我的意见代码:

#define HORIZ_SWIPE_DRAG_MIN 100
CGPoint mystartTouchPosition;
BOOL isProcessingListMove;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint newTouchPosition = [touch locationInView:self.view];
    if(mystartTouchPosition.x != newTouchPosition.x || mystartTouchPosition.y != newTouchPosition.y) {
        isProcessingListMove = NO;
    }
    mystartTouchPosition = [touch locationInView:self.view];
    [super touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = touches.anyObject;
    CGPoint currentTouchPosition = [touch locationInView:self.view];

    // If the swipe tracks correctly.
    double diffx = mystartTouchPosition.x - currentTouchPosition.x + 0.1; // adding 0.1 to avoid division by zero
    double diffy = mystartTouchPosition.y - currentTouchPosition.y + 0.1; // adding 0.1 to avoid division by zero

    if(abs(diffx / diffy) > 2.5 && abs(diffx) > HORIZ_SWIPE_DRAG_MIN)
    {
        // It appears to be a swipe.
        if(isProcessingListMove) {
            // ignore move, we're currently processing the swipe
            return;
        }

        if (mystartTouchPosition.x < currentTouchPosition.x) {
            isProcessingListMove = YES;
            self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
            return;
        }
        else {
            isProcessingListMove = YES;

            self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:2];

            return;
        }
    }
    else if(abs(diffy / diffx) > 1)
    {
        isProcessingListMove = YES;
        [super touchesMoved:touches withEvent:event];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    isProcessingListMove = NO;
    [super touchesEnded:touches withEvent:event];
}

Thanks for the help. 谢谢您的帮助。

You CAN subclass UIPickerView. 您可以继承UIPickerView。 The problem is that it's comprised of nine subviews, one of which, called UIPickerTable, is receiving the touch events like touchesBegan:withEvent: to the rows you want. 问题在于它由9个子视图组成,其中一个称为UIPickerTable,它正在接收想要的行的touch事件,如touchesBegan:withEvent: I was able to successfully intercept these with the code included at the end of this post: Responding to touchesBegan in UIPickerView instead of UIView 我可以使用本文结尾处包含的代码成功拦截这些内容: 响应UIPickerView而不是UIView中的touchesBegan

The other answers/comments there are helpful too. 那里的其他答案/评论也有帮助。 I wanted to clear the air, not for someone doing something non-standard (as in this question), but for someone who arrived here wishing to subclass UIPickerView, because the first line of the accepted answer is dead wrong. 我想澄清一下,不是为了做一些非标准的事情(像在这个问题中一样),而是为了一个想继承UIPickerView子类的人来到这里,因为公认的答案的第一行是完全错误的。

You can't subclass UIPickerView. 您不能继承UIPickerView。

However, since a picker view must be displayed in another view (since it doesn't take up the entire screen) you can trap the touches in that view's controller and filter for gestures. 但是,由于选择器视图必须显示在另一个视图中(因为它不会占据整个屏幕),因此您可以将触摸捕获在该视图的控制器中并过滤手势。

(Assuming I understand what your trying to do...) I would warn that swiping a picker view to change tabs is a non-standard UI and will probably confuse your users. (假设我了解您的操作意图...)我警告说,滑动选择器视图以更改选项卡是非标准的UI,并且可能会使您的用户感到困惑。 Since a picker view is perceived as a type of control, they will expect only the normal spinning action of the picker. 由于选择器视图被视为一种控件,因此他们将只期望选择器的正常旋转动作。 How would they even know to swipe a picker view horizontally? 他们甚至不知道如何水平滑动选择器视图?

I left it alone. 我不理会它。 It would have been confusing, you're right. 这是令人困惑的,您是对的。

ZT> You can't subclass UIPickerView. ZT>您不能继承UIPickerView。 "In order to make the picker view spin longer, I subclassed UIPickerView. My subclass had exactly one method" from Longer Spinning and Blurring . 更长的旋转和模糊化 ”中的“为了使选择器视图旋转的时间更长,我将UIPickerView子类化。我的子类只有一种方法”。 Also see UIPickerView Class Reference : "The UIDatePicker class uses a custom subclass of UIPickerView to display dates and times." 另请参见UIPickerView类参考 :“ UIDatePicker类使用UIPickerView的自定义子类来显示日期和时间。”

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

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