如何解决Swift - 如何检查 CAShapeLayer 是否与按钮UIView相交?
就像标题所述,我无法检查按钮是否与 CAShapeLayer 相交。现在我有一个通过 UIPanGestureRecognizer 移动的按钮。当它放开时,我想检查它是否在我拥有的这个轮子上的某种颜色之上: Simulator View
let path1 = UIBezierPath()
path1.addArc(withCenter: center,radius: 50,startAngle: CGFloat.pi/4 + 0.1,endAngle: CGFloat.pi*3/4 - 0.1,clockwise: true)
path1.addArc(withCenter: center,radius: 125,startAngle: CGFloat.pi*3/4 - 0.05,endAngle: CGFloat.pi/4 + 0.05,clockwise: false)
blueCircle.path = path1.cgPath
blueCircle.fillColor = UIColor(red: 0,green: 143/255,blue: 208/255,alpha: 1).cgColor
self.view.layer.addSublayer(blueCircle)
其中 blueCircle 是 CAShapeLayer。
我有代码来检查我的“识别器”(UIPanGestureRecognizer)是否在发布时重叠,看起来像这样:
if recognizer.view!.frame.intersects(blueCircle.frame) {
print("Blue overlap")
}
但我怀疑它总是返回 false,因为它们不在同一个“层”中。我尝试了多种方法,包括使用边界而不是框架以及尝试使用手势识别器的中心进行转换,但它似乎无法正常工作。
解决方法
您需要检查与主视图(self.view)的交集
你也可以试试这个
- (BOOL)checkViewIntersectsWithAnotherView:(UIView*)selectedView {
NSArray *subViewsInView = [self.view subviews];// I assume self is a subclass
// of UIViewController but the view can be
//any UIView that'd act as a container
//for all other views.
for(UIView *theView in subViewsInView) {
if (![selectedView isEqual:theView])
if(CGRectIntersectsRect(selectedView.frame,theView.frame))
return YES;
}
return NO;
}
,
因此,对于遇到此问题的任何人,我设法找到了解决方案。似乎您可以在 CAShapeLayers 上调用 frame,但是当我检查打印输出时,它给了我“(0,0)”。我最终用
替换了调用frame的代码if self.view.intersects((blueCircle.path?.boundingBox)!) {
print("Blue overlap")
}
所以我正在寻找路径,这完美地工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。