任何人都可以告诉我,我在拖动和释放鼠标时使用 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/