2

我想實現平移手勢的功能到imageview,這個imageview是滾動視圖的子視圖。問題是當我嘗試滾動scrollview的圖像視圖的平移手勢被識別,並且滾動視圖不滾動。 請幫助我。UIImageView的平移手勢干涉滾動

這是初始化泛在的UIImageView代碼:

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; 
      [panRecognizer setMinimumNumberOfTouches:1]; 
      [panRecognizer setMaximumNumberOfTouches:1]; 
      [panRecognizer setDelegate:self]; 
      [[[scrollView subviews] objectAtIndex:i] addGestureRecognizer:panRecognizer]; 

的ImageView的具有用戶設置成 「YES」

,併爲其

-(void)move:(UIPanGestureRecognizer *)sender 
{ 
    [[[[[sender view] superview] superview] superview] bringSubviewToFront:sender.view]; 


    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { 

     if ([[[sender view] superview] superview] == itemView) // adds the imageview in the item scroll 
     { 
      int tag = [[sender view] tag]/100; 
      UIScrollView *tempScroll = (UIScrollView *)[itemView viewWithTag:-tag]; 
      CGRect frame = CGRectFromString([itemFrameDict objectForKey:[NSString stringWithFormat:@"%i",[[sender view] tag]]]); 

      CGAffineTransform transform = temp.transform; 
      UIImageView *tempImageView = [[UIImageView alloc] initWithImage: [(UIImageView *) [sender view] image]]; 
      tempImageView.tag = [[sender view] tag]; 
      //NSLog(@"[sender view] superview] : %@", [[sender view] superview]); 
      tempImageView.frame = frame; 
      [tempImageView setTransform:transform]; 
      [tempImageView setFrame:frame]; 
      [tempImageView setUserInteractionEnabled:YES]; 
      [tempScroll addSubview:tempImageView]; 

      UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; 
      [panRecognizer setMinimumNumberOfTouches:1]; 
      [panRecognizer setMaximumNumberOfTouches:1]; 
      [panRecognizer setDelegate:self]; 
      [tempImageView addGestureRecognizer:panRecognizer]; 

      UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 
      [singleTapGestureRecognizer setNumberOfTapsRequired:1]; 
      [tempImageView addGestureRecognizer:singleTapGestureRecognizer]; 
     } 
    } 

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged) 
    { 
     CGPoint translatedPoint = [sender locationInView:[[sender view] superview]] ; 
     newTranslatedPoint = [[[sender view] superview] convertPoint:translatedPoint toView:self.view]; 

     if ([sender view].frame.size.width == 35 && [sender view].frame.size.height == 35) 
      [[sender view] sizeToFit]; 

     [sender view].center = CGPointMake(newTranslatedPoint.x, newTranslatedPoint.y); //startFrame; 

     [self.view addSubview:[sender view]]; 
    } 

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) 
    { 
     CGPoint newPoint = [[[sender view] superview] convertPoint:newTranslatedPoint toView:itemView]; 
     if (newPoint.y < 0)// || !viewUp) 
     { 
      //NSLog(@" baseView"); 
      [baseView addSubview:[sender view]]; 

      UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)]; 
      [doubleTapGestureRecognizer setNumberOfTapsRequired:2]; 
      [[sender view] addGestureRecognizer:doubleTapGestureRecognizer]; 

      UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)]; 
      [pinchRecognizer setDelegate:self]; 
      [[sender view] addGestureRecognizer:pinchRecognizer]; 

      UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)]; 
      [rotationRecognizer setDelegate:self]; 
      [[sender view] addGestureRecognizer:rotationRecognizer]; 
     } 

     else if (newPoint.y > 0)// && viewUp) 
     { 
      //NSLog(@" itemView"); 
      int tag = [[sender view] tag]/100; 
      UIScrollView *tempScroll = (UIScrollView *)[itemView viewWithTag:-tag]; 
      CGRect frame = CGRectFromString([itemFrameDict objectForKey:[NSString stringWithFormat:@"%i",[[sender view] tag]]]); 

      CGAffineTransform transform = temp.transform; 
      [[sender view] setTransform:transform]; 
      [[sender view] setFrame:frame]; 
      [tempScroll addSubview:[sender view]]; 
     } 
    } 
} 
+0

請顯示您在執行平移手勢時做了什麼。 – Zen

+0

同樣在使用IB時,默認情況下,'UIImageView'不支持用戶交互。對於imageview上的手勢識別器,您必須啓用該功能。 – Zen

+0

@zen我已經添加了我使用 – Khushboo

回答

3

我的行動'我不確定我是否明白你想要通過使用已在UIScrollView中的UIImageView上的UIPanGestureRecognizer來實現。但從你的描述來看,這聽起來像是你想讓UIScrollView做到這一點,並且UIPanGestureRecognizer不會阻止它。我相信你需要實現你的UIPanGestureRecognizer此委託方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 
    return YES; 
} 

請閱讀關於UIGestureRecognizerDelegate Protocol reference,以允許同時手勢識別。根據文檔:

默認實現返回NO-沒有兩個手勢可以 同時識別。

/*編輯*/

UIPanGestureRecognizer - Only vertical or horizontal: 實現了UIPanGestureRecognizer只是一個方向。這是用於垂直平移的:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer { 
    CGPoint translation = [panGestureRecognizer translationInView:someView]; 
    return fabs(translation.y) > fabs(translation.x); 
} 
+0

其實我在執行拖放到這裏的功能 – Khushboo

+0

我已經實現了這個 代碼 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 返回YES; } – Khushboo

+0

@Khushboo所以你想在UIScrollView中移動UIImageView? UIScrollView只能垂直/水平滾動,還是兩者都滾動? – DonnaLea