Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions ios/legacy/RiveReactNativeView.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import MetalKit
import NitroModules
import RiveRuntime
import UIKit
Expand Down Expand Up @@ -141,7 +142,7 @@
}

func playIfNeeded() {
if let baseViewModel = baseViewModel, (baseViewModel.isPlaying == false) {

Check warning on line 145 in ios/legacy/RiveReactNativeView.swift

View workflow job for this annotation

GitHub Actions / lint-swift

`if`, `for`, `guard`, `switch`, `while`, and `catch` statements shouldn't unnecessarily wrap their conditionals or arguments in parentheses (control_statement)
Task {
baseViewModel.play()
}
Expand Down Expand Up @@ -230,6 +231,8 @@
riveView = baseViewModel?.createRiveView()

if let riveView = riveView {
// The drawable is sized by `syncDrawableSize()` instead.
riveView.autoResizeDrawable = false
riveView.translatesAutoresizingMaskIntoConstraints = false
addSubview(riveView)
riveView.stateMachineDelegate = self
Expand All @@ -239,6 +242,37 @@
riveView.topAnchor.constraint(equalTo: topAnchor),
riveView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
setNeedsLayout()
}
}

override func layoutSubviews() {
super.layoutSubviews()
syncDrawableSize()
}

override func didMoveToWindow() {
super.didMoveToWindow()
syncDrawableSize()
}

/// MTKView's automatic resize scales the bounds by per-axis factors it
/// computes once, on its first window attach when `nativeScale != scale`
/// (Display Zoom, mini and Plus iPhones), by converting the screen bounds
/// through every ancestor transform. A rotated ancestor at that moment
/// leaves a non-uniform drawable for the view's lifetime, so `contain`
/// squashes the artboard; a zero-scale ancestor leaves it blank. Sizing
/// from the view's own bounds ignores ancestor transforms.
private func syncDrawableSize() {
guard let riveView = riveView, let screen = window?.screen else { return }
let pointSize = riveView.bounds.size
guard pointSize.width > 0, pointSize.height > 0 else { return }
let size = CGSize(
width: (pointSize.width * screen.nativeScale).rounded(),
height: (pointSize.height * screen.nativeScale).rounded()
)
if riveView.drawableSize != size {
riveView.drawableSize = size
}
}

Expand Down
Loading