可点击区域圆形矩形ios
我正在尝试在iOS中绘制“圆形/椭圆形/椭圆形”自定义UIView(使用swift) 我使用一个子类绘制UIView,如下所示可点击区域圆形矩形ios,ios,objective-c,iphone,swift,Ios,Objective C,Iphone,Swift,我正在尝试在iOS中绘制“圆形/椭圆形/椭圆形”自定义UIView(使用swift) 我使用一个子类绘制UIView,如下所示 import Foundation import UIKit class CustomEllipse: UIView { override func drawRect(rect: CGRect) { var ovalPath = UIBezierPath(ovalInRect: rect) UIColor.grayColor().setFi
import Foundation
import UIKit
class CustomEllipse: UIView {
override func drawRect(rect: CGRect)
{
var ovalPath = UIBezierPath(ovalInRect: rect)
UIColor.grayColor().setFill()
ovalPath.fill()
}
}
这将产生与以下类似的输出
我现在需要为这个“CustomEllipse”定义一个可点击的区域
但是,当我为“CustomElipse”定义UITapgestureRecognitor时,默认情况下,上面看到的黑色角落是可单击的
有没有办法只让灰色椭圆可点击?您可以通过覆盖方法自定义可点击区域。 默认情况下,该区域与
边界
矩形相同
在这种情况下,可以使用UIBezierPath
中的方法。像这样:
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
}
}