0

在我的项目中,我在底部滚动,在顶部滚动表格。在滚动视图中,我添加了 10 个按钮。对于每个按钮,我都有平移手势以在屏幕上移动。当按钮与表格相交时,将在表格视图中添加一张图片。

但是平底锅正在滚动中工作。如何使它在视图中移动

我的编码是:

downscroll=[[UIScrollView alloc]initWithFrame:CGRectMake(24, 635, 980, 100)];
downscroll.backgroundColor=[UIColor redColor];
downscroll.contentSize=CGSizeMake(990, 100);

[self.view addSubview:downscroll];

for(int i=1;i<=8;i++)
{
        b1=[UIButton buttonWithType:UIButtonTypeCustom];
        b1.frame=CGRectMake(30+px, 0, 80, 80);
       [b1 setImage:[UIImage imageNamed: [NSString stringWithFormat:@"Icon%i.png",i]]      forState:UIControlStateNormal];
     [downscroll addSubview:b1];
    // [self.view sendSubviewToBack:b1];
   // [self.view bringSubviewToFront:b1];

    [groupbutton addObject:b1];
     panRecognizer3= [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(move:)];
    [b1 setUserInteractionEnabled:YES];
    b1.exclusiveTouch=YES;
    img.tag=i;
    [b1 addGestureRecognizer:panRecognizer3];

    px=px+120;
}

当我在没有滚动滚动视图的情况下直接添加按钮时,它工作正常。但我需要添加按钮滚动视图,请帮助提供正确的代码。提前致谢。

4

1 回答 1

1

这是我在应用程序中使用的方式在我的代码中,我按照以下步骤操作

  1. 在 UIPanGestureRecognizer 起点中,我将按钮从 scrollView 添加到 self.view
  2. 使用translationInView方法 拖动按钮

3.在 UIPanGestureRecognizer 结束状态下,我将检查按钮是否掉落在目标视图中。如果是,您需要执行其他任务,否则将按钮添加到滚动视图中的相同位置

`MoveControl` is a UIPanGestureRecognizer method



- (void)MoveControl:(UIPanGestureRecognizer *)recognizer
    {
        UIButton *vew=(UIButton *)[recognizer view];
        CGPoint newCenter = [recognizer translationInView:self.view];
        if (recognizer.state==UIGestureRecognizerStateBegan) {
            CGPoint point=vew.frame.origin;
            [vew setTitle:NSStringFromCGPoint(point) forState:UIControlStateSelected];
            CGRect rect=[self.view convertRect:[vew frame] fromView:[vew superview]];
            [vew setFrame:rect];
            [self.view addSubview:vew];
            CGPoint point1=vew.frame.origin;
            [vew setTitle:NSStringFromCGPoint(point1) forState:UIControlStateDisabled];
        }
        else
        {
            CGPoint oldcentre= CGPointFromString([vew titleForState:UIControlStateDisabled]);
            CGRect rect=vew.frame;
            CGPoint  origin=rect.origin;
            origin.x=(newCenter.x+oldcentre.x);
            origin.y=(newCenter.y+oldcentre.y);
            rect.origin=origin;
            vew.frame=rect;
            if (CGRectIntersectsRect(rect, [pageOriginalContainer frame])) {
                [YourTable setBackgroundColor:[UIColor lightGrayColor]];//Notifying that the tableView will accept the icon
            }
            else
            {
                [YourTable setBackgroundColor:[UIColor clearColor]];
            }
        }
        if (recognizer.state==UIGestureRecognizerStateEnded)
        {
            CGRect rect=[vew frame];
            if (CGRectIntersectsRect(rect, [pageOriginalContainer frame])) {
                  //your method of adding the Image to table
            }
            else//else part is means for if user dropped dragging somewhere else other than Table
            {
                CGPoint point=CGPointFromString([vew titleForState:UIControlStateSelected]);
                CGRect frame=vew.frame;
                frame.origin=point;
                vew.frame=frame;
                [pageCopyContainer addSubview:vew];
    //            [NSFileManager defaultManager]
            }
        }
    }
于 2013-02-26T07:22:20.957 回答