程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> NGUI實現滑動翻頁效果實例代碼

NGUI實現滑動翻頁效果實例代碼

編輯:關於C語言

廢話不多說了,直接給大家上干貨了。

具體代碼如下所示:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 using UnityEngine; using System.Collections; public class PageVIEw : MonoBehaviour { const int ITEM_NUM = 2; //總頁數 const int PAGE_WIDTH = 2048; //頁寬 const float DRAG_SPEED = 0.5f; //翻頁時間 const int DRAG_OFFECT = 30; //滑動的起點和終點的差需大於這個數才能觸發翻頁效果 float beganX = 0; float beganY = 0; //鼠標按下的坐標 int curIndex = 1; //當前頁數,默認為第一頁 bool isPlay = false; //是否正在翻頁 bool isPress = false; //鼠標是否按下 bool isPageFoot = false; //當前是否處於頁尾 bool isHomePage = true; //當前是否處於首頁 string left = "left"; //左滑動畫的name string right = "right"; //右滑動畫的name GameObject[] Item_Objects; // Use this for initialization void Start () { this.Init (); } void Init() { Item_Objects = new GameObject[ITEM_NUM]; for(int i = 1; i <= ITEM_NUM; ++i) { Transform trans = this.transform.FindChild("item" + i); if(trans) { GameObject spr = trans.transform.FindChild("Background").gameObject; spr.AddComponent<UIEventListener>(); UIEventListener.Get(spr.gameObject).onPress = OnPressEvent; } Item_Objects[i - 1] = trans.gameObject; } } //鼠標按下事件監聽 void OnPressEvent(GameObject obj,bool isDown) { float endX; float endY; if (isDown) { beganX = UICamera.lastTouchPosition.x; beganY = UICamera.lastTouchPosition.y; isPress = true; } else { endX = UICamera.lastTouchPosition.x; endY = UICamera.lastTouchPosition.y; if (isPress) { if(isPlay == false) { if(endX - beganX > DRAG_OFFECT) { if(isHomePage == false) { RightDrag(); } }else if(endX - beganX < DRAG_OFFECT){ if(isPageFoot == false) { LeftDrag(); } } } } isPress = false; } } //向左滑 void LeftDrag() { isPlay = true; float x = this.transform.localPosition.x - PAGE_WIDTH; TweenPosition leftTween = TweenPosition.Begin (this.gameObject,DRAG_SPEED,new Vector3(x,0,0)); leftTween.method = UITweener.Method.EaseInOut; leftTween.callWhenFinished = "callback"; leftTween.name = left; leftTween.Reset (); } //向右滑 void RightDrag() { isPlay = true; float x = this.transform.localPosition.x + PAGE_WIDTH; TweenPosition rightTween = TweenPosition.Begin (this.gameObject,DRAG_SPEED,new Vector3(x,0,0)); rightTween.method = UITweener.Method.EaseInOut; rightTween.callWhenFinished = "callback"; rightTween.name = right; rightTween.Reset (); } //動畫結束的回調函數 void callback(UITweener tween) { isPlay = false; if (tween.name == left) { curIndex ++; } else if (tween.name == right) { curIndex --; } if (curIndex == 1) { isHomePage = true; }else { isHomePage = false; } if(curIndex == ITEM_NUM){ isPageFoot = true; }else { isPageFoot = false; } } }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved