1

我有 UIScrollView 包含许多 UIImageViews。我需要将 UIImage 从 UIScrollView 拖到另一个视图。但是我的代码仅在 UIScrollView 中拖动。我无法将图像拖动到 scrollView 之外。请帮助我..我的代码是..

for (int i = 0; i < [myArray count]; i++) {
  image = [[UIImageView alloc]initWithFrame:CGRectMake(0, y, 75, 30)];
  image.userInteractionEnabled = YES;
  image.backgroundColor = [UIColor blueColor];

  UIPanGestureRecognizer *panRecg = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(labelDragged:)];
  [image addGestureRecognizer:panRecg];
  y=y+35;

  [gScrollView addSubview:image];
}

-(void)labelDragged:(UIPanGestureRecognizer *)recognizer {
  UIImageView *imageView = (UIImageView *)recognizer.view;
  CGPoint newCenter = [recognizer translationInView:self.view];

  if([recognizer state] == UIGestureRecognizerStateBegan) {     
    beginX = imageView.center.x; 
    beginY = imageView.center.y;
  }

  newCenter = CGPointMake(beginX + newCenter.x, beginY + newCenter.y);

  [imageView setCenter:newCenter];
}
4

3 回答 3

2

几点观察:

  1. 将其拖出滚动视图时,您希望将其从滚动视图中移除并将其添加到例如self.view. 然后,您将addSubview相应地调整坐标:

    newCenter = [self.view convertPoint:newCenter fromView:imageView.superview];
    [self.view addSubview:imageView];
    imageView.center = newCenter;
    
  2. 确保clipsToBounds将滚动视图设置为 NO,以便在将其拖出时可以继续看到它。

  3. 请注意,我已将其更改为引用imageView.superview而不是滚动视图。如果您在其中保留滚动视图引用,则必须添加条件检查以确保图像视图尚未从滚动视图中移出。

于 2013-02-26T15:47:16.060 回答
1

这是拖动的方法

-(void)labelDragged:(UIPanGestureRecognizer *)recognizer {

{
        UIImageView *imageView=(UIButton *)[recognizer view];
        CGPoint newCenter = [recognizer translationInView:self.view];
        if (recognizer.state==UIGestureRecognizerStateBegan) {
            CGRect rect=[self.view convertRect:[imageView frame] fromView:[imageView superview]];
            beginX = imageView.center.x; 
            beginY = imageView.center.y;
            [vew setFrame:rect];
            [self.view addSubview:vew];
        }
        else
        {
            CGRect rect=imageView.frame;
            newCenter.x=(newCenter.x+Begin.x);
            newCenter.y=(newCenter.y+Begin.y);
            imageView.center=newCenter;
            if (CGRectIntersectsRect(rect, [AcceptingView frame])) {
                [AcceptingView setBackgroundColor:[UIColor lightGrayColor]];//Notifying that the AcceptingView will accept the icon
            }
            else
            {
                //change back to old color
            }
        }
        if (recognizer.state==UIGestureRecognizerStateEnded)
        {
            CGRect rect=[imageView frame];
            if (CGRectIntersectsRect(rect, [AcceptingView frame])) {
                  //your method of adding the Image to acceptView
                 CGRect rect=[acceptView bounds];
                 rect.size=imageView.frame.size;
                 imageView.frame=rect;
                 [AcceptingView addSubView:imageView];
            }
            else//else part is means for if user drop the dragged view somewhere else other than AcceptingView
            {
                   //add image back to scrollView
            }
        }
    }
于 2013-02-26T15:51:49.847 回答
1

我在评论中有几个 UI 问题要问你,但@Rob 是正确的,让视图在层次结构中移动的关键是在层次结构中实际移动它。当拖动开始...

CGRect newFrame = [gScrollView convertRect:imageView.frame toView:self.view];
imageView.frame = newFrame;
[self.view addSubview:imageView];

手势识别器应该保持不变。

于 2013-02-26T15:46:13.483 回答