diff --git a/Sources/PlayButton/PlayButton.swift b/Sources/PlayButton/PlayButton.swift index 980d4e8..9d4c78f 100644 --- a/Sources/PlayButton/PlayButton.swift +++ b/Sources/PlayButton/PlayButton.swift @@ -179,8 +179,25 @@ public class PlayButton: UIButton { yScale = scale } - let xTranslate = (xScale - 1) * scaleLayer.bounds.width / 2 - let yTranslate = (yScale - 1) * scaleLayer.bounds.height / 2 + var xTranslate = (xScale - 1.0) * scaleLayer.bounds.height / 2.0 + var yTranslate = (yScale - 1.0) * scaleLayer.bounds.width / 2.0 + + switch contentMode { + case .scaleToFill: + break + case .scaleAspectFill: + if bounds.width < bounds.height { + xTranslate += (bounds.width - xScale * scaleLayer.bounds.width) / 2.0 + } else if bounds.width > bounds.height { + yTranslate += (bounds.height - yScale * scaleLayer.bounds.height) / 2.0 + } + default: + if bounds.width > bounds.height { + xTranslate += (bounds.width - xScale * scaleLayer.bounds.width) / 2.0 + } else if bounds.width < bounds.height { + yTranslate += (bounds.height - yScale * scaleLayer.bounds.height) / 2.0 + } + } scaleLayer.transform = CATransform3DConcat( CATransform3DMakeScale(xScale, yScale, 1), diff --git a/Tests/PlayButtonTests/PlayButtonTests.swift b/Tests/PlayButtonTests/PlayButtonTests.swift index 0006def..fa0c1bb 100644 --- a/Tests/PlayButtonTests/PlayButtonTests.swift +++ b/Tests/PlayButtonTests/PlayButtonTests.swift @@ -21,7 +21,7 @@ final class PlayButtonTests: XCTestCase { windowView = vc.view SnapshotTesting.diffTool = "ksdiff" - // isRecording = true +// isRecording = true } func testPlayToPause() { @@ -225,6 +225,60 @@ final class PlayButtonTests: XCTestCase { button.setStop(true, animated: false) XCTAssertEqual(button.isStop, true) } + + func testScaleAspectFillWidthGreaterThanHeight() { + let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 60, height: 30)) + button.backgroundColor = .blue + button.contentMode = .scaleAspectFill + + windowView.addSubview(button) + assertSnapshot(matching: button, as: .image) + } + + func testScaleAspectFillHeightGreaterThanWidth() { + let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 30, height: 60)) + button.backgroundColor = .blue + button.contentMode = .scaleAspectFill + + windowView.addSubview(button) + assertSnapshot(matching: button, as: .image) + } + + func testScaleAspectFitWidthGreaterThanHeight() { + let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 60, height: 30)) + button.backgroundColor = .blue + button.contentMode = .scaleAspectFit + + windowView.addSubview(button) + assertSnapshot(matching: button, as: .image) + } + + func testScaleAspectFitHeightGreaterThanWidth() { + let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 30, height: 60)) + button.backgroundColor = .blue + button.contentMode = .scaleAspectFit + + windowView.addSubview(button) + assertSnapshot(matching: button, as: .image) + } + + func testScaleToFillWidthGreaterThanHeight() { + let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 60, height: 30)) + button.backgroundColor = .blue + button.contentMode = .scaleToFill + + windowView.addSubview(button) + assertSnapshot(matching: button, as: .image) + } + + func testScaleToFillHeightGreaterThanWidth() { + let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 30, height: 60)) + button.backgroundColor = .blue + button.contentMode = .scaleToFill + + windowView.addSubview(button) + assertSnapshot(matching: button, as: .image) + } } private enum CompletionCondition { diff --git a/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleAspectFillHeightGreaterThanWidth.1.png b/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleAspectFillHeightGreaterThanWidth.1.png new file mode 100644 index 0000000..dfab58d Binary files /dev/null and b/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleAspectFillHeightGreaterThanWidth.1.png differ diff --git a/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleAspectFillWidthGreaterThanHeight.1.png b/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleAspectFillWidthGreaterThanHeight.1.png new file mode 100644 index 0000000..e68f107 Binary files /dev/null and b/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleAspectFillWidthGreaterThanHeight.1.png differ diff --git a/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleAspectFitHeightGreaterThanWidth.1.png b/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleAspectFitHeightGreaterThanWidth.1.png new file mode 100644 index 0000000..a8ba7fd Binary files /dev/null and b/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleAspectFitHeightGreaterThanWidth.1.png differ diff --git a/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleAspectFitWidthGreaterThanHeight.1.png b/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleAspectFitWidthGreaterThanHeight.1.png new file mode 100644 index 0000000..26f21a9 Binary files /dev/null and b/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleAspectFitWidthGreaterThanHeight.1.png differ diff --git a/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleToFillHeightGreaterThanWidth.1.png b/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleToFillHeightGreaterThanWidth.1.png new file mode 100644 index 0000000..ab8c95c Binary files /dev/null and b/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleToFillHeightGreaterThanWidth.1.png differ diff --git a/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleToFillWidthGreaterThanHeight.1.png b/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleToFillWidthGreaterThanHeight.1.png new file mode 100644 index 0000000..88c0273 Binary files /dev/null and b/Tests/PlayButtonTests/__Snapshots__/PlayButtonTests/testScaleToFillWidthGreaterThanHeight.1.png differ