【问题标题】:iPhone - UIWebView GesturesiPhone - UIWebView 手势
【发布时间】:2010-02-16 19:18:21
【问题描述】:

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

#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。

【问题讨论】:

  • 你的意思是你继承了 UIWebView 并添加了这段代码,你得到了错误?你得到什么错误?

标签: iphone cocoa-touch uiwebview gesture


【解决方案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]

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

【讨论】:

  • 查看我之前的帖子。谢谢。
  • 感谢您的回复。我想出了 self.view 到 self,但我被困在 tabBarController 部分。你能详细说明一下吗?
  • 感谢您的编辑。不过,我仍然遇到 tabBarController 错误的问题。我真的很感激一些帮助,拜托。非常感谢。
  • 对不起;我花了一段时间来编辑它。再看一遍。或者,查看 NSNotification:developer.apple.com/mac/library/documentation/Cocoa/Reference/…
  • 感谢您的回复。我的 tabBarController 是通过 IB 设置的,我只是在 AppDelegate 中引用它。那么我将如何设置父级和 webView 连接?另外,我知道我必须在 TabBarController.m 中添加 switchToView 方法,但是,由于使用 IB 创建,我没有 TabBarController.m。
【解决方案2】:

我的完整代码如下:

#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

代码中包含以下错误。

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    相关资源
    最近更新 更多