Skip to content

Commit a49ac3d

Browse files
committed
Fix scale and translation for different content modes
1 parent 2e36ddd commit a49ac3d

8 files changed

+74
-3
lines changed

Sources/PlayButton/PlayButton.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,25 @@ public class PlayButton: UIButton {
179179
yScale = scale
180180
}
181181

182-
let xTranslate = (xScale - 1) * scaleLayer.bounds.width / 2
183-
let yTranslate = (yScale - 1) * scaleLayer.bounds.height / 2
182+
var xTranslate = (xScale - 1.0) * scaleLayer.bounds.height / 2.0
183+
var yTranslate = (yScale - 1.0) * scaleLayer.bounds.width / 2.0
184+
185+
switch contentMode {
186+
case .scaleToFill:
187+
break
188+
case .scaleAspectFill:
189+
if bounds.width < bounds.height {
190+
xTranslate += (bounds.width - xScale * scaleLayer.bounds.width) / 2.0
191+
} else if bounds.width > bounds.height {
192+
yTranslate += (bounds.height - yScale * scaleLayer.bounds.height) / 2.0
193+
}
194+
default:
195+
if bounds.width > bounds.height {
196+
xTranslate += (bounds.width - xScale * scaleLayer.bounds.width) / 2.0
197+
} else if bounds.width < bounds.height {
198+
yTranslate += (bounds.height - yScale * scaleLayer.bounds.height) / 2.0
199+
}
200+
}
184201

185202
scaleLayer.transform = CATransform3DConcat(
186203
CATransform3DMakeScale(xScale, yScale, 1),

Tests/PlayButtonTests/PlayButtonTests.swift

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class PlayButtonTests: XCTestCase {
2121
windowView = vc.view
2222

2323
SnapshotTesting.diffTool = "ksdiff"
24-
// isRecording = true
24+
// isRecording = true
2525
}
2626

2727
func testPlayToPause() {
@@ -225,6 +225,60 @@ final class PlayButtonTests: XCTestCase {
225225
button.setStop(true, animated: false)
226226
XCTAssertEqual(button.isStop, true)
227227
}
228+
229+
func testScaleAspectFillWidthGreaterThanHeight() {
230+
let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 60, height: 30))
231+
button.backgroundColor = .blue
232+
button.contentMode = .scaleAspectFill
233+
234+
windowView.addSubview(button)
235+
assertSnapshot(matching: button, as: .image)
236+
}
237+
238+
func testScaleAspectFillHeightGreaterThanWidth() {
239+
let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 30, height: 60))
240+
button.backgroundColor = .blue
241+
button.contentMode = .scaleAspectFill
242+
243+
windowView.addSubview(button)
244+
assertSnapshot(matching: button, as: .image)
245+
}
246+
247+
func testScaleAspectFitWidthGreaterThanHeight() {
248+
let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 60, height: 30))
249+
button.backgroundColor = .blue
250+
button.contentMode = .scaleAspectFit
251+
252+
windowView.addSubview(button)
253+
assertSnapshot(matching: button, as: .image)
254+
}
255+
256+
func testScaleAspectFitHeightGreaterThanWidth() {
257+
let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 30, height: 60))
258+
button.backgroundColor = .blue
259+
button.contentMode = .scaleAspectFit
260+
261+
windowView.addSubview(button)
262+
assertSnapshot(matching: button, as: .image)
263+
}
264+
265+
func testScaleToFillWidthGreaterThanHeight() {
266+
let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 60, height: 30))
267+
button.backgroundColor = .blue
268+
button.contentMode = .scaleToFill
269+
270+
windowView.addSubview(button)
271+
assertSnapshot(matching: button, as: .image)
272+
}
273+
274+
func testScaleToFillHeightGreaterThanWidth() {
275+
let button = PlayButton(frame: CGRect(x: 0, y: 0, width: 30, height: 60))
276+
button.backgroundColor = .blue
277+
button.contentMode = .scaleToFill
278+
279+
windowView.addSubview(button)
280+
assertSnapshot(matching: button, as: .image)
281+
}
228282
}
229283

230284
private enum CompletionCondition {

0 commit comments

Comments
 (0)