0

在我的項目中,我在底部滾動並在頂部顯示錶格。 滾動視圖裏面我添加了10個按鈕,每個按鈕我都有平移手勢在屏幕上移動。當按鈕與表相交時,會在tableview中添加圖片。在UIScrollview中爲UIButton添加UIPangesture以移動到所有視圖

但鍋工作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; 
} 

當我直接加我的按鈕沒有滾動滾動視圖它的工作原理fine.But我需要添加按鈕滾動視圖 請幫助正確的代碼。提前致謝。

回答

1

這是我在我的應用程序使用 在我的代碼的方式,我按照以下步驟

  1. 在UIPanGestureRecognizer起點,我通過使用滾動視圖
  2. 將按鈕添加到self.view
  3. 拖動按鈕該translationInView方法

3.In UIPanGestureRecognizer結束狀態,我會檢查按鈕,在目的地下降View.If所以不要否則你的任務需要在滾動視圖的同一位置添加按鈕

`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] 
      } 
     } 
    } 
相關問題