0

我有一个并UImageViewIB上面添加了一个地图图像UImageView。我想让该图像可以缩放。

这是我的代码:

- (void)viewDidLoad
{
   [super viewDidLoad];
    self.title = @"Map";

   self.mapImageView.contentMode = UIViewContentModeScaleAspectFit;
   [self.mapScrollView addSubview:self.mapImageView];
   [self.mapScrollView setContentSize:CGSizeMake(self.mapImageView.frame.size.width, self.mapImageView.frame.size.height)];
   [self.mapScrollView setMinimumZoomScale:1.0];
   [self.mapScrollView setMaximumZoomScale:4.0];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
   return self.mapImageView;
}

但这无济于事,我可以看到UIScrollView即将到来,但我的图像没有任何反应。
提前致谢。

4

3 回答 3

3

UIScrollViewDelegate在你的ViewController.h文件中添加

然后将以下代码添加到您的ViewController.m文件中

UIPinchGestureRecognizer如果您使用此代码,则无需添加

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView
    {

    UIView* zoomView = [scrollView.delegate viewForZoomingInScrollView:scrollView];

    CGRect zoomViewFrame = zoomView.frame;

    if(zoomViewFrame.size.width < scrollView.bounds.size.width)

    {

    zoomViewFrame.origin.x = (scrollView.bounds.size.width - zoomViewFrame.size.width) / 2.0;

    }

    else

    {
    zoomViewFrame.origin.x = 0.0;
    }

    if(zoomViewFrame.size.height < scrollView.bounds.size.height)

     {      zoomViewFrame.origin.y = (scrollView.bounds.size.height - zoomViewFrame.size.height) / 2.0;

    }
    else

    {
    zoomViewFrame.origin.y = 0.0;
    }
    zoomView.frame = zoomViewFrame;
    }

更改viewDidLoad如下

- (void)viewDidLoad
 {
     [super viewDidLoad];
    self.mapScrollView.delegate = self;
    self.mapScrollView.minimumZoomScale = 1.0;
    self.mapScrollView.maximumZoomScale = 4.0;
    UIImage * myImage= [UIImage imageNamed:@"Background.png"]; //add your image here
    [self.mapImageView setImage:myImage];
    [self.mapImageView sizeToFit];
    self.mapScrollView.contentSize = myImage.size;

}

在此处指定要缩放的imageView

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.mapImageView;
}

为避免 UIScrollView 在 zoomimg 上的奇怪行为,请使用此方法

- (void)view:(UIView*)view setCenter:(CGPoint)centerPoint
{
    CGRect viewFrame = view.frame;
        CGPoint contentOffset = self.mapScrollView.contentOffset;

        CGFloat x = centerPoint.x - viewFrame .size.width / 2.0;
        CGFloat y = centerPoint.y - viewFrame .size.height / 2.0;

    if(x < 0)
    {
        contentOffset.x = -x;
        viewFrame .origin.x = 0.0;
    }
    else
    {
        viewFrame .origin.x = x;
    }
    if(y < 0)
    {
        contentOffset.y = -y;
        viewFrame .origin.y = 0.0;
    }
    else
    {
        viewFrame .origin.y = y;
    }

    view.frame = viewFrame ;
    self.mapScrollView.contentOffset = contentOffset;
}

然后在 viewDidAppear 上调用上述方法

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CGPoint centerPoint = CGPointMake(CGRectGetMidX(self.mapScrollView.bounds),
                                  CGRectGetMidY(self.mapScrollView.bounds));
    [self view:self.mapImageView setCenter:centerPoint];
}

如果您需要更多说明,请查看此链接。我从那个链接学到了这种技术。

于 2013-05-21T10:55:26.747 回答
2

试试这个

    //At ViewDidLoad (OR whare you create your mapImageView)
   UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
   [pinchRecognizer setDelegate:self];
   [mapImageView addGestureRecognizer:pinchRecognizer];

   mapImageView.userInteractionEnabled=YES;




   -(void)scale:(id)sender {
       [YourView bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];

       if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
           lastScale = 1.0;  //global float variable.
           return;
       }

       CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

       CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
       CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

       [[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
   }
于 2013-05-21T09:30:27.080 回答
0

to do it without scrollView, Add this Snippet to your class:

-(void) addGesturesTo : (UIImageView *) imageView{
    // Register for double-finger dragging to pan the camera.
    UIPinchGestureRecognizer* pinchGestureReconizer = [[UIPinchGestureRecognizer alloc] initWithTarget: self                                                                                                    action: @selector(handlePich:)];
    [imageView addGestureRecognizer:pinchGestureReconizer];
}
- (void) handlePich: (UIPinchGestureRecognizer *) pinchGestureRecognizer{
    CGFloat scale = pinchGestureRecognizer.scale;
    [[pinchGestureRecognizer view]  setTransform:CGAffineTransformScale([pinchGestureRecognizer view].transform, scale, scale)];
    pinchGestureRecognizer.scale = 1.0;
}

Now call addGesturesTo with imageView. As:

[self addGesturesTo:imageView];

To Do it with ScrollView: Follow These steps:

  • add scrollView set delegate to self
  • Add imageView inside scrolView
  • implement - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; in your class and return imageView
于 2013-05-21T09:35:18.217 回答