如何在iOS中的圆形UIView中获取CGPoint?
我已经使用如何在iOS中的圆形UIView中获取CGPoint?,ios,swift,Ios,Swift,我已经使用UIBezierPath和setcorneradius设置了UIView角半径。检查以下代码: func roundCorners(cornerRadius: Double) { let path = UIBezierPath(roundedRect: self.puttingView.bounds, byRoundingCorners: [.topLeft, .topRight, .bottomLeft, .bottomRight], cornerRadii: CGS
UIBezierPath
和setcorneradius
设置了UIView
角半径。检查以下代码:
func roundCorners(cornerRadius: Double) {
let path = UIBezierPath(roundedRect: self.puttingView.bounds, byRoundingCorners: [.topLeft, .topRight, .bottomLeft, .bottomRight], cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.puttingView.bounds
maskLayer.path = path.cgPath
self.puttingView.layer.mask = maskLayer
}
现在,当我尝试单击外部ui视图
角半径时,我将ui动作识别器
设置为打开ui视图
,并且可以单击ui视图
。
所以我不想得到CGPoint
外角半径。试试这个:
@objc func tapped(_ gesture: UITapGestureRecognizer) {
let loc:CGPoint = gesture.location(in: gesture.view)
if let layer = puttingView.layer.mask, layer.bounds.contains(loc){
print("inside")
}
}
试试这个:
@objc func tapped(_ gesture: UITapGestureRecognizer) {
let loc:CGPoint = gesture.location(in: gesture.view)
if let layer = puttingView.layer.mask, layer.bounds.contains(loc){
print("inside")
}
}
您可能需要实现这一点
func point(inside point: CGPoint, with event: UIEvent?) -> Bool
您可能需要实现这一点
func point(inside point: CGPoint, with event: UIEvent?) -> Bool
首先,将您的
控制器
与UIgestureRecognitizerDelegate
相一致,并实施gestureRecognitizer(u:shouldReceive:)
方法
如果触摸屏位于遮罩内,则返回true
,否则返回false
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
let frame = self.puttingView.layer.mask?.bounds
let location = touch.location(in: self.puttingView)
if frame?.contains(location) ?? false {
return true
}
return false
}
不要忘记设置要添加到puttingView
的手势识别器的委托
,即
let gesture = UITapGestureRecognizer(target: self, action: #selector(viewTapped(_:)))
gesture.delegate = self //here......
self.puttingView.addGestureRecognizer(gesture)
首先,将您的控制器
与UIgestureRecognitizerDelegate
相一致,并实施gestureRecognitizer(u:shouldReceive:)
方法
如果触摸屏位于遮罩内,则返回true
,否则返回false
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
let frame = self.puttingView.layer.mask?.bounds
let location = touch.location(in: self.puttingView)
if frame?.contains(location) ?? false {
return true
}
return false
}
不要忘记设置要添加到puttingView
的手势识别器的委托
,即
let gesture = UITapGestureRecognizer(target: self, action: #selector(viewTapped(_:)))
gesture.delegate = self //here......
self.puttingView.addGestureRecognizer(gesture)
@iOS,请记录loc的值和层的边界,然后让我know@iOS,请记录loc的值和层的边界,并让我知道