2010-02-16 104 views
0

我有viewcontrollers设置为跟踪手势并提交一个操作,在这种情况下,更改选项卡。对于viewcontrollers的代码如下:iPhone - UIWebView手势

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

+0

你的意思是你的子类UIWebView并添加了这段代码,你会得到错误?你会得到什么错误? – Don

回答

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,将父级注册为侦听器。

+0

检查我以前的帖子。谢谢。 – intl

+0

感谢您的回复。我想到self.view自我,但我卡在tabBarController部分。你能详细解释一下吗? – intl

+0

所以代码格式很好,我会编辑我的帖子。 – Don

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 

的错误是如下所包含的代码。

我很感激帮助。