核心图形(iPhone)-是否可以知道CGPath是否关闭? [英] Core-Graphics (iPhone) -Can we know whether a CGPath is closed?

查看:49
本文介绍了核心图形(iPhone)-是否可以知道CGPath是否关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何变通办法来知道 CGpath (使用坐标数组创建)在哪里被关闭



我无法在标题 CGPath 中找到任何合适的方法。



<我想跟踪用户跟踪的路径。如果它是一个闭环,那么我希望从上下文中提取该部分。诸如掩盖或剪切上下文之类的东西,但应该是用户跟踪的剪切。



谢谢!

解决方案

实际上,路径可以包含多个子路径。当您移动路径的当前点而不连接两个点时,会创建一个新的子路径。要关闭路径,实际上必须关闭其所有子路径。

 扩展CGPath { 

///请注意,在当前
///的起始路径上添加一条直线或曲线不会将其关闭。这可以通过用$ .butt的线帽描画
///路径来可视化。
public var isClosed:Bool {
varcompletedSubpathsWereClosed = true
var currentSubpathIsClosed = true

self.applyWithBlock({
中的指针让element =指针。 pointee

开关element.type {
case .moveToPoint:
if!currentSubpathIsClosed {
completeSubpathsWereClosed = false
}

currentSubpathIsClosed = true
case .addLineToPoint,.addQuadCurveToPoint,.addCurveToPoint:
currentSubpathIsClosed = false
case .closeSubpath:
currentSubpathIsClosed = true
}
})

返回完成SubSubssWereClosed&& currentSubpathIsClosed
}
}


Is there any workaround to know where a CGpath (created using an array of co-ordinates) is closed.

I was not able to find any suitable method in the header CGPath.

I wish to track the path traced by user .If its a closed loop, then i wish to extract that part from the context. Something like masking or clipping the context but it should be user tracked clipping.

Thanks!

解决方案

In fact, a path can consist of multiple subpaths. A new subpath is created when you move the path's current point without connecting the two points. For the path to be closed, all its subpaths must in fact be closed.

extension CGPath {

    /// Note that adding a line or curve to the start point of the current
    /// subpath does not close it. This can be visualized by stroking such a
    /// path with a line cap of `.butt`.
    public var isClosed: Bool {
        var completedSubpathsWereClosed = true
        var currentSubpathIsClosed = true

        self.applyWithBlock({ pointer in
            let element = pointer.pointee

            switch element.type {
            case .moveToPoint:
                if !currentSubpathIsClosed {
                    completedSubpathsWereClosed = false
                }

                currentSubpathIsClosed = true
            case .addLineToPoint, .addQuadCurveToPoint, .addCurveToPoint:
                currentSubpathIsClosed = false
            case .closeSubpath:
                currentSubpathIsClosed = true
            }
        })

        return completedSubpathsWereClosed && currentSubpathIsClosed
    }
}

这篇关于核心图形(iPhone)-是否可以知道CGPath是否关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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