2010-02-16 107 views
2

我怎么会去加入的手势事件uipickerview改变标签?我必须创建一个自定义类,但是,我不知道如何处理uipickerview。我目前在uiviews中有这样的手势,但我在uipickerview遇到问题。iPhone UIPickerView手势

我的看法代码:

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

感谢您的帮助。

+0

你可能想澄清你到底想要做什么。你是否试图用UIPickerView捕捉的轻扫手势切换UITabbar选项卡?你是否试图使用滑动来操纵UIPickerView本身? – TechZen 2010-02-16 15:24:36

回答

0

你不能继承UIPickerView。

然而,由于选择器视图必须在其他视图中显示(因为它不占用整个屏幕),你就能够在该视图的控制器和滤波器手势触摸。

(假设我理解你试图做...)我会警告说,刷卡挑选器以改变标签是一种非标准的用户界面和可能会迷惑用户。由于选取器视图被视为一种控制类型,因此他们只会期望选取器的正常旋转动作。他们甚至会怎么知道水平滑动选取器视图?

+0

如果这是正确答案,请点击旁边的复选标记。 – TechZen 2010-02-16 19:42:45

+1

你绝对可以继承UIPickerView,在某些情况下它确实是一个好主意,但是你的答案的后半部分并不坏。 – pob21 2012-03-20 05:15:46

0

我一个人留下。这会让人困惑,你是对的。

-1

ZT>你不能继承UIPickerView。 “为了使选取器视图旋转更长时间,我将子类UIPickerView。我的子类只有一个方法”从Longer Spinning and Blurring。另请参阅UIPickerView Class Reference:“UIDatePicker类使用UIPickerView的自定义子类来显示日期和时间。”

2

你也可以继承UIPickerView。问题在于它包含九个子视图,其中一个名为UIPickerTable,正在接收类似touchesBegan:withEvent:的触摸事件。我能够成功地拦截这些包含在本文结尾的代码: Responding to touchesBegan in UIPickerView instead of UIView

其他答案/评论也有帮助。我想清除空气,而不是为了某人做非标准的事情(如在这个问题中),但是对于那些想要继承UIPickerView的人来说,因为接受的答案的第一行是错误的。

+0

当我在运行时查看UIPickerView的子视图时,我只看到3,所有这些只是说他们是标准的UIView类。 – stonedauwg 2016-08-05 19:20:38