Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow hit test layers inside SVGLayer #99 #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions SwiftSVG.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
06B8771A214F03AA00CB0628 /* CAShapeLayerWithHitTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06B87718214F03AA00CB0628 /* CAShapeLayerWithHitTest.swift */; };
06B8771B214F03AA00CB0628 /* CALayerWithChildHitTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06B87719214F03AA00CB0628 /* CALayerWithChildHitTest.swift */; };
174FF06D1EF10691003F3DBE /* PathDLexer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 174FF06C1EF10691003F3DBE /* PathDLexer.swift */; };
175093C11E6C669F00EF1853 /* SVGParserSupportedElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 175093C01E6C669F00EF1853 /* SVGParserSupportedElements.swift */; };
175093C31E6C6C2100EF1853 /* SVGElement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 175093C21E6C6C2100EF1853 /* SVGElement.swift */; };
Expand Down Expand Up @@ -144,6 +146,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
06B87718214F03AA00CB0628 /* CAShapeLayerWithHitTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CAShapeLayerWithHitTest.swift; sourceTree = "<group>"; };
06B87719214F03AA00CB0628 /* CALayerWithChildHitTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CALayerWithChildHitTest.swift; sourceTree = "<group>"; };
174FF06C1EF10691003F3DBE /* PathDLexer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PathDLexer.swift; sourceTree = "<group>"; };
175093C01E6C669F00EF1853 /* SVGParserSupportedElements.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SVGParserSupportedElements.swift; sourceTree = "<group>"; };
175093C21E6C6C2100EF1853 /* SVGElement.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SVGElement.swift; path = SVG/Elements/SVGElement.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -366,6 +370,8 @@
38048F321F24210F00179C95 /* CALayer+SVG.swift */,
38048F331F24210F00179C95 /* CAShapeLayer+SVG.swift */,
38048F341F24210F00179C95 /* SVGLayer.swift */,
06B87719214F03AA00CB0628 /* CALayerWithChildHitTest.swift */,
06B87718214F03AA00CB0628 /* CAShapeLayerWithHitTest.swift */,
38048F351F24210F00179C95 /* SVGView.swift */,
38048F361F24210F00179C95 /* UIBezierPath+SVG.swift */,
38048F371F24210F00179C95 /* UIView+SVG.swift */,
Expand Down Expand Up @@ -702,10 +708,12 @@
39B8D8A71B3C32DD009DAF60 /* String+Subscript.swift in Sources */,
380FED791F07F48500EDB255 /* DispatchQueue+Extensions.swift in Sources */,
38048F381F24210F00179C95 /* CALayer+SVG.swift in Sources */,
06B8771B214F03AA00CB0628 /* CALayerWithChildHitTest.swift in Sources */,
17CCE7381EE666D6007FA5AD /* Scalar+FromByteArray.swift in Sources */,
39B8D8A91B3C32DD009DAF60 /* Stack.swift in Sources */,
38048F391F24210F00179C95 /* CAShapeLayer+SVG.swift in Sources */,
175093CC1E6C773600EF1853 /* SVGRootElement.swift in Sources */,
06B8771A214F03AA00CB0628 /* CAShapeLayerWithHitTest.swift in Sources */,
176BC9BC1EE4AE2700F7B54C /* Dictionary+Add.swift in Sources */,
38048F3D1F24210F00179C95 /* UIView+SVG.swift in Sources */,
176BC9B61EE498C700F7B54C /* SVGRectangle.swift in Sources */,
Expand Down
52 changes: 52 additions & 0 deletions SwiftSVG/SVG Extensions/CALayerWithChildHitTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// SVGLayer.swift
// SwiftSVG
//
//
// Copyright (c) 2017 Michael Choe
// http://www.github.com/mchoe
// http://www.straussmade.com/
// http://www.twitter.com/_mchoe
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.



import Foundation
import QuartzCore

/*!
* Overrides Apple's CALayer purely to change one method, so that hit-testing
* is done by checking whether the hit point lies:
*
* "inside ANY of my child sub-layers (some of which have over-ridden hit-testing too)"
*/

open class CALayerWithChildHitTest: CALayer {

override open func contains(_ p: CGPoint) -> Bool {
for subLayer in sublayers ?? [] {
let pointInSubLayer = convert(p, to: subLayer)
if subLayer.contains(pointInSubLayer) {
return true
}
}
return false
}
}
43 changes: 43 additions & 0 deletions SwiftSVG/SVG Extensions/CAShapeLayerWithHitTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// SVGLayer.swift
// SwiftSVG
//
//
// Copyright (c) 2017 Michael Choe
// http://www.github.com/mchoe
// http://www.straussmade.com/
// http://www.twitter.com/_mchoe
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.



import Foundation
import QuartzCore

/**
A `CAShapeLayer` subclass that allows you to do perfect "containsPoint" calculations via Apple's API calls
*/

open class CAShapeLayerWithHitTest: CAShapeLayer {

override open func contains(_ p: CGPoint) -> Bool {
return path?.contains(p) == true
}
}
2 changes: 1 addition & 1 deletion SwiftSVG/SVG/Elements/SVGCircle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class SVGCircle: SVGShapeElement {
internal var circleRadius: CGFloat = 0

/// :nodoc:
internal var svgLayer = CAShapeLayer()
internal var svgLayer = CAShapeLayerWithHitTest()

/// :nodoc:
internal var supportedAttributes: [String : (String) -> ()] = [:]
Expand Down
2 changes: 1 addition & 1 deletion SwiftSVG/SVG/Elements/SVGContainerElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public protocol SVGContainerElement: SVGElement, DelaysApplyingAttributes, Filla
/**
The layer that stores all the SVG sublayers
*/
var containerLayer: CALayer { get set }
var containerLayer: CALayerWithChildHitTest { get set }
}


2 changes: 1 addition & 1 deletion SwiftSVG/SVG/Elements/SVGEllipse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class SVGEllipse: SVGShapeElement {
internal var yRadius: CGFloat = 0

/// :nodoc:
internal var svgLayer = CAShapeLayer()
internal var svgLayer = CAShapeLayerWithHitTest()

/// :nodoc:
internal var supportedAttributes: [String : (String) -> ()] = [:]
Expand Down
2 changes: 1 addition & 1 deletion SwiftSVG/SVG/Elements/SVGGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final class SVGGroup: SVGContainerElement {
internal var delayedAttributes = [String : String]()

/// A `CALayer` that will hold all sublayers of the `SVGGroup`
internal var containerLayer = CALayer()
internal var containerLayer = CALayerWithChildHitTest()

/// :nodoc:
internal var supportedAttributes = [String : (String) -> ()]()
Expand Down
2 changes: 1 addition & 1 deletion SwiftSVG/SVG/Elements/SVGLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class SVGLine: SVGShapeElement {
internal var start = CGPoint.zero

/// :nodoc:
internal var svgLayer = CAShapeLayer()
internal var svgLayer = CAShapeLayerWithHitTest()

/// :nodoc:
internal var supportedAttributes: [String : (String) -> ()] = [:]
Expand Down
2 changes: 1 addition & 1 deletion SwiftSVG/SVG/Elements/SVGPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final class SVGPath: SVGShapeElement, ParsesAsynchronously, DelaysApplyingAttrib
internal var supportedAttributes = [String : (String) -> ()]()

/// :nodoc:
internal var svgLayer = CAShapeLayer()
internal var svgLayer = CAShapeLayerWithHitTest()

/// :nodoc:
internal init() { }
Expand Down
2 changes: 1 addition & 1 deletion SwiftSVG/SVG/Elements/SVGPolygon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct SVGPolygon: SVGShapeElement {
internal var supportedAttributes: [String : (String) -> ()] = [:]

/// :nodoc:
internal var svgLayer = CAShapeLayer()
internal var svgLayer = CAShapeLayerWithHitTest()

/**
Function that parses a coordinate string and creates a polygon path
Expand Down
2 changes: 1 addition & 1 deletion SwiftSVG/SVG/Elements/SVGPolyline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct SVGPolyline: SVGShapeElement {
internal var supportedAttributes: [String : (String) -> ()] = [:]

/// :nodoc:
internal var svgLayer = CAShapeLayer()
internal var svgLayer = CAShapeLayerWithHitTest()

/**
Parses a coordinate string and creates a new polyline based on them
Expand Down
2 changes: 1 addition & 1 deletion SwiftSVG/SVG/Elements/SVGRectangle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class SVGRectangle: SVGShapeElement {
internal var rectangleRect = CGRect()

/// :nodoc:
internal var svgLayer = CAShapeLayer()
internal var svgLayer = CAShapeLayerWithHitTest()

/// :nodoc:
internal var supportedAttributes: [String : (String) -> ()] = [:]
Expand Down
2 changes: 1 addition & 1 deletion SwiftSVG/SVG/Elements/SVGRootElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct SVGRootElement: SVGContainerElement {
internal var delayedAttributes = [String : String]()

// :nodoc:
internal var containerLayer = CALayer()
internal var containerLayer = CALayerWithChildHitTest()

// :nodoc:
internal var supportedAttributes = [String : (String) -> ()]()
Expand Down
2 changes: 1 addition & 1 deletion SwiftSVG/SVG/Elements/SVGShapeElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public protocol SVGShapeElement: SVGElement, Fillable, Strokable, Transformable,
/**
The `CAShapeLayer` that can draw the path data.
*/
var svgLayer: CAShapeLayer { get set }
var svgLayer: CAShapeLayerWithHitTest { get set }
}

extension SVGShapeElement {
Expand Down