首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CAShapeLayer攻丝

CAShapeLayer攻丝
EN

Stack Overflow用户
提问于 2015-04-17 12:41:19
回答 1查看 598关注 0票数 1

我正在使用CAShapeLayer在视图中绘制一些形状。我做了一个功能来处理这件事。因此,例如:

viewDidLoad()

代码语言:javascript
复制
drawQuad(UIColor(red: 0.600, green: 0.224, blue: 0.341, alpha: 1.00), firstPoint: CGPointMake(screenWidth * -0.159, screenHeight * 0.249), secondPoint: CGPointMake(screenWidth * 0.65, screenHeight * 0.249), thirdPoint: CGPointMake(screenWidth * 1.01, screenHeight * 0.50), fourthPoint: CGPointMake(screenWidth * 0.399, screenHeight * 0.50))

drawQuad(...)

代码语言:javascript
复制
func drawQuad(fillColor: UIColor, firstPoint: CGPoint, secondPoint: CGPoint, thirdPoint: CGPoint, fourthPoint: CGPoint) {
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height

    let shape = CAShapeLayer()
    containerLayer.addSublayer(shape)
    shape.lineJoin = kCALineJoinMiter
    shape.fillColor = fillColor.CGColor

    let path = UIBezierPath()
    path.moveToPoint(firstPoint)
    path.addLineToPoint(secondPoint)
    path.addLineToPoint(thirdPoint)
    path.addLineToPoint(fourthPoint)

    path.closePath()
    shape.path = path.CGPath
}

我被困在试图添加点击识别到每一个形状。我试图做的是创建一个容器CAShapeLayer,它将每个形状保持为子层。如果您查看上面的函数,您将在第6行看到这一点。然后,在viewDidLoad()中,我将最后一个容器层添加到视图中。

这样做的目的是能够这样做一个hitTest

代码语言:javascript
复制
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    let location = touch.locationInView(self.view)

    for layer in containerLayer.sublayers {
        if let hitLayer = layer.hitTest(location) {
            println("Tapped")
        }
    }
}

不幸的是,这似乎不起作用。有人知道我犯了什么错吗?还是我走错路了?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-17 13:15:31

尝试使用CGPathContainsPoint检查触摸的位置是否包含在您层的路径中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29699534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档