可点击区域圆角矩形ios
我试图在iOS中绘制一个’圆形/椭圆/椭圆’自定义UIView(使用swift)
我正在使用子类绘制UIView,如下所示
import Foundation import UIKit class CustomEllipse: UIView { override func drawRect(rect: CGRect) { var ovalPath = UIBezierPath(ovalInRect: rect) UIColor.grayColor().setFill() ovalPath.fill() } }
这产生类似于以下的输出
我现在需要为这个’CustomEllipse’定义一个可点击区域。
但是,当我为’CustomElipse’定义UITapGestureRecognizer时,默认情况下可以单击上面看到的黑角。
有没有办法让灰色椭圆可点击?
您可以通过重写pointInside(_: withEvent:)
方法来自定义可单击区域。 默认情况下,该区域与bounds
矩形相同。
在这种情况下,您可以在UIBezierPath
使用containsPoint()
方法。 喜欢这个:
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 } }