2012-03-15 3 views
0

사용자가 ImageView를 오래 누르고 다른 곳으로 이미지를 드래그 할 수있게 해주는 UILongPressGestureRecognizer를 구현하고 있습니다. 길게 누르기가 시작되면 드래그가 준비되었음을 나타내도록 사용자가 뷰를 축척하고 사용자가 드래그 할 수 있습니다. 선택기가 변경 상태로 호출 될 때마다 ImageViews 프레임을 업데이트하면됩니다. 이것은 완벽하게 작동하지만 뷰의 복사본을 만들어 시작 상태의 부모 뷰에 추가 한 다음 부모 뷰에서 복사본의 프레임을 계속 업데이트하려고합니다. scrollview에서 끌기에 대한 문제를 없애기 위해이 작업을 수행하려고합니다. 하지만 HandleChangeState 메서드에서 올바른 논리를 얻을 수없는 것 같습니다. 복사본이 제대로 드래그되지 않습니다.UILongPressGestureRecognizer를 사용하여 UIImageView의 끌어서 놓기 구현

아이디어가 있으십니까?

일부 코드는 UILabel 및 UIImageView에 대한보다 일반적인 UILongPressGestureRecognizer를 만들려고하기 때문에 거기에 있습니다. 그러나이 문제와 관련이 없습니다.

public class LearningViewLongPressGestureRecognizer : UILongPressGestureRecognizer 
    { 
     private UIImageView draggedImageView; 
     private UILabel draggedLabel; 
     private RectangleF originalFrame;  
     private UIView parentView; 
     private DRAG_CONTENT_TYPE contentType; 

     public enum DRAG_CONTENT_TYPE 
     { 
      UIIMAGEVIEW, 
      UILabel 
     } 

     public static Selector LongPressSelector 
     { 
      get 
      { 
       return new Selector ("HandleLongPress"); 
      } 
     } 

     public LearningViewLongPressGestureRecognizer (UIView parent, DRAG_CONTENT_TYPE contentType) 
     { 
      this.contentType = contentType; 
      this.AddTarget(this, LongPressSelector); 
      this.Delegate = new LongPressRecognizerDelegate(); 
      this.parentView = parent; 
     } 

     [Export("HandleLongPress")] 
     public void HandleLongPress(UILongPressGestureRecognizer recognizer) 
     { 
      Console.WriteLine ("HandleLongPress Called."); 
      switch (recognizer.State) 
      { 
      case UIGestureRecognizerState.Began: 
       Console.WriteLine ("HandleLongPress Began."); 
       HandleBeginState(recognizer); 
       break; 
      case UIGestureRecognizerState.Changed: 
       Console.WriteLine ("HandleLongPress Changed."); 
       HandleChangedState(recognizer); 
       break; 
      case UIGestureRecognizerState.Cancelled: 
       Console.WriteLine ("HandleLongPress Canceled."); 
       break; 
      case UIGestureRecognizerState.Ended: 
       Console.WriteLine ("HandleLongPress Ended."); 
       break; 
      default: 
       Console.WriteLine ("HandleLongPress Default."); 
       break; 
      } 
     } 

     private void HandleBeginState(UILongPressGestureRecognizer recognizer) 
     { 
      this.originalFrame = this.View.Frame;      

      if (contentType == DRAG_CONTENT_TYPE.UIIMAGEVIEW) 
      { 
       draggedImageView = new UIImageView(); 
       draggedImageView.Image = ((UIImageView)this.View).Image;  
       draggedImageView.Frame = this.View.Frame; 

       //Change the view scale to indicate to the user that the view is selected and ready for dragging. 
       draggedImageView.Transform *= CGAffineTransform.MakeScale(1.1f, 1.1f); 

       parentView.AddSubview(draggedImageView); 
       parentView.BringSubviewToFront(draggedImageView);     
      }        
     } 

     private void HandleChangedState(UILongPressGestureRecognizer recognizer) 
     {   
      PointF currentLocationOfTouchInParentView = recognizer.LocationInView(draggedImageView);//recognizer.TranslationInView(this.View); 
      PointF deltaFromOriginal = new PointF(currentLocationOfTouchInParentView.X - originalFrame.X, currentLocationOfTouchInParentView.Y - originalFrame.Y); 

      RectangleF newFrame = draggedImageView.Frame; 
      newFrame.Offset(deltaFromOriginal.X, deltaFromOriginal.Y); 
      draggedImageView.Frame = newFrame; 
     } 

     private void HandleCanceledState(UILongPressGestureRecognizer recognizer) 
     { 

     } 

     private void HandleEndedState(UILongPressGestureRecognizer recognizer) 
     { 

     } 

     protected class LongPressRecognizerDelegate : UIGestureRecognizerDelegate 
     { 
      public override bool ShouldReceiveTouch (UIGestureRecognizer recognizer, UITouch touch) 
      { 
       return true; 
      } 
     } 
    } 

답변

0

해결되지 않은 문제가 해결되었습니다. 제스처를 시작한 원래 터치 위치의 델타와 상위 뷰 내의 현재 터치 위치를 가져와 그 오프셋을 드래그 한 뷰 프레임에 추가해야했습니다. 아래 내 솔루션입니다.

private void HandleBeginState(UILongPressGestureRecognizer recognizer) 
     { 
      this.originalFrame = this.View.Frame;      
      this.originalTouchLocation = recognizer.LocationInView(parentView); 

      if (contentType == DRAG_CONTENT_TYPE.UIIMAGEVIEW) 
      { 
       draggedImageView = new UIImageView(); 
       draggedImageView.Image = ((UIImageView)this.View).Image;  
       draggedImageView.Frame = new RectangleF(originalFrame.X, originalFrame.Y, originalFrame.Size.Width, originalFrame.Size.Height); 

       parentView.AddSubview(draggedImageView); 
       parentView.BringSubviewToFront(draggedImageView); 

       //Change the view scale to indicate to the user that the view is selected and ready for dragging. 
       draggedImageView.Transform *= CGAffineTransform.MakeScale(1.1f, 1.1f); 
      }        
     } 

     private void HandleChangedState(UILongPressGestureRecognizer recognizer) 
     {   
      PointF currentLocationOfTouchInParentView = recognizer.LocationInView(parentView); 
      PointF deltaFromOriginalTouch = new PointF(currentLocationOfTouchInParentView.X - originalTouchLocation.X, currentLocationOfTouchInParentView.Y - originalTouchLocation.Y); 

      RectangleF newFrame = originalFrame; 
      newFrame.Offset(deltaFromOriginalTouch.X, deltaFromOriginalTouch.Y); 
      draggedImageView.Frame = newFrame;   
     } 
+0

어떤 SDK를 사용하고 있습니까? – Oliver