I tried to make a repeated animation, and at first I use the setInterval() to achieve this , but the result seems not well because there is a small break between animations.
And I tried to use the callback in start() :
_rotate(){
this.state.rotation.setValue(0)
Animated.timing(
this.state.rotation,
{
duration:this.speed,
toValue:1,
}
).start( this._rotate.bind(this) )
}
Expected a continuously animation , but the result is just like the situation when using setInterval().
And, when there is no callback in start() , I can use
this.state.rotation.stopAnimation()
to stop the animation. But with the callback , it does not work.
Maybe I missed something. Anyone helps?
After digging into the source code, it seems that Facebook need to do more on the react-native document.
The callback in start should be like this:
//AnimatedImplementation.js
type EndCallback = (result: EndResult) => void;
So , I changed my code to:
_rotate(result){
this.state.rotation.setValue(0)
Animated.timing(
this.state.rotation,
{
duration:this.speed,
toValue:1,
}
).start( this._rotate.bind(this,result) )
}
Then, the problem is gone....
Related
I have an async watch that fetches some data from the server. It will batch process the Response in a blocking operation. I am trying to update the view before kicking off the blocking operation, like so:
Vue.component("foo-bar", {
...
watch: {
async things() {
const response = await API.getThings();
this.someUIMessage = `processing ${response.length} things...`;
someBlockingOperation(response);
}
}
}
But this.someUIMessage is not updated in the view until after someBlockingOperation. I stuck an await Vue.nextTick() in between setting the string and calling the blocking op, and this.$forceUpdate(), but without success.
What does work (sometimes! depends on what else is going on in the app) is calling setTimeout(() => someBlockingOperation(response), 0), but that seems like a kludge. Is there a step I'm missing?
You may need to show some more code because your use case is exactly described in the docs - Async Update Queue and await Vue.nextTick() should work
If your someBlockingOperation take too long, it may be worth to think about UI responsiveness anyway and maybe apply a strategy of splitting up you workload into smaller chunks, process only one at a time and use setTimeout(nextBatch, 0) to schedule "next chunk" processing. See this SO question for more details...
in this piece of code:
Vue.prototype.$navigateTo( MenuModule, {
frame : 'basement' ,
backstackVisible : true ,
transition : {
name : "slideTop",
duration : 300 ,
}
} )
how can I run a function that belongs to the MenuModule after the navigation is FULLY COMPLETED (after 300ms : duration).
I'm not looking for "setTimeout" method, cause using that seems cause some UI problems, (the animation for navigating takes sometimes a bit longer than 300ms, so I would like to detect prefect timing to run a function right after that it is FULLY NAVIGATED to the module).
Use mounted() event on MenuModule to trigger the function.
This is just general question. I want to make drums-like app but i have sound delay. Maybe someone tried some other package(s) and did some magic, I used react-native-sound and react-native-video since they are most popular and not outdated like others. This is code sample:
new Sound(`${filename}.mp3`, Sound.MAIN_BUNDLE, error => {
if (error) {
return;
} else {
audio.play(() => {
audio.release();
});
}
});
There are other libraries, maybe outdated, but i am looking for any input to make it better, we did handler with onPressIn to make it slightly faster but still big delay:
<TouchableOpacity onPressIn={handleOpenPress}>
If this fails I will try to make another library, but I fear it will have the same delay. For some reason this repo works very fast: https://github.com/dwicao/react-native-drum-kit. I used same code and made even a list with prepared sounds to be used like soundList[i++ % 10].play() but still no effect.
Without a 3rd party lib, we can detect orientation changes with DeviceEventEmitter with this undocumented feature like this:
import { DeviceEventEmitter } from 'react-native'
function handleOrientationDidChange(data) {
console.log('orientation changed, data:', data)
}
DeviceEventEmitter.addListener('namedOrientationDidChange', handleOrientationDidChange);
This gives us data that looks like this:
{ rotationDegrees: -90, isLandscape: true, name: "landscape-primary" }
Note: I tested this only on Android. It would be nice to know if it works on iOS too.
However this only works ON CHANGE. Is there a way to get this info on startup?
Have you tried this library!
Here is example usage from the repo:
componentWillMount() {
// The getOrientation method is async. It happens sometimes that
// you need the orientation at the moment the JS runtime starts running on device.
// `getInitialOrientation` returns directly because its a constant set at the
// beginning of the JS runtime.
const initial = Orientation.getInitialOrientation();
if (initial === 'PORTRAIT') {
// do something
} else {
// do something else
}
}
It will have a callback function called repeatedly to when I start a download task. But It will be so slow that it can't give feedback when touch a button.
Fileio.downloadFile(downloadData[downloadFileIndex].uri, '1.jpg',this.progressFunc.bind(this)).then((DownloadResult)=> {
if (DownloadResult.statusCode == 200) {
let nextDownloadFileIndex = this.props.downloadFileIndex + 1;
this.props.dispatch(DOWNLOADED_A_FILE({
downloadFileIndex:nextDownloadFileIndex,
progressNum:0
}))
}
}).catch((error)=> {
console.log(error)
})
This is my code and the callback function are as be folllowed
progressFunc(DownloadBeginCallbackResult) {
let progressNum = DownloadBeginCallbackResult.bytesWritten / DownloadBeginCallbackResult.contentLength;
if(progressNum<=0.99){
this.props.dispatch(DOWNLOADING_A_FILE({
progressNum:progressNum,
jobId:this.props.jobId
}));
}else{
this.props.dispatch(DOWNLOADING_A_FILE({
progressNum:0,
jobId:this.props.jobId
}));
}
}
I mean I can't get feedback immediately when I touch button. I think that it is because I have a callback function called repeatedly. so js can't handle so many tasks;
It does sound like JS thread is busy doing the request and not able to communicate back to UI thread. One thing you can try is to wrap you on press handler in an InteractionManager.runAfterInteractions(() => ...)
See https://facebook.github.io/react-native/docs/interactionmanager.html