iPhone UIPickerView 手势
-
20-09-2019 - |
题
我将如何向 uipickerview 添加手势事件来更改选项卡?我必须创建一个自定义类,但是,我不知道如何处理 uipickerview。我当前在 uiviews 中存在手势来执行此操作,但我在使用 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];
}
谢谢您的帮助。
解决方案
您不能继承UIPickerView。
然而,由于选择器视图必须在另一视图中被显示(因为它不占用整个屏幕),则可以在陷阱该视图的控制器和滤波器,用于手势的触摸。
(假设我理解你试图做...)我会警告说,刷卡挑选器以改变标签是一种非标准的用户界面和可能会迷惑用户。由于选择器视图被认为是一个类型的控制,他们会期望只拾取器的正常的旋转动作。他们怎么会知道到水平滑动一个选择器视图?
其他提示
您可以继承UIPickerView。问题是,它是由九个子视图,其中一个名为UIPickerTable,正在接收触摸事件,如touchesBegan:withEvent:
到你想要的行。我能够与包含在这篇文章的末尾码成功拦截这些:
响应的touchesBegan在UIPickerView而不是UIView的的
其他答案/评论有也有帮助。我想澄清事实,而不是别人做一些不规范的(在这个问题),但人谁来到这里希望继承UIPickerView,因为公认的答案的第一行是完全错误的。
我离开它独自。它会被混淆,你是对的。
ZT> 你不能子类化 UIPickerView。“为了让选取器视图旋转得更久,我对 UIPickerView 进行了子类化。我的子类只有一种方法” 更长的旋转和模糊. 。另请参阅 UIPickerView 类参考: :“UIDatePicker 类使用 UIPickerView 的自定义子类来显示日期和时间。”