Using Engine: COCOS2D-JS 3.0
Languages used: JavaScript
Running platform: mobile Web
---------------------------------------------------------------
Preliminary analysis:
Operation Process:
Slide, rotate, slow down, balance, stop.
function Analysis of time picker:
1. Slide
2. N option results (eg. 0~10)
3. Some numbers are visible
4. According to recent principles, automatically balance to the result item
5 .....
--------------------------------------------------------------------
As a qualified cocos2d programmer, the head first reaction should be at least: ScrollView, Clippingnode.
So we immediately faced two options, one of inheriting scrollview, or using Clippingnode to build the wheels ourselves.
First of all, not to say that ScrollView has so many private variables, the most unfavorable is that its whole sliding logic and time selector is different, it is an end. Therefore, if we choose to inherit the practice, then undoubtedly the workload will be more (modify the ScrollView logic flow).
In fact, the logic of cyclic scrolling is not complicated, we do not need to use any of the existing sliding class control, whether it is scalability or efficiency, this wheel is the best choice to build their own.
So we use Clippingnode?
No! Instead, use Ccui::layout. The reason I chose it eventually was that it did not need to be set stencil, and a straight sentence could be set as cropping (setclippingenabled), reducing the complexity of the coordinate calculation of child parent nodes.
--------
The UI is as follows:
--------
--------
Core points:
--------
1. In view of efficiency and convenience, use a contentnode to manage all item.
2. Handle the interaction between touch and movement through the _bmoveing and _btouching logos.
3. The implementation logic of cyclic scrolling: Move up, then the top item will move to the bottom, and vice versa.
4. After the operation has been completed, make a corrective balance (move it to a fixed position exactly).
--------
Code implementation:
--------
/** * Created by XXF on 12/19/2014. * * 1. Touchtime < 0.3 s is determined to be accelerated * 2. Balance of Judgment * 3. Balance after stop * * * eg: * * var list = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]; var definition = {size:cc.size (60,125), Items:list, BackGround:res.back }; var node = new Scrollselector (definition); */var scrollselector = ccui. Layout.extend ({_visibleclipnumber:3,//display the number of items at the same time _velocity:3,//Rate _timelimit:0.5,//Time Limit _distancelimit:500,//Distance limit _timecondition:0.3,//Time condition _distancecondition:30,//Distance condition _diffy:0, _diffycount:0,//Periodic count, y-axis movement distance _oncediffycount:0,//Touch y-axis movement distance _timecount:0,//Touch Time timing _runningaction:null, _list:null,//Item list, used to balance coordinates _originlist:null, _currentitemindex:2, Currently is the number of item _value:null,//current item of value _beginpos:null, _contentnode:null, _background:null, _bmoveinG:false, _btouching:false, _bbegincounttime:false, _fontcolor:cc.color.black, _fontsize:40, _fontname: "A Rial ",//_mode:null,//game type: selector, game ctor:function (params) {//params parameter list: items, textures.back, size This._super (); This.setclippingenabled (TRUE); This.setcontentsize (params.size); This.setanchorpoint (CC.P (0.5, 0)); This.ignoreanchor = false; This._background = new CC. Sprite (Params.background); This.addchild (This._background); This._background.x = THIS.WIDTH/2; This._background.y = THIS.HEIGHT/2; This._contentnode = new CC. Node (); This._contentnode.setanchorpoint (CC.P (0.5,0)); This.addchild (This._contentnode); This._contentnode.setposition (CC.P (THIS.WIDTH/2, 0)); Create n Item this._list = []; This._originlist = []; for (var i = 0; i < params.items.length; i++) {var text = Ccui. Text.create (Params.items[i], This._fontName, this._fontsize); Text.settextverticalalignment (CC. Vertical_text_alignment_center); Text.getvirtualrenderer (). SetColor (This._fontcolor); Text.settextareasize (Cc.size (Params.size.width, PARAMS.SIZE.HEIGHT/3)); Text.setposition (CC.P (0, i* text.height + text.height/2)); This._contentnode.addchild (Text,1,i); This._list.push (text); This._originlist.push (text); } This._value = This._originlist[1].getstring (); This._lister = cc. Eventlistener.create ({swallowtouches:true, event:cc. Eventlistener.touch_one_by_one, Ontouchbegan:this._ontouchbegan, ontouchmoved:this._ontouchmoved, ontouchended:this._ontouchended}); Cc.eventManager.addListener (This._lister, this); This.scheduleupdatewithpriority (0); init data This._diffycount = this._list[0].height; This._beginpos = CC.P (0,0); This._distanCelimit = params.size.height*2 + math.random () *50; }, Update:function (dt) {if (!this._btouching && this._bmoveing) {//action, calculate offset var diff Y = This._contentnode.y-this._beginpos.y; This._diffycount = This._diffycount + Diffy; This._beginpos = This._contentnode.getposition (); } this._balance (); if (this._bbegincounttime) This._timecount = this._timecount + dt; }, _ontouchbegan:function (Touch, event) {var target = Event.getcurrenttarget (); if (!target.containstouchlocation (Touch)) return false; Target._beginpos = Touch.getlocation (); Target._btouching = true; Target._bmoveing = false; if (target._contentnode.isrunning ()) target._contentnode.stopaction (target._runningaction); Turn on slide timing target._bbegincounttime = true; Target._timecount = 0; return true; }, _ontouchmoved:function (Touch, event) {//move, calculateOffset var target = Event.getcurrenttarget (); var getpoint = touch.getlocation (); var diffy = Getpoint.y-target._beginpos.y; Target._contentnode.y = Target._contentnode.y + Diffy; Target._beginpos = GetPoint; Target._diffycount = Target._diffycount + Diffy; Target._oncediffycount = Target._oncediffycount + Diffy; }, _ontouchended:function (Touch, event) {var target = Event.getcurrenttarget (); Target._btouching = false; Calculate the sliding calculation distance calculation speed if (/*math.abs (target._oncediffycount) > Target._distancecondition &&*/target._time Count < Target._timecondition) {if (target._contentnode.isrunning ()) Target._contentnode.stopaction (target . _runningaction); var distance = Math.Round (target._oncediffycount*target._velocity); var time = Target._timecount * target._velocity; var pn = distance > 0? 1:-1; Distance = Math.Abs (distance) > Math.Abs(Target._distancelimit)? PN * TARGET._DISTANCELIMIT:DISTANCE; Time = time < Target._timelimit? Target._timelimit:time; var move = Cc.moveby (time, 0, distance); Target._runningaction = Cc.sequence (move.easing (Cc.easesineout ()), Cc.callfunc (target._bouncebalance, target)); Target._runningaction = Target._contentnode.runaction (target._runningaction); Target._beginpos = Target._contentnode.getposition (); Target._bmoveing = true; }else{//If not moved, then do the balance target._bouncebalance directly (); } target._oncediffycount = 0; Target._timecount = 0; Target._bbegincounttime = false; }, Containstouchlocation:function (Touch) {var getpoint = touch.getlocation (); var myrect = This.getboundingbox (); Return Cc.rectcontainspoint (Myrect, GetPoint); }, _balance:function () {if (This._diffycount > This._list[0].height) {var topItem = This._list.pop ( ); TOPITEM.Y = This._list[0].y-this._list[0].height; This._list.unshift (TopItem); This._diffycount = This._diffycount-this._list[0].height; }else if (This._diffycount <-this._list[0].height) {var bottomitem = This._list.shift (); BOTTOMITEM.Y = This._list[this._list.length-1].y + this._list[this._list.length-1].height; This._list.push (Bottomitem); This._diffycount = This._diffycount + this._list[0].height; }}, _bouncebalance:function () {var itemhight = this._list[0].height; var num = Math.Round (this._contentnode.y%itemhight); var distance = 0; if (num > 0) {distance = num > itemhight/2? itemhight-num:-num; }else {distance = num >-itemhight/2?-num:-(itemhight + num); } var action = Cc.moveby (0.2, 0, distance). Easing (Cc.easesineout ()); This._contentnode.runaction (Cc.sequence (Action, Cc.callFunc (This._end, this))); }, _end:function () {var num = Math.Round (this._contentnode.y/this._list[0].height); var num2 = 1 * (num% this._list.length); if (num2 > 0) {this._currentitemindex = num2 + 2; }else if (num2 < 0) {This._currentitemindex = this._list.length + num2 + 2; }else{This._currentitemindex = 2; } if (This._currentitemindex > This._list.length) {this._currentitemindex = this._currentitemindex% This._list.length; } This._value = This._originlist[this._currentitemindex-1].getstring (); Cc.log (This._value); }});
----------------------------------
Because of the limit of sliding distance per second, the code has little efficiency problems in the mobile web platform.
On the native platform you can relax a bit and make the sliding more refreshing ~
Finally, because the younger brother knowledge level is limited, the code has all sorts of nonstandard places to hope that the great God many contain.
COCOS2D: Analog iOS Time picker