/** override functions */ public virtual bool TouchBegan(CCTouch pTouch, CCEvent touchEvent) { if (!Visible) { return(false); } var frame = ViewRect; //dispatcher does not know about clipping. reject touches outside visible bounds. if (touches.Count > 2 || IsTouchMoved || !frame.ContainsPoint(container.Layer.ScreenToWorldspace(pTouch.LocationOnScreen))) { return(false); } if (!touches.Contains(pTouch)) { touches.Add(pTouch); } if (touches.Count == 1) { // scrolling touchPoint = Layer.ScreenToWorldspace(pTouch.LocationOnScreen); IsTouchMoved = false; Dragging = true; //Dragging started scrollDistance = CCPoint.Zero; touchLength = 0.0f; } else if (touches.Count == 2) { touchPoint = CCPoint.Midpoint(Layer.ScreenToWorldspace(touches[0].LocationOnScreen), Layer.ScreenToWorldspace(touches[1].LocationOnScreen)); touchLength = CCPoint.Distance(container.Layer.ScreenToWorldspace(touches[0].LocationOnScreen), container.Layer.ScreenToWorldspace(touches[1].LocationOnScreen)); Dragging = false; } return(true); }
public virtual void TouchMoved(CCTouch touch, CCEvent touchEvent) { if (!Visible) { return; } if (touches.Contains(touch)) { if (touches.Count == 1 && Dragging) { // scrolling CCPoint moveDistance, newPoint; //, maxInset, minInset; float newX, newY; var frame = ViewRect; newPoint = Layer.ScreenToWorldspace(touches[0].LocationOnScreen); moveDistance = newPoint - touchPoint; float dis = 0.0f; if (Direction == CCScrollViewDirection.Vertical) { dis = moveDistance.Y; } else if (Direction == CCScrollViewDirection.Horizontal) { dis = moveDistance.X; } else { dis = (float)Math.Sqrt(moveDistance.X * moveDistance.X + moveDistance.Y * moveDistance.Y); } if (!IsTouchMoved && Math.Abs(ConvertDistanceFromPointToInch(dis)) < MOVE_INCH) { //CCLOG("Invalid movement, distance = [%f, %f], disInch = %f", moveDistance.x, moveDistance.y); return; } if (!IsTouchMoved) { moveDistance = CCPoint.Zero; } touchPoint = newPoint; IsTouchMoved = true; if (frame.ContainsPoint(touchPoint)) { switch (Direction) { case CCScrollViewDirection.Vertical: moveDistance = new CCPoint(0.0f, moveDistance.Y); break; case CCScrollViewDirection.Horizontal: moveDistance = new CCPoint(moveDistance.X, 0.0f); break; default: break; } newX = container.Position.X + moveDistance.X; newY = container.Position.Y + moveDistance.Y; scrollDistance = moveDistance; SetContentOffset(new CCPoint(newX, newY)); } } else if (touches.Count == 2 && !Dragging) { float len = CCPoint.Distance(Layer.ScreenToWorldspace(touches[0].LocationOnScreen), Layer.ScreenToWorldspace(touches[1].LocationOnScreen)); ZoomScale = ZoomScale * len / touchLength; } } }