Update to use the aspectRatio modifier instead of empty modifier, which makes it match SwiftUI.Image in attribute graph (no extra empty attr)

This commit is contained in:
DreamPiggy 2020-03-29 12:43:57 +08:00
parent 8984320cb6
commit ba1b36406d
1 changed files with 3 additions and 9 deletions

View File

@ -556,11 +556,11 @@ extension AnimatedImage {
// So, if we don't override this method, SwiftUI ignore the content mode on actual ImageView // So, if we don't override this method, SwiftUI ignore the content mode on actual ImageView
// To workaround, we want to call the default `SwifUI.View.aspectRatio(_:contentMode:)` method // To workaround, we want to call the default `SwifUI.View.aspectRatio(_:contentMode:)` method
// But 2: there are no way to call a Protocol Extention default implementation in Swift 5.1 // But 2: there are no way to call a Protocol Extention default implementation in Swift 5.1
// So, we need a hack, that create a empty modifier, they call method on that view instead // So, we directly call the implementation detail modifier instead
// Fired Radar: FB7413534 // Fired Radar: FB7413534
self.imageLayout.aspectRatio = aspectRatio self.imageLayout.aspectRatio = aspectRatio
self.imageLayout.contentMode = contentMode self.imageLayout.contentMode = contentMode
return self.modifier(EmptyModifier()).aspectRatio(aspectRatio, contentMode: contentMode) return self.modifier(_AspectRatioLayout(aspectRatio: aspectRatio, contentMode: contentMode))
} }
/// Constrains this view's dimensions to the aspect ratio of the given size. /// Constrains this view's dimensions to the aspect ratio of the given size.
@ -572,13 +572,7 @@ extension AnimatedImage {
/// - Returns: A view that constrains this view's dimensions to /// - Returns: A view that constrains this view's dimensions to
/// `aspectRatio`, using `contentMode` as its scaling algorithm. /// `aspectRatio`, using `contentMode` as its scaling algorithm.
public func aspectRatio(_ aspectRatio: CGSize, contentMode: ContentMode) -> some View { public func aspectRatio(_ aspectRatio: CGSize, contentMode: ContentMode) -> some View {
var ratio: CGFloat? return self.aspectRatio(aspectRatio.width / aspectRatio.height, contentMode: contentMode)
if aspectRatio.width > 0 && aspectRatio.height > 0 {
ratio = aspectRatio.width / aspectRatio.height
} else {
NSException(name: .invalidArgumentException, reason: "\(type(of: self)).\(#function) should be called with positive aspectRatio", userInfo: nil).raise()
}
return self.aspectRatio(ratio, contentMode: contentMode)
} }
/// Scales this view to fit its parent. /// Scales this view to fit its parent.