【问题标题】:CALayer::hitTest failing in landscape orientationCALayer::hitTest 在横向上失败
【发布时间】:2013-08-29 15:58:59
【问题描述】:

我的视图控制器只支持横向。我在主视图中添加了一个白色视图(whiteView - xib 中的 IBOutlet),并在 whiteview 中添加了一个蓝色 CALayer(blueLayer - 类中的属性)。当我在此视图控制器上运行 hitTest 时,它在应用程序窗口右侧的触摸返回不正确的结果,左侧一切正常。即使当前方向是横向,应用程序也好像在纵向模式下运行。在纵向模式下一切正常。

下面是代码。在横向中,每次向右触摸都会返回“在主视图中触摸”。我需要做什么才能使其在横向中正常工作?

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.blueLayer = [[CALayer alloc] init];
    CGRect frame = self.whiteView.frame;
    self.blueLayer.frame = CGRectMake(20.0, 20.0, frame.size.width-40.0, frame.size.height - 40.0f);
    self.blueLayer.backgroundColor = [UIColor blueColor].CGColor;
    [self.whiteView.layer addSublayer:self.blueLayer];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"Touch in main view" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    CALayer *touchedLayer = [self.view.layer hitTest:touchPoint];

    if(touchedLayer == self.blueLayer){
        alertView.message = @"Touch in blue view";
    }
    else if (touchedLayer == self.whiteView.layer){
        alertView.message = @"Touch in white view";
    }
    else
        alertView.message = @"Touch in main view";

    [alertView show];
}

【问题讨论】:

    标签: iphone ios calayer hittest


    【解决方案1】:

    它与方向无关。 根据苹果文档,“hitTest 返回图层最远的后代”。 兄弟姐妹按从上到下的顺序进行搜索。接触点在接收者超层的坐标系中。 因此,在您获得 touchPoint 后立即添加此行。即

    CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
    touchPoint = [self.view.layer convertPoint:touchPoint toLayer:self.view.layer.superlayer];
    ...........
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-03
      相关资源
      最近更新 更多