简体   繁体   中英

Drag an image from one scrollview to another

Does anyone know how to drag ImageView from one scrollview to another scroll view? my code is

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.fPoint = [[[event allTouches] anyObject] locationInView:self.rootViewContoller.view];
    self.rootViewContoller.taskScroller.fPoint=fPoint;
    if ([[self superview] isKindOfClass:[TaskScroller class]])
    {
        self.rootViewContoller.taskScroller.scrollEnabled=FALSE;
        if ([callerDelegate respondsToSelector:@selector(resizeFrameForView:)])
        {
            [callerDelegate resizeFrameForView:self];
        } 
    }
    else if([[self superview] isKindOfClass:[MyScrollView class]])
    {
        self.rootViewContoller.myScrollView.scrollEnabled=FALSE;
        if ([callerDelegate respondsToSelector:@selector(resizeFrameForView:)])
        {
            [callerDelegate resizeFrameForView:self];
        } 
    }
    else if([[self superview] isKindOfClass:[UnusedTaskView class]])
    {
        self.rootViewContoller.unusedTaskView.scrollEnabled=FALSE;
        if ([callerDelegate respondsToSelector:@selector(resizeFrameForView:)])
        {
            [callerDelegate resizeFrameForView:self];
        } 
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.rootViewContoller.view];
    self.rootViewContoller.taskScroller.endPoint=point;
    if ((point.y-(self.frame.size.height/2)>70) && (point.x)>30 && (point.y)<440) 
    {
        if ((self.frame.origin.x>=255) && (point.y<400) )
        {
            [self.rootViewContoller.myScrollView setContentOffset:CGPointMake(self.rootViewContoller.myScrollView.contentOffset.x+10, 0)];
        }
        else if ((self.frame.origin.x<=70) && (point.y<400) )
        {
            [self.rootViewContoller.myScrollView setContentOffset:CGPointMake(self.rootViewContoller.myScrollView.contentOffset.x-10, 0)];
        }
        self.center=CGPointMake(point.x, point.y);
        [self.rootViewContoller.view bringSubviewToFront:self];
    }
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint endPoint = [[[event allTouches] anyObject] locationInView:self.rootViewContoller.view];
    if ([callerDelegate respondsToSelector:@selector(putBackThisView:)] ) 
    {
        [callerDelegate putBackThisView:endPoint];
    }
    if ([[self superview] isKindOfClass:[MyScrollView class]])
    {
       // NSLog(@"touchesEnded MyScrollView");
        if (endPoint.y<420 && !self.rootViewContoller.taskScroller.scrollEnabled)
        {
            [self.rootViewContoller.taskScroller ReDigin:self.tag];
        }
        else if(endPoint.x>80 && !self.rootViewContoller.unusedTaskView.scrollEnabled)
        {
            [self.rootViewContoller.unusedTaskView rearrangeTheList];
        }
        self.rootViewContoller.taskScroller.scrollEnabled=TRUE;
        self.rootViewContoller.unusedTaskView.scrollEnabled=TRUE;
    }
    if ([[self superview] isKindOfClass:[UnusedTaskView class]])
    {
       // NSLog(@"touchesEnded UnusedTaskView");
        if (endPoint.y<420 && !self.rootViewContoller.taskScroller.scrollEnabled)
        {
            [self.rootViewContoller.taskScroller ReDigin:self.tag];
        }
        self.rootViewContoller.myScrollView.scrollEnabled=TRUE;
        self.rootViewContoller.taskScroller.scrollEnabled=TRUE;
    }
    if ([[self superview] isKindOfClass:[TaskScroller class]])
    {
        //NSLog(@"touchesEnded TaskScroller");
        if(endPoint.x>80 && !self.rootViewContoller.unusedTaskView.scrollEnabled)
        {
            [self.rootViewContoller.unusedTaskView rearrangeTheList];
        }
        self.rootViewContoller.unusedTaskView.scrollEnabled=TRUE;
        self.rootViewContoller.myScrollView.scrollEnabled=TRUE;
    }
    if ((endPoint.x>80)&&(endPoint.x<420))
    {
       // NSLog(@"(endPoint.x>80)&&(endPoint.x<420)");
        self.rootViewContoller.myScrollView.scrollEnabled=TRUE;
    }
}

The better way is subclass your image-view class and implement the touchesBegan , touchesMoved , touchesEnded , touchesCancelled delegates.

Then Add the subClass to your UIScrollView.Make sure that your subClassed imageView object " UserIntractionEnbled " property set to YES.

Example:

    DragAndDropImage *dragAndDropImage = [[DragAndDropImage alloc] initWithFrame:urFrame];

    [dragAndDropImage initScroller2:scroller2]; 

   [dragAndDropImage.userIntractionEnabled:YES];

    [YourScrollView.addSubView:dragAndDropImage];

DragAndDropImage.h

@interface DragAndDropImage : UIImageView {

  UIScrollView *childScroller2;

} 

@end

DragAndDropImage.m

 @implementation DragAndDropImage



     -(void)initScroller2:(UIScrollView *)scroller2{

        childScroller2 = [scroller2 retain];
     }

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

      //do the code here

    }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

        //do the code here
    }


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

        UITouch *touch = [touches anyObject];       

   CGPoint dragEndpoint = [touch locationInView:childScroller2];        

   CGRect VisibleRect;
   VisibleRect.origin = childScroller2.contentOffset;
   VisibleRect.size = childScroller2.bounds.size;

 CGFloat scale = (CGFloat) 1.0 /childScroller2.zoomScale;
 if (isless(scale,1.0)) {

     VisibleRect.origin.x *= scale;
     VisibleRect.origin.y *= scale;
     VisibleRect.size.width *= scale;
     VisibleRect.size.height *= scale;
 }

if(CGRectContainsPoint([self frame], dragEndpoint) && CGRectContainsPoint(VisibleRect,self.center))
{
        [childScroller2.addSubView:self];
  }    
}

For Drag and Drop,

Please refer the link iphone-how-to-drag-and-drop-images

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM