2015-09-07 17 views
0

我想PanGestureRecognizer移動的ImageView這樣 (ImageView的可縮放)如何通過PanGestureRecognizer控制imageView的移動?

  1. 如果ImageView的的位置是(0,0),它不能移動。
  2. 如果imageView的位置x超過60,則無法移動更多。
  3. 如果imageView的位置y超過80,則無法移動更多。
  4. 當imageView的比例恢復(1.0)時,它的位置是(0,0)。

很難限制imageView的移動和位置。 我該怎麼辦?

這是我的代碼。

img = [UIImage imageNamed:[NSString stringWithFormat:@"a.jpg"]]; 
imgView = [[UIImageView alloc]initWithImage:img]; 
imgView.frame = CGRectMake(0,0, self.view.frame.size.width, 448); 
imgView.userInteractionEnabled = YES; 
[self.view imgView]; 


- (void)panAction : (UIPanGestureRecognizer *)sender { 

    CGPoint CGP = imgView.center; 

    if(newScale != 1.0 && CGP.x-160 != 0 && CGP.y-224 != 50){ 

    CGPoint p = [sender translationInView:self.view]; 

    CGPoint movedPoint = CGPointMake(imgView.center.x + p.x, imgView.center.y + p.y); 
    imgView.center = movedPoint; 


    [sender setTranslation:CGPointZero inView:self.view]; 
    } 
} 


-(void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer { 

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) { 
     // Reset the last scale, necessary if there are multiple objects with different scales 
     lastScale = [gestureRecognizer scale]; 
    } 

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || 
     [gestureRecognizer state] == UIGestureRecognizerStateChanged) { 

     CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue]; 

     // Constants to adjust the max/min values of zoom 
     const CGFloat kMaxScale = 2.0; 
     const CGFloat kMinScale = 1.0; 

     newScale = 1 - (lastScale - [gestureRecognizer scale]); 
     newScale = MIN(newScale, kMaxScale/currentScale); 
     newScale = MAX(newScale, kMinScale/currentScale); 
     CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale); 
     [gestureRecognizer view].transform = transform; 

     lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call 
    } 


} 

回答

0

您必須使用以下條件檢查在您的移動手勢動作: 對於您的信息,當您移動對象時,這panAction()方法是在每個像素的舉動定期調用。

- (void)panAction : (UIPanGestureRecognizer *)sender { 

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) 
{ 
    //Do something whatever you need to be done while the pan gesture starts 
} 
if ([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateChanged) 
{ 
    //Do something whatever you need to be done while the imageView object is in the moving state 
} 
if ([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateEnded){ 
//Do something you need to do as you end the pan gesture. 
} 
} 
相關問題