0

我设置了视图控制器来跟踪手势并提交操作,在这种情况下,更改选项卡。视图控制器的代码如下:

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

我希望在视图控制器视图下设置的 uiwebview 上使用相同的手势执行相同的操作。简而言之,uiwebview 在视图上。当我滑动时,我只是获得基本的 uiwebview 功能(滚动)。如果我使用与 uiviewcontroller 相同的代码设置自定义 uiwebview 类,则会出现错误。我需要知道如何翻译该代码块,保持功能完整,以适应 uiwebview。

4

2 回答 2

1

您应该能够继承 UIWebView 并覆盖这些方法。但是,您可能必须覆盖所有的触摸方法,

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

编辑:啊,我现在看到你的错误了。它们是因为这些东西不适用于 UIWebView 就像它们对父级所做的那样(你已经知道了......)

由于 UIWebView 是一个 UIView,你应该可以替换

[touch locationInView:self.view]

[touch locationInView:self]

但是,要访问父级的标签栏控制器,您必须设计一种不同的方式来执行此操作。您可以将对父级的引用传递给您的 UIWebView 子类,并在父级上公开一个方法以更改哪个选项卡处于活动状态,例如:

在您的子类中,为父标签栏控制器添加一个属性:

UITabBarController *parent;

创建 UIWebView 子类并将其添加到标签栏控制器时,还要设置父类:

webView.parent = self;

如果您在 Interface Builder 中进行设置,请将父级设为 IBOutlet 并通过 IB 将其连接起来。

在您的标签栏控制器视图中,添加一个更改所选控制器的方法。您可以为要切换到的视图使用命名常量,或者使用准确描述您要切换到的内容的方法,但为简单起见,您可以在概念上执行类似的操作:

- (void) switchToView: (int)viewNumber 
{
    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:viewNumber]; 
}

然后调用[parent switchToView:2]你的代码。

另一种可以说是更好的方法是使用NSNotification,将父级注册为侦听器。

于 2010-02-16T19:44:08.077 回答
0

我的完整代码如下:

#import "CustomWebView.h"

@implementation CustomWebView

//Swipe between tabs
#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]; "error: request for member 'view' in something not a structure or union"


    if(mystartTouchPosition.x != newTouchPosition.x || mystartTouchPosition.y != newTouchPosition.y) {
        isProcessingListMove = NO;
    }
    mystartTouchPosition = [touch locationInView:self.view]; "error: request for member 'view' in something not a structure or union"
    [super touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = touches.anyObject;
    CGPoint currentTouchPosition = [touch locationInView:self.view]; "error: request for member 'view' in something not a structure or union"

    // 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]; "error: request for member 'tabBarController' in something not a structure or union"
            return;
        }
        else {
            isProcessingListMove = YES;

            self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:2]; "error: request for member 'tabBarController' in something not a structure or union"

            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];
}
// End of swipe

@end

代码中包含以下错误。

我很感激帮助。

于 2010-02-16T19:47:25.547 回答