2011-11-17 56 views
0
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; 
    [panRecognizer setMinimumNumberOfTouches:1]; 
    [panRecognizer setMaximumNumberOfTouches:1]; 
    [panRecognizer setDelegate:self]; 
    [templatePhotoPlaceholderView addGestureRecognizer:panRecognizer]; 

-(void)move:(UIPanGestureRecognizer *)gestureRecognizer{ 

     CGPoint translatedPoint = [gestureRecognizer translationInView:imageview]; 
     CGPoint center; 
     if([gestureRecognizer state] == UIGestureRecognizerStateBegan) { 
      _firstX = [imageview center].x; 
      _firstY = [imageview center].y; 

      if(center.x < templatePhotoPlaceholderView.frame.origin.x + (imageview.frame.size.width/2)){ 
       center.x = templatePhotoPlaceholderView.frame.origin.x+(imageview.frame.size.width/2); 
      } 
      if(center.x > templatePhotoPlaceholderView.frame.origin.x+templatePhotoPlaceholderView.frame.size.width - (imageview.frame.size.width/2)){ 
       center.x = templatePhotoPlaceholderView.frame.origin.x+templatePhotoPlaceholderView.frame.size.width - (imageview.frame.size.width/2); 
      } 
      if(center.y < templatePhotoPlaceholderView.frame.origin.y + (imageview.frame.size.height/2)){ 
       center.y = templatePhotoPlaceholderView.frame.origin.y + (imageview.frame.size.height/2); 
      } 
      if(center.y > templatePhotoPlaceholderView.frame.origin.y + templatePhotoPlaceholderView.frame.size.height -(imageview.frame.size.height/2)){ 
       center.y = templatePhotoPlaceholderView.frame.origin.y + (templatePhotoPlaceholderView.frame.size.height)-(imageview.frame.size.height/2); 
      } 
     } 
     translatedPoint = CGPointMake(center.x+translatedPoint.x, center.y+translatedPoint.y); 
     [imageview setCenter:translatedPoint]; 
    } 

移動圖像如何在指定視圖[templatePhotoPlaceholderView is a view imageview is an UIImageView]如何在指定的UIView iphone

當圖像觸及的UIView的所有角落的邊緣移動的圖像,然後圖像不必移動。

不允許圖像在UIView的外面。

我嘗試但無法修復指定區域中的圖像視圖以便移動。

@對不起,如果在打字時出現任何語法錯誤。

@all請指教我如何找出問題。

//縮放

UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)] autorelease]; 
    [pinchRecognizer setDelegate:self]; 
    [templatePhotoPlaceholderView addGestureRecognizer:pinchRecognizer]; 
    [self.view addSubview:templatePhotoPlaceholderView]; 
    [tapRecognizer release]; 

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

    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer]; 
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) { 


     float hfactor = imageview.frame.size.width/templatePhotoPlaceholderView.frame.size.width; 
     float vfactor = imageview.frame.size.height/templatePhotoPlaceholderView.frame.size.height; 
     float factor = MAX(hfactor, vfactor); 
     if (([gestureRecognizer scale] > 1 && factor < 3) || ([gestureRecognizer scale] <= 1 && factor >1)) { 
     imageview.transform = CGAffineTransformScale([imageview transform], [gestureRecognizer scale], [gestureRecognizer scale]); 
     } 
     [gestureRecognizer setScale:1]; 
    } 
} 
+0

@all任何一個可以告訴我在哪裏,我錯了。 – user905582

回答

0

如果不縮放圖像或視圖,然後它是非常簡單的。

試試這個方法:

-(void)move:(UIPanGestureRecognizer *)gestureRecognizer{ 

     CGPoint translatedPoint = [gestureRecognizer translationInView:imageview]; 
     CGPoint center; 
     if([gestureRecognizer state] == UIGestureRecognizerStateBegan) { 
      _firstX = [imageview center].x; 
      _firstY = [imageview center].y; 
//checking for x axis 
if((self.view.frame.origin.x + 2 > _firstX) && (imageVIew.frame.size.width+_fisrtX < 340)) 
    //perform move condition 

//checking for Y axis 
if(self.view.frame.origin.y+ 2 > _firstY) && (imageview.Frame.size.height + _firstY < 420) 
//perform move condtion 


    } 
+0

我也在縮放圖像。 – user905582