UIGraphicsGetCurrentContext没有出现在UIView中 [英] UIGraphicsGetCurrentContext didn't appear inside UIView

查看:153
本文介绍了UIGraphicsGetCurrentContext没有出现在UIView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须绘制形状并检测用户的触摸是否在形状内,所以我定义了一个从UIView继承的自定义类,如下所示:

 类ShapeView:UIView {

覆盖init(frame:CGRect){
super.init(frame:frame)
}

是否需要init?(编码器aDecoder:NSCoder){
fatalError( init(coder :)尚未实现)
}

覆盖func draw(_ rect :CGRect){
drawShape()
}

func drawShape(){

guard let ctx = UIGraphicsGetCurrentContext()else {return}

让零= CGPoint(x:frame.midX,y:frame.midY)

让大小:CGFloat = 50

ctx.beginPath()
ctx.move(至:.zero)
ctx.addLine(至:CGPoint(x:-size,y:-size))
ctx.addLine(至:CGPoint(x: zero.x,y:(-size * 2)))
ctx.addLine(to:CGPoint(x:size,y:-size))
ctx.closePath()
ctx .setFillColor(UIColor.red.cgColor)
ctx.fillPath()
}

此代码应绘制如下形状



 覆盖功能点(内部点:CGPoint,事件:UIEvent?)-> Bool {
let path = UIBezierPath(ovalIn:self.frame)
返回path.contains(point)
}
}

,然后在viewController中,我编写了这段代码,将这个自定义视图添加到UIViewController中:

 可变形状:ShapeView? 

覆盖功能funD viewDidLoad(){
super.viewDidLoad()

let x = view.frame.midX
let y = view.frame。 midY

self.shape = ShapeView(frame:CGRect(x:x,y:y,width:100,height:100))
shape?.backgroundColor = .green
view.addSubview(shape!)
}

我知道形状在查看此方法后的视图: b $ b let location = touches.first!.location(in:self.view)
if(shape?.point(inside:location,with:event))! {
print( inside view)
} else {
print( outside view)
}

,但整体结果是这张图片,它只具有一种颜色的绿色视图,并且有一个子视图,但没有颜色出现





那么这段代码怎么了?

解决方案

您应使用替代函式绘制(_ rect的 rect 的大小CGRect)进行计算。



我也认为您的计算不正确,尚未测试,但我认为应该是这样的:

  ctx.move(至:CGPoint(x:矩形宽度/ 2,y:0))
ctx.addLine(至:CGPoint(x:矩形宽度) ,y:rect.height / 2))
ctx.addLine(to:CGPoint(x:rect.width / 2,y:rect.height))
ctx.addLine(to:CGPoint(x :0,y:rect.height / 2))


I have to draw a shape and detect if user touches are inside the shape or not, so I defined a custom class inherit from UIView as follow :

class ShapeView: UIView {

  override init(frame: CGRect) {
    super.init(frame: frame)
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  override func draw(_ rect: CGRect) {
    drawShape()
  }

  func drawShape() {

    guard let ctx = UIGraphicsGetCurrentContext() else { return }

    let zero = CGPoint(x: frame.midX, y: frame.midY)

    let size: CGFloat = 50

    ctx.beginPath()
    ctx.move(to: .zero)
    ctx.addLine(to: CGPoint(x: -size, y: -size))
    ctx.addLine(to: CGPoint(x: zero.x , y: (-size * 2)))
    ctx.addLine(to: CGPoint(x: size, y: -size))
    ctx.closePath()
    ctx.setFillColor(UIColor.red.cgColor)
    ctx.fillPath()
  }

this code should draw shape like this

  override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    let path = UIBezierPath(ovalIn: self.frame)
    return path.contains(point)
  }
}

and in the viewController I wrote this code to add this custom view to UIViewController :

var shape: ShapeView?

override func viewDidLoad() {
    super.viewDidLoad()

    let x = view.frame.midX
    let y = view.frame.midY

    self.shape = ShapeView(frame: CGRect(x: x, y: y, width: 100, height: 100))
    shape?.backgroundColor = .green
    view.addSubview(shape!)
 }

I knew that the shape is inside the view after wrote this method :

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let location = touches.first!.location(in: self.view)
    if (shape?.point(inside: location, with: event))! {
        print("inside view")
    } else {
        print("outside view")  
    }

but overall result was this image it just has one color of the view as green and there's a subview but no color appeared

So what's wrong with this code ?

解决方案

You should use the size of the rect of the override func draw(_ rect: CGRect) to do your calculations on.

I also think your calculations are incorrect, haven't tested it but I think it should be something like this:

ctx.move(to: CGPoint(x: rect.width / 2, y: 0))
ctx.addLine(to: CGPoint(x: rect.width, y: rect.height / 2))
ctx.addLine(to: CGPoint(x: rect.width / 2 , y: rect.height))
ctx.addLine(to: CGPoint(x: 0, y: rect.height / 2))

这篇关于UIGraphicsGetCurrentContext没有出现在UIView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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