gpt4 book ai didi

iPhone UIPickerView 手势

转载 作者:行者123 更新时间:2023-12-03 21:23:51 27 4
gpt4 key购买 nike

我将如何向 uipickerview 添加手势事件来更改选项卡?我必须创建一个自定义类,但是,我不知道如何处理 uipickerview。我当前在 uiviews 中存在手势来执行此操作,但我在使用 uipickerview 时遇到了问题。

我的 View 代码:

#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。问题是它由九个 subview 组成,其中一个名为 UIPickerTable,正在接收触摸事件,例如 touchesBegan:withEvent: 到您想要的行。我能够使用本文末尾包含的代码成功拦截这些内容: Responding to touchesBegan in UIPickerView instead of UIView

那里的其他答案/评论也很有帮助。我想澄清一下,不是为了那些做非标准事情的人(如这个问题),而是为了那些来到这里希望子类化 UIPickerView 的人,因为接受的答案的第一行是完全错误的。

关于iPhone UIPickerView 手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2273543/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com