【问题标题】:Drag an image from one scrollview to another将图像从一个滚动视图拖到另一个
【发布时间】:2012-04-03 11:44:03
【问题描述】:

有谁知道如何将 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;
    }
}

【问题讨论】:

  • 您需要提供更多详细信息。我们在这里查看您的代码中的哪些内容?

标签: iphone uiscrollview uiimageview


【解决方案1】:

更好的方法是子类化您的图像视图类并实现 touchesBegantouchesMovedtouchesEndedtouchesCancelled 代表。

然后将子类添加到您的 UIScrollView。确保您的子类 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多