2012-07-19 95 views
1

新的一天,一個新的問題!UIWebView addSubview:UIImageView在觸摸事件觸發時不顯示

我一直在研究一個應用程序,它需要我在用戶手指觸摸屏幕時顯示一個圓圈。

我跟着tutorial我發現子類UIWindow和my comment解釋如何傳遞這些觸摸,如果你正在使用故事板和ARC。

我使用下面的代碼(其是基於this tutorial),以使觸摸指示符來顯示:

#pragma mark - Touch Events 
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    NSArray *subviews = [self.webView subviews]; 
    for (UIView *view in subviews) 
    { 
     // Check if view is scrollview 
     if ([view isKindOfClass:[UserTouchImageView class]]) 
     { 
      [view removeFromSuperview]; 
     } 
    } 

    // Enumerate over all the touches and draw a red dot on the screen where the touches were 
    [touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { 
     // Get a single touch and it's location 
     UITouch *touch = obj; 
     CGPoint touchPoint = [touch locationInView:self.webView]; 

     // Add touch indicator 
     UserTouchImageView *touchView = [[UserTouchImageView alloc] initWithFrame:CGRectMake(touchPoint.x -15, touchPoint.y -15, 30, 30)]; 
     [self.webView addSubview:touchView]; 
    }]; 
    NSLog(@"Touches began"); 
} 
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 
    NSArray *subviews = [self.webView subviews]; 
    for (UIView *view in subviews) 
    { 
     // Check if view is scrollview 
     if ([view isKindOfClass:[UserTouchImageView class]]) 
     { 
      [view removeFromSuperview]; 
     } 
    } 

    // Enumerate over all the touches and draw a red dot on the screen where the touches were 
    [touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { 
     // Get a single touch and it's location 
     UITouch *touch = obj; 
     CGPoint touchPoint = [touch locationInView:self.webView]; 

     // Draw a red circle where the touch occurred 
     UserTouchImageView *touchView = [[UserTouchImageView alloc] initWithFrame:CGRectMake(touchPoint.x -15, touchPoint.y -15, 30, 30)]; 
     [self.webView addSubview:touchView]; 
    }]; 
    NSLog(@"Touches moved"); 
} 
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { 
    NSArray *subviews = [self.webView subviews]; 
    for (UIView *view in subviews) 
    { 
     // Check if view is scrollview 
     if ([view isKindOfClass:[UserTouchImageView class]]) 
     { 
      [UIView animateWithDuration:0.5f 
          animations:^{ 
           view.alpha = 0.0; 
          } 
          completion:^(BOOL finished) { 
           [view removeFromSuperview]; 
          }]; 
     } 
    } 
    NSLog(@"Touches ended"); 
} 
- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event { 
    NSArray *subviews = [self.webView subviews]; 
    for (UIView *view in subviews) 
    { 
     // Check if view is scrollview 
     if ([view isKindOfClass:[UserTouchImageView class]]) 
     { 
      [view removeFromSuperview]; 
     } 
    } 
    NSLog(@"Touches cancelled"); 
} 

UserTouchImageView是的UIImageView的子類,與變更到默認感:

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     self.image = [UIImage imageNamed:@"greyCircle.png"]; 
     self.alpha = 0.5f; 
    } 
    return self; 
} 

我已經測試了這個沒有UIWebview的代碼(用self.view代替self.webView),它工作正常,繪製了我點擊和拖動的圓,然後當我釋放按鈕時淡出。

我還成功實例化了UserTouchImageView,並將其添加到viewViewer的viewDidLoad方法的webView中。

只要我把UIWebview放在上面的代碼中,同一行([self.webView addSubview:touchView];)就不起作用。我仍然可以將NSLog消息發送到控制檯,但子視圖不會被明顯添加。

任何人都可以幫我找出爲什麼這不會出現?是否還有其他我需要注意的代碼,或者我無法做到這一點的任何原因?

非常感謝!

編輯

我已經上傳我的源至今here (MediaFire)

EDIT 2

我下面添加了兩個方法:

-(void)listWebViewSubViews 
{ 
    NSLog(@"BEGIN LISTING SUBVIEWS"); 
    for (UIView *view in [self.webView subviews]) { 
     NSLog(@"Subview: %@", view.description); 
    } 
    NSLog(@"END LISTING SUBVIEWS"); 
} 
-(void)view:(UIView *)view addTouchIndicatorWithCentreAtPoint:(CGPoint)point 
{ 
    NSLog(@"View: %@, x: %f, y: %f", view.description, point.x, point.y); 

    // Add touch indicator 
    UserTouchImageView *touchView = [[UserTouchImageView alloc] initWithFrame:CGRectMake(point.x -15, point.y -15, 30, 30)]; 

    [view addSubview:touchView]; 


} 

這些被稱爲從UIWebView外部的兩個按鈕,以及內部的的touchesBegan方法:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    NSArray *subviews = [self.webView subviews]; 
    for (UIView *view in subviews) 
    { 
     if ([view isKindOfClass:[UserTouchImageView class]]) 
     { 
      [view removeFromSuperview]; 
     } 
    } 

    [touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { 
     UITouch *touch = obj; 
     CGPoint touchPoint = [touch locationInView:self.webView]; 

     [self view:self.webView addTouchIndicatorWithCentreAtPoint:touchPoint]; 

     [self listWebViewSubViews]; 
    }]; 
    NSLog(@"Touches began"); 
} 

的記錄似乎表明,web視圖通過self.webView引用WITHIN的觸摸開始從兩個按鈕引用時方法比一個完全獨立的實例。我可以通過按鈕添加多個子視圖,然後列出它們,並且它們都顯示在日誌中,但是當我單擊以在模擬器上觸摸時,這些額外的子視圖都不會列出。

有誰知道任何特殊功能的UIWebView有觸摸事件的重複實例嗎?

+0

首先,檢查你要添加的子視圖不'nil'。然後嘗試添加一個'setNeedsDisplay'調用。這也可能是因爲你在一個街區內打電話。你在塊內嘗試了'NSLog'嗎? – Dustin 2012-07-19 16:55:18

+0

嗨達斯汀,謝謝你的回覆。它完全通過了我,因爲它在塊內部被調用,所以我已經嘗試了你的兩個建議,對象不是'nil','setNeedsDisplay'沒有可見的效果。我也嘗試在調用dispatch_async(dispatch_get_main_queue(),^ {});''時調用'[selv.webView addSubview:touchView];'。那也行不通。我會繼續尋找,謝謝你的幫助! – 2012-07-20 09:36:29

回答

0

問題解決了:

很不幸,這是由somethign完全不同造成的,所以謝謝大家對你給予的幫助。

這個問題來自於我嘗試修復代碼以與故事板一起運行。我試圖從故事板訪問視圖控制器,並將其作爲接收觸摸事件的視圖作爲優先事項添加(請參閱我鏈接到的教程,以及我也回覆了的評論)。我應該一直在使用self.window.rootViewController。

我更新的評論是here

0

A UIWebView不是一個視圖,而是一個視圖的集合。當您將子視圖添加到UIWebView時,新的子視圖將添加到子視圖的層次結構中。您的新子視圖可能被添加在其他不透明視圖後面。它在那裏,但隱藏。我建議將此指標視圖添加到層​​次結構中的不同視圖。

+0

感謝您提供這些信息,我從一個按鈕中調用了相同的代碼,這個按鈕顯示在'UIWebView'上。我遇到的問題是,當從觸摸事件調用同一個方法時,看起來通過觸摸事件訪問的webView完全獨立於按鈕的方式,也就是NSLog中所有webview子視圖的方法當從觸摸事件中調用時,不會列出按鈕添加的子視圖。我也嘗試在webview上添加一個空白的UIView作爲子視圖,但是這會從web視圖中去除互動。 – 2012-07-20 13:42:52

+0

@ mr-berna:我嘗試添加'[view bringSubviewToFront:touchView];'來添加子視圖的方法,但它沒有任何區別。 – 2012-07-20 13:56:10

0

userInteractionEnabled對於UIImageView的默認值是NO。您可能需要將其設置爲YES

+0

嗨,本篤,這沒有幫助,不幸的是。我遇到的唯一問題是UIWebView在直接從touchbegan等方法添加時不顯示子視圖。它可以工作,如果我使用外部輸入,如按鈕,但。 – 2012-07-20 13:39:36