refactor(apple): advance artboard with layout fit on layoutSubview (#10427) 759f2fc742

Co-authored-by: David Skuza <david@rive.app>
This commit is contained in:
dskuza
2025-08-27 15:07:14 +00:00
parent e896ee7a1e
commit 085fb846f7
4 changed files with 35 additions and 1 deletions

View File

@@ -25,6 +25,7 @@
@property(nullable, nonatomic, readonly) id<CAMetalDrawable> currentDrawable;
@property(nonatomic) MTLPixelFormat colorPixelFormat;
@property(nonatomic) CGSize drawableSize;
- (void)drawableSizeDidChange:(CGSize)drawableSize;
@end
#endif /* RiveMetalDrawableView_h */

View File

@@ -110,6 +110,7 @@
newSize.width *= self.traitCollection.displayScale;
newSize.height *= self.traitCollection.displayScale;
self.drawableSize = newSize;
[self drawableSizeDidChange:newSize];
}
- (void)setContentScaleFactor:(CGFloat)contentScaleFactor
@@ -145,9 +146,19 @@
[self drawRect:self.bounds];
}
- (void)drawableSizeDidChange:(CGSize)drawableSize
{}
@end
#else
@implementation RiveMTKView
- (void)setDrawableSize:(CGSize)drawableSize
{
[super setDrawableSize:drawableSize];
[self drawableSizeDidChange:drawableSize];
}
- (void)drawableSizeDidChange:(CGSize)drawableSize
{}
@end
#endif

View File

@@ -436,6 +436,28 @@ open class RiveView: RiveRendererView {
}
}
open override func drawableSizeDidChange(_ drawableSize: CGSize) {
super.drawableSizeDidChange(drawableSize)
if fit == .layout, let artboard = riveModel?.artboard {
let currentSize = drawableSize
let artboardSize = artboard.bounds().size
if currentSize != artboardSize {
// We can use currentSize; we are mirroring setting
// the updated layout size (if needed) as in drawRive,
// which uses the same rect as drawRect (assuming 'self')
let scale = layoutScaleFactor == RiveView.Constants.layoutScaleFactorAutomatic ? _layoutScaleFactor : layoutScaleFactor
artboard.setWidth(Double(currentSize.width) / scale)
artboard.setHeight(Double(currentSize.height) / scale)
advance(delta: 0)
}
}
}
open override func layoutSubviews() {
super.layoutSubviews()
drawableSizeDidChange(drawableSize)
}
// MARK: - UITraitCollection
#if os(iOS)
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {