2012-04-03 208 views
1

有誰知道如何將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; 
    } 
} 
+0

您需要提供更多詳細信息。我們在你的代碼中看到了什麼? – 2012-04-03 14:17:11

回答

0

更好的方法是子圖像視圖類,並實現的touchesBegantouchesMovedtouchesEndedtouchesCancelled代表。

然後將子類添加到您的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