问题描述
我正在尝试在iOS中绘制圆形/椭圆形/椭圆形"自定义UIView(使用Swift)
I am attempting to draw a 'circular/oval/ellipse' custom UIView in iOS (using swift)
我正在使用如下子类绘制UIView
I am drawing the UIView using a subclass as follows
import Foundation
import UIKit
class CustomEllipse: UIView {
override func drawRect(rect: CGRect)
{
var ovalPath = UIBezierPath(ovalInRect: rect)
UIColor.grayColor().setFill()
ovalPath.fill()
}
}
这将产生类似于以下内容的输出
This produces output similar to the following
我现在需要为此"CustomEllipse"定义一个可点击区域.
I now need to define a clickable area for this 'CustomEllipse'.
但是,当我为"CustomElipse"定义UITapGestureRecognizer时,默认情况下可以单击上面看到的黑角.
However, when I define a UITapGestureRecognizer for the 'CustomElipse', the black corners seen above are clickable by default.
有没有一种方法可以使灰色椭圆形变为可点击状态?
Is there a way to make just the grey ellipse clickable?
推荐答案
您可以通过覆盖 pointInside(_:withEvent:)
方法.默认情况下,该区域与 bounds
矩形相同.
You can customize the clickable area by overriding pointInside(_: withEvent:)
method.By default, the area is the same as bounds
rectangle.
在这种情况下,您可以使用 UIBezierPath
中的 containsPoint()
方法.像这样:
In this case, you can use containsPoint()
method in UIBezierPath
. like this:
class CustomEllipse: UIView {
var ovalPath:UIBezierPath?
override func drawRect(rect: CGRect) {
ovalPath = UIBezierPath(ovalInRect: rect)
UIColor.grayColor().setFill()
ovalPath!.fill()
}
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
return ovalPath?.containsPoint(point) == true
}
}
这篇关于可点击区域圆角矩形iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!