$Touch={}
$Touch.click=1//定义视为点击的距离,单位像素

$Touch.init=function(obj)
{
	//定义变量，用于记录坐标和角度
	var startx,starty,movex,movey,movex_,movey_,endx,endy,nx,ny,angle,over;
	var tag,_click;
	_click=obj.click?obj.click:$Touch.click;
	//获取DOM标签
	var box = $M.find(obj.id);
	if(!box)
	{
		$M.info('【触控监听】初始化错误',id)
	}

	function touchs(event)
	{
		event.preventDefault();
		if($M.unSwitch(event.type) =="touchstart")
		{
			tag=true;
			var touch = event.touches[0];
			startx = Math.floor(touch.pageX);
			starty = Math.floor(touch.pageY);
			movex=startx;
			movey=starty;
			if(obj.start)obj.start({startx:startx,starty:starty});
		}
		else if($M.unSwitch(event.type)=="touchmove")
		{
			var touch = event.touches[0];
			movex_=movex?movex:'';
			movey_=movey?movey:'';
			movex = Math.floor(touch.pageX);
			movey = Math.floor(touch.pageY);
			if(obj.move)
			{
				obj.move
				(
					{
						startx:startx,starty:starty,movex:movex,movey:movey,
						movex_:movex_,movey_:movey_
					}
				);
			}
		}
		else if($M.unSwitch(event.type) == "touchend" || $M.unSwitch(event.type) == "touchcancel")
		{
			if(!tag)return;
			tag=false;
			//获取最后的坐标位置
			endx = Math.floor(event.changedTouches[0].pageX);
			endy = Math.floor(event.changedTouches[0].pageY);
			//获取开始位置和离开位置的距离
			nx = endx-startx;
			ny = endy-starty;
			//通过坐标计算角度公式 Math.atan2(y,x)*180/Math.PI
			angle = Math.atan2(ny, nx) * 180 / Math.PI;
			over=
			{
				startx:startx,starty:starty,movex:movex,movey:movey,
				movex_:movex_,movey_:movey_,endx:endx,endy:endy
			}
			if(Math.abs(nx)<=_click && Math.abs(ny)<=_click)
			{
				$M.info('【事件】点击');
				if(obj.end)obj.end('click',over);
				return false;
			}
			//通过滑动的角度判断触摸的方向
			if(angle<45 && angle>=-45)
			{
				$M.info('【事件】右滑动');
				if(obj.end)obj.end('right',over);
				return false;
			}
			else if(angle<135 && angle>=45)
			{
				$M.info('【事件】下滑动');
				if(obj.end)obj.end('down',over);
				return false;
			}
			else if((angle<=180 && angle>=135) || (angle>=-180 && angle<-135 ))
			{
				$M.info('【事件】左滑动');
				if(obj.end)obj.end('left',over);
				return false;
			}
			else if(angle<=-45 && angle >=-135)
			{
				$M.info('【事件】上滑动');
				if(obj.end)obj.end('up',over);
				return false;
			}
		}
	}
	
	//添加触摸事件的监听，并直行自定义触摸函数
	box.listener('touchstart,touchmove,touchend,touchcancel',touchs);
}
