当前位置:网站首页>Cocos makes Scrollview to realize the effect of zooming in the middle and zooming out on both sides
Cocos makes Scrollview to realize the effect of zooming in the middle and zooming out on both sides
2022-07-07 15:38:00 【Vegetable chicken on the road】
The effect is as follows :
1. To make a prefab, Then generate 10 Join to an empty node , Considering that if the number of levels generated is large , load prefab There must be a Caton effect when , So I found a method similar to frame loading on the Internet , Every prefab The position calculation can calculate the arrangement by itself , The code is as follows :
_initData(){
// establish item
let cb = (i) => {
this._initItemPrefab(i)
};
this._loadPrefabFrame('item', 2, this._itemLength, cb);
}
_loadPrefabFrame(name, limit, max, cb) {
let count = 0;
this[name] = () => {
if (count < max) {
for (let i = 0; i < limit; i++) {
count++;
cb && cb(count);
// Turn off animation
if (count === max - 1) {
}
}
}
};
this.schedule(this[name], 1 / 60, (max - 1) / limit, 0);
}
private _initItemPrefab(index: number) {
let node = LevelModel.inst.createLevelUnitNode();
let unit: HomeLevelUnit = node.getComponent("HomeLevelUnit");
unit.ID = index;
unit.setData();
this.itemList.push(unit);
this.levelContent.addChild(node);
}
2. Load this prefab The parent node of joins the click event
_touchEvent(){
this.levelContent.on('touchstart', function (event) {
this._moveSpeed = 0;
this._startTime = new Date().getTime();
}.bind(this));
this.levelContent.on('touchmove', function (event) {
var movePos = event.getDelta();
this.itemMoveBy(movePos);
}.bind(this));
this.levelContent.on('touchend', function (event) {
this.touchEnd(event)
}.bind(this));
this.levelContent.on('touchcancel', function (event) {
this.touchEnd(event)
}.bind(this));
}
3. Listen and handle events
touchEnd(event) {
var curpos = event.getLocation();
var startpos = event.getStartLocation();
var dis = startpos.x - curpos.x;
var curTime = new Date().getTime();
var disTime = curTime - this._startTime;
this._moveSpeed = dis / disTime;
this.fixedPosition = true;
}
itemMoveBy(pos) {
if(this.itemList[0].node.x > 0 && pos.x>0){
return;
}
if(this.itemList[this.itemList.length-1].node.x < 0 && pos.x<0){
return;
}
for (let i = 0; i < this.itemList.length; i++) {
this.itemList[i].node.active = true;
this.itemList[i].node.x += pos.x;
}
this.updateScale();
}
updateScale() {
if (this.scaleMax < this.scaleMin || this.scaleMax == 0) {
return;
}
for (let i = 0; i < this.itemList.length; i++) {
var pre;
var x = this.itemList[i].node.x + this._maxSize.width / 2;
if (this.itemList[i].node.x < 0) {
pre = x / this._maxSize.width;
}
else {
pre = 1 - x / this._maxSize.width;
}
pre *= 2;
var scaleTo = this.scaleMax - this.scaleMin;
scaleTo *= pre;
scaleTo += this.scaleMin;
scaleTo = Math.abs(scaleTo);
this.itemList[i].node.scaleX = scaleTo;
this.itemList[i].node.scaleY = scaleTo;
if(this.itemList[i].node.scaleX < 0.8){
this.itemList[i].node.active = false;
}else if(this.itemList[i].node.scaleX >=0.95 && this.itemList[i].node.scaleX<=1){
this.itemList[i].node.zIndex = 100;
}else{
this.itemList[i].node.zIndex = 0;
}
}
}
update(dt) {
if(!this.fixedPosition){
return;
}
if (this._moveSpeed == 0 && this.fixedPosition){
this.fixedPosition = false;
let _maxIndex = 0;
let _maxScale = 0;
for (let i = 0; i < this.itemList.length; i++) {
if(this.itemList[i].node.scaleX > _maxScale){
_maxScale = this.itemList[i].node.scaleX;
_maxIndex = i;
}
}
for (let i = 0; i < this.itemList.length; i++) {
this.itemList[i].node.x = 168 * (i - _maxIndex);
if(i == _maxIndex){
this.itemList[_maxIndex].node.scaleX = 1;
this.itemList[_maxIndex].node.zIndex = 100;
}else{
this.itemList[i].node.scaleX = 0.8;
this.itemList[i].node.zIndex = 0;
}
}
};
if(this.itemList[0].node.x > 0){
return;
}
if(this.itemList[this.itemList.length-1].node.x < 0){
return;
}
for (let i = 0; i < this.itemList.length; i++) {
this.itemList[i].node.x -= this._moveSpeed * dt * this.speed;
}
if (this._moveSpeed > 0) {
this._moveSpeed -= dt * this.rub;
if (this._moveSpeed < 0) {
this._moveSpeed = 0;
}
} else {
this._moveSpeed += dt * this.rub;
if (this._moveSpeed > 0) {
this._moveSpeed = 0;
}
}
var moveTo = -this._moveSpeed * dt * this.speed;
this.itemMoveBy(cc.v2(moveTo, moveTo))
}
4. explain
Last in update The main processing in is to let him have a smooth deceleration to stop process , Make him look less stiff .
( Of course, when doing this, you don't need to generate so many at once prafab Of , I thought for a moment about 5 Just one , Then do the fake infinite loop effect , This should be the best , I'll change it later , Code has reference to others' code , It's a little messy. I'll sort it out later )
** Be careful : I'll give it directly here node Of x assignment , In fact, this is wrong , It is better not to assign the location information directly , To pass the setPosition Go set , Otherwise, the matrix will not be updated , Typically, this node has child nodes , If you assign directly , Child nodes will not follow the change of location
边栏推荐
- Configure mongodb database in window environment
- Pit avoidance: description of null values in in and not in SQL
- [quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
- Unity之ASE实现全屏风沙效果
- HW primary flow monitoring, what should we do
- Ctfshow, information collection: web1
- MySQL bit type resolution
- 【OBS】RTMPSockBuf_ Fill, remote host closed connection.
- Super simple and fully automated generation super signature system (cloud Xiaoduo minclouds.com cloud service instance), free application in-house test app distribution and hosting platform, maintenan
- 避坑:Sql中 in 和not in中有null值的情况说明
猜你喜欢

【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码

【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)
![[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)](/img/a3/7f08f189c608d6086b368dfa3831f7.png)
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)

#HPDC智能基座人才发展峰会随笔

Ida Pro reverse tool finds the IP and port of the socket server

【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例

Monthly observation of internet medical field in May 2022

如何在opensea批量发布NFT(Rinkeby测试网)
使用Scrapy框架爬取网页并保存到Mysql的实现
![[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)](/img/78/29eb8581e9a8fb4c6c7e1e35ad7adc.png)
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
随机推荐
居然从408改考自命题!211华北电力大学(北京)
[quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
Starting from 1.5, build a microservice framework link tracking traceid
MySQL bit类型解析
【数字IC验证快速入门】25、SystemVerilog项目实践之AHB-SRAMC(5)(AHB 重点回顾,要点提炼)
[quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)
2.Golang基础知识
Create lib Library in keil and use lib Library
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
MongoD管理数据库的方法介绍
Pit avoidance: description of null values in in and not in SQL
Use cpolar to build a business website (2)
Typescript release 4.8 beta
[Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering
CTFshow,信息搜集:web7
数学建模——什么是数学建模
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
【深度学习】图像超分实验:SRCNN/FSRCNN
[target detection] yolov5 Runtong voc2007 data set
What is Base64?