Performance 我想在react native中更改滚动视图滚动速度
现在我用间隔使它成为现实,但它是非常不连贯的。 如果我能改变方法(Performance 我想在react native中更改滚动视图滚动速度,performance,react-native,scrollview,Performance,React Native,Scrollview,现在我用间隔使它成为现实,但它是非常不连贯的。 如果我能改变方法(滚动)的速度,那就太好了 this.interval = setInterval(()=>{ if(!_scroll){ _this.interval && clearInterval(_this.interval); } if(totalWide+ScreenWidth >= width ){ _scroll.scrollWithoutAnima
滚动
)的速度,那就太好了
this.interval = setInterval(()=>{
if(!_scroll){
_this.interval && clearInterval(_this.interval);
}
if(totalWide+ScreenWidth >= width ){
_scroll.scrollWithoutAnimationTo();
totalWide=0;
i=0;
}else{
_scroll.scrollTo({x:eachWide*i,animate:true});
totalWide = totalWide + eachWide;
i= i+1;
}
},250)
使用ScrollView的属性
<ScrollView decelerationRate={0.5}>
</ScrollView>
我通过让setInterval调用函数(在函数中定义滚动条移动的逻辑或速度)实现了这一点
确保在完成后调用clearInterval(this.interval)。我建议附加到js(据我所知,React Native支持js) 下面的示例将从上到下线性滚动。如果需要调到不同的偏移量,只需更改距离变量即可
startingPoint
变量在从上到下滚动时是多余的,但将保留在示例中
scroll() {
if (this.scrollAnimationFrame) {
cancelAnimationFrame(this.scrollAnimationFrame);
}
this.listRef.scrollToOffset({offset: 0, animated: false}); // remove if You don't start scroll from top
const duration = this.scrollTime,
startingPoint = 0, // change if You don't start scroll from top
distance = Scrolling.LINE_HEIGHT * Scrolling.ITEMS_COUNT;
let startTimestamp, progress;
const frameCallback = (timestamp) => {
if (!startTimestamp) {
startTimestamp = timestamp;
}
progress = timestamp - startTimestamp;
this.listRef.scrollToOffset({
offset: distance * (progress / duration) + startingPoint,
animated: false,
});
if (progress < duration) {
this.scrollAnimationFrame = requestAnimationFrame(frameCallback);
}
};
this.scrollAnimationFrame = requestAnimationFrame(frameCallback);
}
scroll(){
如果(此.scrollAnimationFrame){
cancelAnimationFrame(此.scrollAnimationFrame);
}
this.listRef.scrollToOffset({offset:0,animated:false});//如果不从顶部开始滚动,请删除
const duration=this.scrollTime,
startingPoint=0,//如果不从顶部开始滚动,则更改
距离=滚动.LINE\u高度*滚动.ITEMS\u计数;
让我们开始前进;
常量帧回调=(时间戳)=>{
如果(!startTimestamp){
startTimestamp=时间戳;
}
进度=时间戳-开始时间戳;
this.listRef.scrollToOffset({
偏移量:距离*(进度/持续时间)+起始点,
动画:错,
});
如果(进度<持续时间){
this.scrollAnimationFrame=requestAnimationFrame(frameCallback);
}
};
this.scrollAnimationFrame=requestAnimationFrame(frameCallback);
}
思考,但这不是滚动速度,这不是我需要的。请注意,此属性仅适用于iOS
。使用android
scroll() {
if (this.scrollAnimationFrame) {
cancelAnimationFrame(this.scrollAnimationFrame);
}
this.listRef.scrollToOffset({offset: 0, animated: false}); // remove if You don't start scroll from top
const duration = this.scrollTime,
startingPoint = 0, // change if You don't start scroll from top
distance = Scrolling.LINE_HEIGHT * Scrolling.ITEMS_COUNT;
let startTimestamp, progress;
const frameCallback = (timestamp) => {
if (!startTimestamp) {
startTimestamp = timestamp;
}
progress = timestamp - startTimestamp;
this.listRef.scrollToOffset({
offset: distance * (progress / duration) + startingPoint,
animated: false,
});
if (progress < duration) {
this.scrollAnimationFrame = requestAnimationFrame(frameCallback);
}
};
this.scrollAnimationFrame = requestAnimationFrame(frameCallback);
}