2010-02-16 105 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 

的錯誤是如下所包含的代碼。

我很感激幫助。