一、 点击滑动页面

新建了一个带mask的prefab,加上代码只需要将图片prefab、按钮prefab和所想添加的图片

拖进去会自动生成按钮,滑动速度可以随意调time,滑动效果用itween实现的,所以需要加上itween插件

效果如下:(图片是我最爱的马路小天使(¯﹃¯))

附上代码

 using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI; public class Mask : MonoBehaviour { public List<Sprite> sprite = new List<Sprite>();
List<GameObject> image=new List<GameObject >();
public GameObject pic;
public Button but;
public float time;
float width, height;
int num;
void Start ()
{
num = sprite.Count;
width = this.gameObject.GetComponent<RectTransform>().rect.width;
height = this.gameObject.GetComponent<RectTransform>().rect.height;
pic.transform.GetComponent<RectTransform>().sizeDelta=new Vector2(width,height);
for (int i = ; i < num; i++)
{ GameObject p = Instantiate(pic,new Vector3(transform.position.x+i*width,transform.position.y,),transform.rotation)as GameObject;
p.transform.parent = this.gameObject.transform;
image.Add (p) ;
p.GetComponent<Image>().sprite = sprite[i];
Button b = Instantiate(but, new Vector3(transform.position.x- ( * num - ) / +*i, transform.position.y - height/ + , ), transform.rotation) as Button;
b.transform.parent = GameObject.FindWithTag("Button").transform;
System.Object obj = (System.Object)i;
b.onClick.AddListener(delegate(){this.MoveToPic((int)obj);});
} }
void OnGUI()
{
if (GUI.Button(new Rect(transform.position.x + + width / , Screen.height - transform.position.y - , , ), ">"))
{
if (image[].transform.position.x < transform.position.x-)
{
Move();
}
}
if (GUI.Button(new Rect(transform.position.x -width/-,Screen.height- transform.position.y- , , ), "<"))
{
if (image[num - ].transform.position.x > transform.position.x+)
{
Move(-);
}
}
}
public void Move(int dir)
{
for (int i = ; i <num; i++)
{
iTween.MoveAdd(image[i], iTween.Hash("x", width * dir, "time", time));
}
}
public void MoveToPic(int i)
{ float offset = transform.position.x - image[i].transform.position.x;
for (int j = ; j < num; j++)
{
iTween.MoveAdd(image[j], iTween.Hash("x", offset, "time", time));
}
} }

二、间隔时间自动滑动,也可点击滑动

 using UnityEngine;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System; public class ScrollPage : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
ScrollRect rect;
//页面:0,1,2,3 索引从0开始
//每页占的比列:0/3=0 1/3=0.333 2/3=0.6666 3/3=1
//float[] pages = { 0f, 0.333f, 0.6666f, 1f };
List<float> pages = new List<float>();
int currentPageIndex = -; //滑动速度
public float smooting = ; //滑动的起始坐标
float targethorizontal = ; //是否拖拽结束 0 -结束 1-拖拽中 2-结束超过5s
int isDrag = ;
int curindex = ;
int dir = ;
float timecount = ;
/// <summary>
/// 用于返回一个页码,-1说明page的数据为0
/// </summary>
public System.Action<int,int> OnPageChanged; float startime = 0f;
float delay = 0.1f; // Use this for initialization
void Start()
{
rect = transform.GetComponent<ScrollRect>();
//rect.horizontalNormalizedPosition = 0;
//UpdatePages();
startime = Time.time;
} void Update()
{
if (Time.time < startime + delay) return;
UpdatePages();
//如果不判断。当在拖拽的时候要也会执行插值,所以会出现闪烁的效果
//这里只要在拖动结束的时候。在进行插值
if (isDrag == && pages.Count > )
{
rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, Time.deltaTime * smooting);
if (Mathf.Abs(rect.horizontalNormalizedPosition - targethorizontal) <= 0.001)
{ isDrag = ; timecount = ; }
}
if (isDrag == )
{
timecount += Time.deltaTime;
if (timecount > )
{
AutoMove();
rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, pages[curindex + dir * ], Time.deltaTime * smooting);
if (pages == null || OnPageChanged == null)
print("Error" + "," + (pages==null) + "," + (OnPageChanged == null));
OnPageChanged(pages.Count, curindex + dir * );
if (Mathf.Abs(rect.horizontalNormalizedPosition - pages[curindex + dir * ]) < 0.001)
{
timecount = ;
}
}
}
} public void AutoMove()
{
for (int i = ; i < pages.Count; i++)
{
if (Mathf.Abs(pages[i] - rect.horizontalNormalizedPosition) < 0.001)
{
curindex = i;
}
}
if (curindex == pages.Count - ) { dir = -; }
if (curindex == ) { dir = ; }
}
public void OnBeginDrag(PointerEventData eventData)
{
isDrag = ; }
public void MoveToPage(int target)
{
isDrag = ;
targethorizontal = pages [target];
if(target!=currentPageIndex)
{
currentPageIndex = target;
OnPageChanged(pages.Count, currentPageIndex);
}
}
public void OnEndDrag(PointerEventData eventData)
{
isDrag = ; float posX = rect.horizontalNormalizedPosition;
int index = ;
//假设离第一位最近
float offset = Mathf.Abs(pages[index] - posX);
for (int i = ; i < pages.Count; i++)
{
float temp = Mathf.Abs(pages[i] - posX);
if (temp < offset)
{
index = i; //保存当前的偏移量
//如果到最后一页。反翻页。所以要保存该值,
offset = temp;
}
} if(index!=currentPageIndex)
{
currentPageIndex = index;
OnPageChanged(pages.Count, currentPageIndex);
} targethorizontal = pages[index];
} void UpdatePages()
{
// 获取子对象的数量
int count = this.rect.content.childCount;
int temp = ;
for(int i=; i<count; i++)
{
if(this.rect.content.GetChild(i).gameObject.activeSelf)
{
temp++;
}
}
count = temp; if (pages.Count!=count)
{
if (count != )
{
pages.Clear();
for (int i = ; i < count; i++)
{
float page = ;
if(count!=)
page = i / ((float)(count - ));
pages.Add(page);
}
}
OnEndDrag(null);
}
}
}

unity3d之实现各种滑动效果的更多相关文章

  1. a 锚点跳转滑动效果

    点击a链接时,跳转到相应id的位置处,有一个滑动效果. <a href="#my">我是跳转到div</a><div id="my" ...

  2. Android Scroll分析——滑动效果产生

    相对于在Android2.x版本上出现的长按.点击事件的效果,不得不说,滑动操作具有更好的用户体验.因此,从Android 4.X版本开始,出现了更多滑动操作的效果.越来越多第三方应用模仿这样的效果, ...

  3. jquery左右滑动效果的实现

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. bootstrap实现 手机端滑动效果,滑动到下一页,jgestures.js插件

    bootstrap能否实现 手机端滑动效果,滑动到下一页 jgestures.js插件可以解决,只需要引入一个JS文件<script src="js/jgestures.min.js& ...

  5. Android实现多页左右滑动效果,支持子view动态创建和cache

    要实现多页滑动效果,主要是需要处理onTouchEvent和onInterceptTouchEvent,要处理好touch事件的子控件和父控件的传递问题. 滚动控制可以利用android的Scroll ...

  6. 【Android 界面效果27】利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果

    本文主要介绍如何利用ViewPager.Fragment.PagerTabStrip实现多页面滑动效果.即google play首页.新浪微博消息(at.评论.私信.广播)页面的效果.ViewPage ...

  7. 十六、Android 滑动效果汇总

    Android 滑动效果入门篇(一)—— ViewFlipper Android 滑动效果入门篇(二)—— Gallery Android 滑动效果基础篇(三)—— Gallery仿图像集浏览 And ...

  8. Android 滑动效果进阶篇(六)—— 倒影效果

    上篇介绍了使用Animation实现3D动画旋转翻页效果,现在介绍图片倒影实现,先看效果图 本示例主要通过自定义Gallery和ImageAdapter(继承自BaseAdapter)实现 1.倒影绘 ...

  9. Android 滑动效果入门篇(二)—— Gallery

    Gallery 是Android官方提供的一个View容器类,继承于AbsSpinner类,用于实现页面滑动效果. 从上面的继承关系可以看出,AbsSpinner类继承自AdapterView,因此我 ...

随机推荐

  1. 求二叉树第K层的节点个数+求二叉树叶子节点的个数

    size_t _FindLeafSize(Node* root)     //求二叉树叶子节点的个数    {        //static size_t count = 0;        if ...

  2. c++primer 学习笔记

    1.1 编写简单的c++程序 函数4元素:函数类型.函数名.形参表.函数体 调用GNU(UNIX) g++ prog1.cc -o prog1 //生成可执行文件prog1,UNIX下默认a.out ...

  3. 【医学影像】《Dermatologist-level classification of skin cancer with deep neural networks》论文笔记

    这是一篇关于皮肤癌分类的文章,核心就是分类器,由斯坦福大学团队发表,居然发到了nature上,让我惊讶又佩服,虽然在方法上没什么大的创新,但是论文本身的工作却意义重大,并且这篇17年见刊的文章,引用量 ...

  4. 基础篇:6.9)GD&T较线性尺寸公差的优缺点

    本章目的:理解GD&T标注对比线性/传统/坐标尺寸公差的优势,但也不要忘记其使用限制. 1.线性尺寸公差   1.1 定义 线性尺寸公差=传统尺寸公差=坐标尺寸公差. 传统尺寸公差(Tradi ...

  5. 下载Kitti 数据集(dataset) data_road.zip

    官网下载http://www.cvlibs.net/download.php?file=data_road.zip,耗时近3小时,虽然只有几百兆. 但是,我坚持下来了. 保存到了百度网盘,以供国内用户 ...

  6. touch插件

    第一种: <script> (function($) { var options, Events, Touch; options = { x: 20, y: 20 }; Events = ...

  7. Robot Framework(AutoItLibrary库关键字介绍)

    AutoItLibrary库关键字 AutoItLibrary 的对象操作大体上有几大主要部分,Window 操作.Control 操作.Mouse 操作.Process操作.Run 操作.Reg 操 ...

  8. ios UISearchDisplayController 实现 UITableView 搜索功能

    UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类. 里面内置了一个 UITableView 用于显示搜索的结果.它可以和一个需要搜索功能的 co ...

  9. android webview 中 js 模板引擎的使用

    最近在项目中要求用 webview 展示几个界面, 而后台返回的不是 html 而是 json 数据. 起初用 StringBuilder 一个一个拼 html, 后来感觉太繁琐,拼一个还行,拼多了就 ...

  10. 【linux】虚拟机内装Linux系统的ssh访问

    一般在虚拟机内安装一个Linux系统,虚拟机网络设置为桥接后,Linux系统会在安装的过程中自动设置其为dhcp配置,会给其随机分配一个ip,这个ip可以用命令 "ifconfig" ...