在UIScrollview中为UIButton添加UIPangesture以移动所有视图

时间:2013-02-26 05:24:20

标签: objective-c xcode uiscrollview uipangesturerecognizer uiscrollviewdelegate

在我的项目中,我在底部滚动,桌面在顶部。 在scrollview里面我添加了10个按钮。对于每个按钮,我都有平移手势在屏幕上移动。当按钮与桌子相交时,将在tableview中添加图片。

但是pan正在scroll.how中工作以使其在视图上移动

我的编码是:

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

当我直接添加我的按钮而没有滚动scrollview它工作正常。但我需要添加按钮scrollview  请帮助正确的代码。提前致谢。

1 个答案:

答案 0 :(得分:1)

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

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

    3.在UIPanGestureRecognizer结束状态中,我将检查按钮是否在目标View中丢失。如果你需要的任务也是如此,否则在scrollView中的相同位置添加按钮

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