Skip to content

Commit

Permalink
Fix scale and translation for different content modes
Browse files Browse the repository at this point in the history
  • Loading branch information
fruitcoder committed Nov 8, 2022
1 parent 2e36ddd commit a49ac3d
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 3 deletions.
21 changes: 19 additions & 2 deletions Sources/PlayButton/PlayButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
56 changes: 55 additions & 1 deletion Tests/PlayButtonTests/PlayButtonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class PlayButtonTests: XCTestCase {
windowView = vc.view

SnapshotTesting.diffTool = "ksdiff"
// isRecording = true
// isRecording = true
}

func testPlayToPause() {
Expand Down Expand Up @@ -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 {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a49ac3d

Please sign in to comment.