如何在指定的UIview iphone中移动图像

时间:2011-11-17 10:38:07

标签: iphone objective-c ipad

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所有角落的边缘时,图像无需移动。

不允许图像离开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];
    }
}

1 个答案:

答案 0 :(得分: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


    }