可点击区域圆角矩形iOS [英] Clickable area rounded rectangle ios

查看:49
本文介绍了可点击区域圆角矩形iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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?

推荐答案

您可以通过覆盖

You can customize the clickable area by overriding pointInside(_: withEvent:) method. By default, the area is the same as bounds rectangle.

在这种情况下,您可以使用

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆