2013-04-22 52 views

回答

0

只能爲特定的UIImageView對象設置UIPanGestureRecognizer。

//set UIPanGestureRecognizer for first image 
    UIPanGestureRecognizer *panRecognizer1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage1:)]; 
    [panRecognizer1 setMinimumNumberOfTouches:1]; 
    [panRecognizer1 setMaximumNumberOfTouches:1]; 
    [panRecognizer1 setDelegate:self]; 
    [img1 addGestureRecognizer:panRecognizer1]; 

    //set UIPanGestureRecognizer for second image 
    UIPanGestureRecognizer *panRecognizer2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage2:)]; 
    [panRecognizer2 setMinimumNumberOfTouches:1]; 
    [panRecognizer2 setMaximumNumberOfTouches:1]; 
    [panRecognizer2 setDelegate:self]; 
    [img1 addGestureRecognizer:panRecognizer2]; 

和寫入圖像中的兩個方法移動

-(void)moveImage1:(id)sender { 
      //coding for moving first image 
      [[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations]; 
      [viewGesture bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]]; 
      CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view]; 

      if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { 
        firstX = [[sender view] center].x; 
        firstY = [[sender view] center].y; 
      } 

      translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y); 
      [[sender view] setCenter:translatedPoint]; 

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


        CGFloat finalX = translatedPoint.x + (.04*[(UIPanGestureRecognizer*)sender velocityInView:viewGesture].x); 
        CGFloat finalY = translatedPoint.y + (.04*[(UIPanGestureRecognizer*)sender velocityInView:viewGesture].y); 

        if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { 

          if(finalX < -5000) {    finalX = 0;  } 
          else if(finalX > 5000) {   finalX = 320;  } 

          if(finalY < -5000) {   finalY = 0;    } 
          else if(finalY > 5000) {  finalY = 460;   } 
        } 
        else { 
          if(finalX < -5000) { finalX = 0; } 
          else if(finalX > 5000) { finalX = 320;} 

          if(finalY < -5000) {   finalY = 0;    } 
          else if(finalY > 5000) {  finalY = 460;  } 
        } 

        [UIView beginAnimations:nil context:NULL]; 
        [UIView setAnimationDuration:.35]; 
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
        [[sender view] setCenter:CGPointMake(finalX, finalY)]; 
        [UIView commitAnimations]; 
      } 
    } 

    -(void)moveImage2:(id)sender { 
      //coding for moving second image 
    } 

,也看到this與uicollectionview

+0

仍然沒有工作..也許它的圖像中移動。因爲我使用了UIGestureRecognizerStateBegan:,UIGestureRecognizerStateChanged:UIGestureRecognizerStateEnded :.你有什麼建議如何實現它??謝謝 – 2013-04-22 10:03:42

+0

看到我編輯的答案... – DharaParekh 2013-04-22 10:17:32

+0

是的..謝謝..我會努力工作..再次感謝你。 – 2013-04-22 10:27:55

相關問題