-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add responsive layouts #273
Changes from 7 commits
5acf085
23cbf89
c708c5f
27443b8
5ad8d24
5fee930
559e685
f220218
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import * as React from 'react'; | ||
import { SafeAreaView, StyleSheet, View, Button, Text } from 'react-native'; | ||
import Rive, { Fit } from 'rive-react-native'; | ||
|
||
const resourceName = 'layout_test'; | ||
|
||
export default function ResponsiveLayout() { | ||
const [scaleFactor, setScaleFactor] = React.useState(4.0); | ||
|
||
const increaseScale = () => setScaleFactor((prev) => prev + 0.5); | ||
const decreaseScale = () => | ||
setScaleFactor((prev) => Math.max(0.5, prev - 0.5)); | ||
|
||
return ( | ||
<SafeAreaView style={styles.safeAreaViewContainer}> | ||
<Rive | ||
autoplay={true} | ||
style={styles.animation} | ||
fit={Fit.Layout} | ||
layoutScaleFactor={scaleFactor} // If you do not set this (or set equal to "-1.0"), Rive will automatically scale the layout to match the device pixel ratio | ||
resourceName={resourceName} | ||
artboardName={'Artboard'} | ||
stateMachineName={'State Machine 1'} | ||
/> | ||
<View style={styles.controls}> | ||
<Text style={styles.label}>Layout Scale Factor</Text> | ||
<View style={styles.scaleControls}> | ||
<Button title="-" onPress={decreaseScale} /> | ||
<View style={styles.scaleText}> | ||
<Text>{scaleFactor.toFixed(1)}x</Text> | ||
</View> | ||
<Button title="+" onPress={increaseScale} /> | ||
</View> | ||
</View> | ||
</SafeAreaView> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
safeAreaViewContainer: { | ||
flex: 1, | ||
}, | ||
animation: { | ||
width: '100%', | ||
flex: 1, | ||
}, | ||
controls: { | ||
padding: 16, | ||
alignItems: 'center', | ||
}, | ||
picker: { | ||
flex: 1, | ||
width: '100%', | ||
}, | ||
pickerWrapper: { | ||
borderWidth: 1, | ||
borderColor: 'black', | ||
borderRadius: 5, | ||
alignItems: 'center', | ||
margin: 16, | ||
}, | ||
pickersWrapper: { | ||
flex: 1, | ||
padding: 16, | ||
alignSelf: 'stretch', | ||
}, | ||
scaleControls: { | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
marginVertical: 16, | ||
gap: 16, | ||
}, | ||
scaleText: { | ||
minWidth: 50, | ||
alignItems: 'center', | ||
}, | ||
label: { | ||
fontSize: 16, | ||
fontWeight: '500', | ||
marginTop: 16, | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,8 @@ class RiveReactNativeView: RCTView, RivePlayerDelegate, RiveStateMachineDelegate | |
|
||
@objc var fit: String? | ||
|
||
@objc var layoutScaleFactor: NSNumber = -1.0 // -1.0 will inform the iOS runtime to determine the correct scale factor automatically | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double doesn't work for nullable(optional) values in these bindings: https://reactnative.dev/docs/legacy/native-modules-ios#argument-types |
||
|
||
@objc var alignment: String? | ||
|
||
@objc var autoplay: Bool | ||
|
@@ -99,6 +101,10 @@ class RiveReactNativeView: RCTView, RivePlayerDelegate, RiveStateMachineDelegate | |
if (changedProps.contains("alignment")) { | ||
viewModel?.alignment = convertAlignment(alignment) | ||
} | ||
|
||
if (changedProps.contains("layoutScaleFactor")) { | ||
viewModel?.layoutScaleFactor = layoutScaleFactor.doubleValue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, usage is here as a double value. But, still curious! |
||
} | ||
} | ||
|
||
private func convertFit(_ fit: String? = nil) -> RiveFit { | ||
|
@@ -143,6 +149,8 @@ class RiveReactNativeView: RCTView, RivePlayerDelegate, RiveStateMachineDelegate | |
updatedViewModel = RiveViewModel(fileName: name, fit: convertFit(fit), alignment: convertAlignment(alignment), autoPlay: autoplay, artboardName: artboardName) | ||
} | ||
|
||
updatedViewModel.layoutScaleFactor = layoutScaleFactor.doubleValue | ||
|
||
createNewView(updatedViewModel: updatedViewModel) | ||
requiresLocalResourceReconfigure = false | ||
} | ||
|
@@ -161,6 +169,8 @@ class RiveReactNativeView: RCTView, RivePlayerDelegate, RiveStateMachineDelegate | |
} else { | ||
updatedViewModel = RiveViewModel(webURL: url, fit: convertFit(fit), alignment: convertAlignment(alignment), autoPlay: autoplay, artboardName: artboardName) | ||
} | ||
|
||
updatedViewModel.layoutScaleFactor = layoutScaleFactor.doubleValue | ||
|
||
createNewView(updatedViewModel: updatedViewModel) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably fine, just calling this out as a thing that's been committed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I forgot this, will remove