将图像从一个滚动视图拖到另一个滚动视图

时间:2012-04-03 11:44:04

标签: iphone uiscrollview uiimageview

有谁知道如何将ImageView从一个滚动视图拖动到另一个滚动视图? 我的代码是

-(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;
    }
}

1 个答案:

答案 0 :(得分:0)

更好的方法是继承您的图片视图类并实施 touchesBegan touchesMoved touchesEnded touchesCancelled 代表。

然后将subClass添加到您的UIScrollView.Make确保您的subClassed imageView对象“ UserIntractionEnbled ”属性设置为YES。

示例:

    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];
  }    
}

拖放,

请参阅链接iphone-how-to-drag-and-drop-images