NGUI實現(xiàn)滑動翻頁效果實例代碼
廢話不多說了,直接給大家上干貨了。
具體代碼如下所示:
using UnityEngine;
using System.Collections;
public class PageView : MonoBehaviour
{
const int ITEM_NUM = 2; //總頁數(shù)
const int PAGE_WIDTH = 2048; //頁寬
const float DRAG_SPEED = 0.5f; //翻頁時間
const int DRAG_OFFECT = 30; //滑動的起點和終點的差需大于這個數(shù)才能觸發(fā)翻頁效果
float beganX = 0;
float beganY = 0; //鼠標按下的坐標
int curIndex = 1; //當前頁數(shù),默認為第一頁
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;
}
}
//鼠標按下事件監(jiān)聽
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 ();
}
//動畫結(jié)束的回調(diào)函數(shù)
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;
}
}
}
代碼到此結(jié)束了,如果大家對代碼有疑問歡迎給我留言,小編會及時和大家取得聯(lián)系的。同時也非常感謝大家對我們網(wǎng)站的支持!
上一篇:Windows系統(tǒng)中使用C#編寫藍牙通信程序的簡單實例
欄 目:C#教程
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6584.html
您可能感興趣的文章
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實現(xiàn)多線程下載文件的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實現(xiàn)遠程關(guān)閉計算機或重啟計算機的方法
- 01-10C#自定義簽名章實現(xiàn)方法
- 01-10C#文件斷點續(xù)傳實現(xiàn)方法
- 01-10winform實現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改


