iphone - if([labelView.layer containsPoint :touchPoint]) not working in Xcode

标签 iphone objective-c ios cocoa-touch

任何人都可以告诉我,我在拖动和释放鼠标时使用 UIPangesture 获取了可拖动标签,我正在使用

找到触摸点
CGPoint touchPoint = [panGesture locationInView:self.view];

现在我想检查这个标签被拖到哪个标签上。为此我正在使用这段代码

for(UILabel *labelView in self.view.subviews){
    if ([labelView isMemberOfClass:[UILabel class]]) {

        NSLog(@"%@",NSStringFromCGPoint(touchPoint ));

        NSLog(@"%@",NSStringFromCGRect(labelView.layer.frame));

        if([labelView.layer containsPoint:touchPoint]){

            int i=[[labelView.subviews objectAtIndex:0] tag];

            NSLog(@">>>> %d",i);
        }
    }
}

最佳答案

传递给手势处理程序方法的 panGesture 具有属性“view”,它指向您使用识别器设置的 UILabel 对象

编辑: *-- 上面的答案是错误的。感谢 Jacky 指出我的意见 --*

self.view 的 hitTest:withEvent: 方法将返回该点的 View 。将 nil 传递给 withEvent:

如果 self.view 也有 UILabel 以外的 subview ,并且您想避免返回其中一个,请调用

bool CGRectContainsPoint (
   CGRect rect,
   CGPoint point
);

对于每个标签。传递框架为矩形,接触点为点

for(UILabel *labelView in self.view.subviews){
    if ([labelView isMemberOfClass:[UILabel class]]) {
        if(CGRectContainsPoint(labelView.frame,touchPoint)){
            int i=[[labelView.subviews objectAtIndex:0] tag];
            NSLog(@">>>> %d",i);
        }
    }
}

编辑: .layer 的 containsPoint: 不起作用的原因:

thePoint A point in the receiver’s coordinate system.

在您的代码中,touchPoint 位于self.view 的坐标系中,该坐标系不同于任何标签的坐标系

关于iphone - if([labelView.layer containsPoint :touchPoint]) not working in Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12953108/

相关文章:

iphone - 在 Settings.app 中隐藏应用程序特定的设置

iphone - 将图像数据转换为 vcard 的二进制文本

iphone - 在 Objective-C 中取消 UILocalnotification 的步骤

ios - 如何扩展算术运算符以支持可选值 - Swift

iphone - 如何检测 iPhone 上的环境声级?

ios - 我需要在 iOS7+ 应用程序中实现 didFailToReceiveAdWithError 吗?

objective-c - 如何在 OSX 中使用 Objective C 获取所有已安装的应用程序

iphone - iOS - UINavigationBar - 平面样式按钮

ios - 如何获得已经被解雇的 UILocalNotification

ios - 使用 NSPredicate 过滤对象和键(需要拆分)