Skip to content

Commit 82323e8

Browse files
committed
fix: safely handle touches
1 parent bb68e56 commit 82323e8

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

ios/RiveReactNativeView.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,8 @@ class RiveReactNativeView: RCTView, RivePlayerDelegate, RiveStateMachineDelegate
854854

855855
@objc open func touchBegan(_ location: CGPoint) {
856856
handleTouch(location: location) { machine, abLocation in
857-
guard let riveView = viewModel?.riveView else { fatalError("No RiveView") }
858-
guard let artboard = viewModel?.riveModel?.artboard else { fatalError("Malformed RiveModel") }
857+
guard let riveView = viewModel?.riveView else { return }
858+
guard let artboard = viewModel?.riveModel?.artboard else { return }
859859
if (riveView.stateMachineDelegate?.touchBegan != nil) {
860860
riveView.stateMachineDelegate?.touchBegan?(onArtboard: artboard, atLocation: abLocation)
861861
}
@@ -864,33 +864,34 @@ class RiveReactNativeView: RCTView, RivePlayerDelegate, RiveStateMachineDelegate
864864

865865
@objc open func touchMoved(_ location: CGPoint) {
866866
handleTouch(location: location) { machine, abLocation in
867-
guard let riveView = viewModel?.riveView else { fatalError("No RiveView") }
868-
guard let artboard = viewModel?.riveModel?.artboard else { fatalError("Malformed RiveModel") }
867+
guard let riveView = viewModel?.riveView else { return }
868+
guard let artboard = viewModel?.riveModel?.artboard else { return }
869869
riveView.stateMachineDelegate?.touchMoved?(onArtboard: artboard, atLocation: abLocation)
870870
}
871871
}
872872

873873
@objc open func touchEnded(_ location: CGPoint) {
874874
handleTouch(location: location) { machine, abLocation in
875-
guard let riveView = viewModel?.riveView else { fatalError("No RiveView") }
876-
guard let artboard = viewModel?.riveModel?.artboard else { fatalError("Malformed RiveModel") }
875+
guard let riveView = viewModel?.riveView else { return }
876+
guard let artboard = viewModel?.riveModel?.artboard else { return }
877877
riveView.stateMachineDelegate?.touchEnded?(onArtboard: artboard, atLocation: abLocation)
878878
}
879879
}
880880

881881
@objc open func touchCancelled(_ location: CGPoint) {
882882
handleTouch(location: location) { machine, abLocation in
883-
guard let riveView = viewModel?.riveView else { fatalError("No RiveView") }
884-
guard let artboard = viewModel?.riveModel?.artboard else { fatalError("Malformed RiveModel") }
883+
guard let riveView = viewModel?.riveView else { return }
884+
guard let artboard = viewModel?.riveModel?.artboard else { return }
885885
riveView.stateMachineDelegate?.touchCancelled?(onArtboard: artboard, atLocation: abLocation)
886886
}
887887
}
888888

889889
private func handleTouch(location: CGPoint, action: (RiveStateMachineInstance, CGPoint)->Void) {
890+
guard let bounds = viewModel?.riveModel?.artboard?.bounds() else { return }
890891
if let viewModel = viewModel, let riveView = viewModel.riveView {
891892
let artboardLocation = riveView.artboardLocation(
892893
fromTouchLocation: location,
893-
inArtboard: viewModel.riveModel?.artboard?.bounds() ?? CGRect.zero,
894+
inArtboard: bounds,
894895
fit: viewModel.fit,
895896
alignment: viewModel.alignment
896897
)

0 commit comments

Comments
 (0)