2011-12-29 34 views
3

我正在使用適用於iOS的Mixare AR SDK,我需要解決一些發生的bug,其中一個是在點擊POI的視圖時顯示POI的信息。UIView hitTest:withEvent:and pointInside:withEvent

前奏:

Mixare具有被放置,MarkerView視圖在屏幕上移動來地理定位所述的POI MarkerView視圖中重疊的UIView而且每一個都有兩個子視圖,一個的UIImageView和的UILabel。

問題:

現在,例如,有在屏幕3級可見的POI,所以有3 MarkerView爲覆蓋子視圖。如果您觸摸疊加層中的任何位置,則會顯示與隨機POI相關的信息視圖。

期望:

我想的是,相關的POI信息顯示,只有當用戶點擊一個MarkerView

讓我們的工作。我已經看到MarkerView從UIView的繼承並實現則hitTest:withEvent

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { 

    viewTouched = (MarkerView*)[super hitTest:point withEvent:event]; 
    return self; 

} 

我把一個斷點,則hitTest爲每個可見MarkerView調用一次,但loadedView總是爲空,所以我不能用它工作,所以我試圖檢查命中點是實現pointInside的MarkerView框架內:withEvent:方法通過這種方式

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    NSLog(@"ClassName: %@", [[self class] description]); 
    NSLog(@"Point Inside: %f, %f", point.x, point.y); 
    NSLog(@"Frame x: %f y: %f widht:%f height:%f", self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height); 

    if (CGRectContainsPoint(self.frame, point)) 
     return YES; 
    else 
     return NO; 


    return YES; 
} 

但這個函數總是返回NO,甚至當我觸摸MarkerView。當我檢查日誌時,我發現X和Y點值有時有負值,視圖的寬度和高度非常小,0.00022或類似的,而不是100 x 150,我在初始化時設置了MarkerView框架。

在這裏,您是我的日誌摘錄,您可以在其中看到類名,點和MarkerView框架值。

ClassName: MarkerView 
2011-12-29 13:20:32.679 paisromanico[2996:707] Point Inside: 105.224899, 49.049023 
2011-12-29 13:20:32.683 paisromanico[2996:707] Frame x: 187.568573 y: 245.735138 widht:0.021862 height:0.016427 

我很迷茫,因此任何幫助都會受到歡迎。在此先感謝您的幫助,並提供我對此磚:(

編輯惋惜:

最後我發現,這個問題是不是則hitTest:withEvent:方法或pointInside:withEvent,問題是與CGTransform適用於MarkerView縮放基於distande和旋轉視圖,如果我評論任何代碼相關的,Mixare AR SDK工作正常,我的意思是,信息視圖顯示正確,如果你觸摸標記,並沒有

所以,目前爲止,我還沒有解決這個問題,但我申請了一個補丁,去除了AugmentedViewController.m中的CGTransform相關代碼class - (void)upda teLocations:(NSTimer *)定時器功能

- (void)updateLocations:(NSTimer *)timer { 
    //update locations! 

    if (!ar_coordinateViews || ar_coordinateViews.count == 0) { 
     return; 
    } 

    int index = 0; 
    NSMutableArray * radarPointValues= [[NSMutableArray alloc]initWithCapacity:[ar_coordinates count]]; 

    for (PoiItem *item in ar_coordinates) { 

     MarkerView *viewToDraw = [ar_coordinateViews objectAtIndex:index]; 
     viewToDraw.tag = index; 

     if ([self viewportContainsCoordinate:item]) { 

      CGPoint loc = [self pointInView:ar_overlayView forCoordinate:item]; 
      CGFloat scaleFactor = 1.5; 

      if (self.scaleViewsBasedOnDistance) { 
       scaleFactor = 1.0 - self.minimumScaleFactor * (item.radialDistance/self.maximumScaleDistance); 
      } 

      float width = viewToDraw.bounds.size.width ;//* scaleFactor; 
      float height = viewToDraw.bounds.size.height; // * scaleFactor; 

      viewToDraw.frame = CGRectMake(loc.x - width/2.0, loc.y-height/2.0, width, height); 

      /* 
      CATransform3D transform = CATransform3DIdentity; 

      //set the scale if it needs it. 
      if (self.scaleViewsBasedOnDistance) { 
       //scale the perspective transform if we have one. 
       transform = CATransform3DScale(transform, scaleFactor, scaleFactor, scaleFactor); 
      } 

      if (self.rotateViewsBasedOnPerspective) { 
       transform.m34 = 1.0/300.0; 

       double itemAzimuth = item.azimuth; 
       double centerAzimuth = self.centerCoordinate.azimuth; 
       if (itemAzimuth - centerAzimuth > M_PI) centerAzimuth += 2*M_PI; 
       if (itemAzimuth - centerAzimuth < -M_PI) itemAzimuth += 2*M_PI; 

       double angleDifference = itemAzimuth - centerAzimuth; 
       transform = CATransform3DRotate(transform, self.maximumRotationAngle * angleDifference/(VIEWPORT_HEIGHT_RADIANS/2.0) , 0, 1, 0); 
      }    

      viewToDraw.layer.transform = transform; 

      */ 
      //if we don't have a superview, set it up. 
      if (!(viewToDraw.superview)) { 
       [ar_overlayView addSubview:viewToDraw]; 
       [ar_overlayView sendSubviewToBack:viewToDraw]; 
      } 
     } else { 
      [viewToDraw removeFromSuperview]; 
      viewToDraw.transform = CGAffineTransformIdentity; 
     } 
     [radarPointValues addObject:item]; 
     index++; 
    } 
    float radius = [[[NSUserDefaults standardUserDefaults] objectForKey:@"radius"] floatValue]; 
    if(radius <= 0 || radius > 100){ 
     radius = 5.0; 
    } 

    radarView.pois = radarPointValues; 
    radarView.radius = radius; 
    [radarView setNeedsDisplay]; 
    [radarPointValues release]; 
} 

任何CoreGrapics或UI專家都可以給我們關於這個問題的觀點?

+0

我建議你看看[FAQ](http://stackoverflow.com/faq)。 – 2011-12-29 14:19:54

回答

1

您應該嘗試的HitTest爲附:

if ([self pointInside:point withEvent:event]) { 
    // do something 
} 

我建議你添加命中測試的上海華,做在markerView小號

母公司的命中測試以下
if ([markerView pointInside:point withEvent:event]) { 
    // extract the tag and show the relevant info 
} 

希望這可以幫助

+0

我試圖完成,但MarkerView座標不對應於scrrens座標。 – Rotten 2012-01-03 07:42:47

+0

嘗試在位置(0,0)中放置標記視圖並查看它是否有效。如果我沒有記錯左上角的最好的作品。如果這適用於你,你必須在調整markerView的CGRect後測試pointInside – orrko 2012-01-04 09:10:06