【问题标题】:Clickable area rounded rectangle ios可点击区域圆角矩形ios
【发布时间】:2014-11-26 12:28:20
【问题描述】:

我正在尝试在 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 时,上面看到的黑角默认情况下是可点击的。

有没有办法让灰色椭圆可以点击?

【问题讨论】:

    标签: ios objective-c iphone swift


    【解决方案1】:

    您可以通过覆盖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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 2010-10-26
      • 2013-11-23
      • 1970-01-01
      • 2018-10-30
      相关资源
      最近更新 更多