Skip to content

Commit 69da98c

Browse files
authored
Merge pull request #4 from public-value-tech/fix/triangle-optical-alignment-alternative-points
2 parents 9941026 + f7ac125 commit 69da98c

File tree

182 files changed

+78
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

182 files changed

+78
-18
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ jobs:
1111
steps:
1212
- uses: actions/checkout@v2
1313
- name: Select Xcode Version
14-
run: sudo xcode-select --switch /Applications/Xcode_13.2.1.app # (13C100)
14+
run: sudo xcode-select --switch /Applications/Xcode_14.1.app
1515
- name: Run tests
16-
run: xcodebuild test -scheme PlayButton -destination "OS=15.2,name=iPhone 11"
16+
run: xcodebuild test -scheme PlayButton -destination "OS=16.1,name=iPhone 11"

Sources/PlayButton.docc/PlayButton.md

Lines changed: 18 additions & 0 deletions

Sources/PlayButton/CGPath+Extensions.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension CGPath {
4040
let intersectionX = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))
4141
let intersectionY = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))
4242

43-
let arcCenter = CGPoint(x: floor(intersectionX), y: floor(intersectionY))
43+
let arcCenter = CGPoint(x: (intersectionX), y: (intersectionY))
4444
let startAngle = fromAngle - (.pi / 2.0)
4545

4646
let createCubicBezierFromArc: (_ angle: CGFloat, _ rotationOffset: CGFloat) -> (start: CGPoint, end: CGPoint, c1: CGPoint, c2: CGPoint) = { angle, rotationOffset in
@@ -117,6 +117,7 @@ extension CGPath {
117117
}
118118

119119
path.closeSubpath()
120+
120121
return (path, firstBezierEndPointXSpacing)
121122
}
122123

Sources/PlayButton/PlayButton.swift

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,6 @@ public class PlayButton: UIButton {
294294
private var shapeWidth: CGFloat { leftShape.bounds.width }
295295
private var shapeHeight: CGFloat { leftShape.bounds.height }
296296

297-
/// Visual corretion for "centering" the triangle
298-
private var triangleXOffset: CGFloat {
299-
let rightTipToBackgroundCircleDistance = (shapeWidth - triangleWidth) / 2.0
300-
let topLeftToBackgroundCircleDistance = shapeWidth / 2.0 - sqrt(pow(triangleWidth, 2) + pow(triangleWidth, 2)) / 2.0
301-
return (rightTipToBackgroundCircleDistance - topLeftToBackgroundCircleDistance) / 2.0
302-
}
303-
304297
/// This is the horizontal spacing between the end points of the triangle's first two bezier curves.
305298
/// It's used to calculate helper points along the straight lines of the bar and square paths.
306299
private var triangleBezierXSpacing: CGFloat = 0.0
@@ -357,7 +350,6 @@ public class PlayButton: UIButton {
357350
addGestureRecognizer(UIHoverGestureRecognizer(target: self, action: #selector(hover)))
358351
}
359352

360-
361353
if #available(macCatalyst 13.4, iOS 13.4, *) {
362354
isPointerInteractionEnabled = true
363355
pointerStyleProvider = { [weak self] _, _, _ in
@@ -481,19 +473,20 @@ public class PlayButton: UIButton {
481473
// ▶️
482474
/// The path of the triangle is only calculated when necessary and cached in ``currentTrianglePath``.
483475
private func updateTrianglePath() {
484-
let xOffset = triangleXOffset
476+
let r: CGFloat = 5.0 / 8.0 * triangleWidth
477+
let centerOffset = shapeWidth / 2.0
485478

486479
let topLeft = CGPoint(
487-
x: (shapeWidth - triangleWidth) / 2.0 + xOffset,
488-
y: (shapeHeight - triangleWidth) / 2.0
480+
x: -3.0 / 8.0 * triangleWidth + centerOffset,
481+
y: -triangleWidth / 2.0 + centerOffset
489482
)
490483
let middleRight = CGPoint(
491-
x: (shapeWidth + triangleWidth) / 2.0 + xOffset,
492-
y: shapeHeight / 2.0
484+
x: r + centerOffset,
485+
y: 0 + centerOffset
493486
)
494487
let bottomLeft = CGPoint(
495-
x: (shapeWidth - triangleWidth) / 2.0 + xOffset,
496-
y: (shapeHeight + triangleWidth) / 2.0
488+
x: topLeft.x,
489+
y: triangleWidth / 2.0 + centerOffset
497490
)
498491

499492
let (path, offset) = CGPath.createTriangleWithVertices(

Tests/PlayButtonTests/PlayButtonTests.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,35 @@ final class PlayButtonTests: XCTestCase {
279279
windowView.addSubview(button)
280280
assertSnapshot(matching: button, as: .image)
281281
}
282+
283+
func testTriangleOpticalAlignmentZeroCornerRadius() {
284+
let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 176, height: 176))
285+
button.triangleCornerRadius = 0.0
286+
button.playBufferingBackgroundColor = .systemBlue
287+
button.playBufferingTintColor = .white
288+
289+
let circle = CircleView(frame: CGRect(origin: .zero, size: CGSize(width: 102, height: 102)))
290+
button.addSubview(circle)
291+
circle.center = button.center
292+
293+
windowView.addSubview(button)
294+
295+
assertSnapshot(matching: button, as: .image, named: "triangle")
296+
}
297+
298+
func testTriangleOpticalAlignmentDefaultCornerRadius() {
299+
let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 176, height: 176))
300+
button.playBufferingBackgroundColor = .systemBlue
301+
button.playBufferingTintColor = .white
302+
303+
let circle = CircleView(frame: CGRect(origin: .zero, size: CGSize(width: 92, height: 92)))
304+
button.addSubview(circle)
305+
circle.center = button.center
306+
307+
windowView.addSubview(button)
308+
309+
assertSnapshot(matching: button, as: .image, named: "triangle")
310+
}
282311
}
283312

284313
private enum CompletionCondition {
@@ -378,3 +407,22 @@ extension CALayer {
378407
return rasterizedView
379408
}
380409
}
410+
411+
class CircleView: UIView {
412+
override init(frame: CGRect) {
413+
super.init(frame: frame)
414+
backgroundColor = .clear
415+
}
416+
417+
required init?(coder: NSCoder) {
418+
fatalError("init(coder:) has not been implemented")
419+
}
420+
421+
override func draw(_ rect: CGRect) {
422+
guard let context = UIGraphicsGetCurrentContext() else {return}
423+
424+
context.setLineWidth(1)
425+
context.setStrokeColor(UIColor.green.cgColor)
426+
context.strokeEllipse(in: rect.insetBy(dx: 1, dy: 1))
427+
}
428+
}

0 commit comments

Comments
 (0)