Search code examples
objective-cxcodeuiscrollviewuipangesturerecognizeruiscrollviewdelegate

Adding UIPangesture for UIButton within UIScrollview to move over the all view


In my project i am having scroll at bottom and table at top. Inside scrollview i am adding 10 buttons.for each button i had pan gesture to move around the screen.when the button intersect with table is will add a picture in tableview.

But the pan is working within the scroll.how to make it move over view

MY coding is:

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

When i add my buttons directly without scroll scrollview it works fine.But i need to add button scrollview please help with proper code. Thanks in advance.


Solution

  • Here is the way I used in my application In my code I follow the below steps

    1. In UIPanGestureRecognizer starting point I add the button to the self.view from scrollView
    2. Drag button by using the translationInView method

    3.In UIPanGestureRecognizer end state I'll check if the button dropped in the destination View.If so do your required task else add the button at the same position in the 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]
                }
            }
        }