iPhone - UIWebView手势

时间:2010-02-16 19:18:21

标签: iphone cocoa-touch uiwebview gesture

我有viewcontrollers设置为跟踪手势并提交动作,在这种情况下,更改选项卡。 viewcontrollers的代码如下:

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

我想在我在viewcontroller视图下设置的uiwebview上做同样的动作做同样的事情。简而言之,uiwebview就是在观点上。当我滑动时,我只获得基本的uiwebview功能(滚动)。如果我使用与uiviewcontroller相同的代码设置自定义uiwebview类,则会出现错误。我需要知道如何翻译该代码块,保持功能完整,以适应uiwebview。

2 个答案:

答案 0 :(得分: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 s,将父级注册为监听器。

答案 1 :(得分: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

代码中包含以下错误。

我很感激帮助。