这个只是实现了双指缩放,至于单指挪动的,是用unity的SCrollView实现的,只需要单指的时候,打开ScrollView,双指的时候,关闭ScrollView就行,如果不关闭的话,ScrollView的平移会和C#中的逻辑产生冲突。TRACE.TraceLn这个是打印日志的,可以忽略。这个感觉还是可以讨论的,当初在网上搜的时候,也是都没有说的很具体,就只能自己摸索,自己尝试。
public class ZoomMap : UIWindow
{
public Image mOriginTexture;
public Image mBaseSize;
private Touch oldTouch1;
private Touch oldTouch2;
public ScrollRect m_scrollRect;
void Update()
{
if (Input.touchCount <= 1)
{
m_scrollRect.enabled = true;
return;
}
if (Input.touchCount >= 2)
{
m_scrollRect.enabled = false;
}
else
{
m_scrollRect.enabled = true;
}
Touch newTouch1 = Input.GetTouch(0);
Touch newTouch2 = Input.GetTouch(1);
if (newTouch2.phase == TouchPhase.Began)
{
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
TRACE.TraceLn("双指缩放地图,oldTouch2 = " + oldTouch2 + "oldTouch1 = " + oldTouch1);
return;
}
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
float offset = newDistance - oldDistance;
float scaleFactor = offset / 1000f;
Vector3 localScale = mOriginTexture.rectTransform.localScale;
Vector3 localSizeDelta = mBaseSize.rectTransform.sizeDelta;
float HWBase;
float localMapWidth = mOriginTexture.rectTransform.rect.width;
float localMapHight = mOriginTexture.rectTransform.rect.height;
float localShowAreaWidth = localSizeDelta.x + UIHelper.Instance.contentWidth;
float localShowAreaHight = localSizeDelta.y + UIHelper.Instance.contentHeight;
float RemovHMax = localShowAreaHight - localMapHight * 1.5f;
float RemovWMax = localShowAreaWidth - localMapWidth * 1.5f;
float BaseOne = localShowAreaWidth / localMapWidth;
float BaseTwo = localShowAreaHight / localMapHight;
float WidthMin = Math.Max(BaseOne,BaseTwo) - localScale.y;
float WidthMan = 1.5f - localScale.x;
if (offset > 0)
{
if (scaleFactor >= WidthMan)
{
scaleFactor = WidthMan;
}
}
if (offset < 0)
{
if (scaleFactor <= WidthMin)
{
scaleFactor = WidthMin;
}
}
ZoomRemove(localMapWidth, localMapHight, localShowAreaWidth, localShowAreaHight, localScale.x + scaleFactor);
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
public void ZoomRemove(float localMapWidth, float localMapHight, float localShowAreaWidth, float localShowAreaHight,float scale)
{
float Map_X = mOriginTexture.rectTransform.anchoredPosition.x;
float Map_Y = mOriginTexture.rectTransform.anchoredPosition.y;
bool IsRemove = false;
float Limit_X = (localMapWidth * scale * 0.5f) - localShowAreaWidth * 0.5f;
float Limit_Y = (localMapHight * scale * 0.5f) - localShowAreaHight * 0.5f;
if (Map_X < 0 && Map_Y < 0)
{
Limit_X = Limit_X * -1;
Limit_Y = Limit_Y * -1;
}
else if (Map_X > 0 && Map_Y < 0)
{
Limit_Y = Limit_Y * -1;
}
else if (Map_X < 0 && Map_Y > 0)
{
Limit_X = Limit_X * -1;
}
if (System.Math.Abs(Map_X) > System.Math.Abs(Limit_X))
{
Map_X = Limit_X;
IsRemove = true;
}
if (System.Math.Abs(Map_Y) > System.Math.Abs(Limit_Y))
{
Map_Y = Limit_Y;
IsRemove = true;
}
if (IsRemove)
{
Vector3 PosXY = new Vector3(Map_X, Map_Y, 0);
TRACE.TraceLn("双指缩放地图, scale = " + scale + " PosXY = " + PosXY);
TRACE.TraceLn("双指缩放地图,参数 localMapWidth = " + localMapWidth + "localMapHight = " + localMapHight);
TRACE.TraceLn("双指缩放地图,参数 localShowAreaWidth = " + localShowAreaWidth + "localShowAreaHight = " + localShowAreaHight);
TRACE.TraceLn("双指缩放地图,当前的距离 Map_X = " + Map_X + "Map_Y = " + Map_Y);
TRACE.TraceLn("双指缩放地图,卡边的极限距离 Limit_X = " + Limit_X + "Limit_Y = " + Limit_Y);
mOriginTexture.rectTransform.anchoredPosition = PosXY;
}
Vector3 Scale = new Vector3(scale, scale, scale);
mOriginTexture.rectTransform.localScale = Scale;
}
}
|