0

我正在尝试使用 aUIPinchGestureRecognizer来调整 a 的大小UIImageView。应用一个简单的CGAffineTransform作品,但它根据左上角调整它的大小,而我想根据图像的中心调整它的大小。我可以使用以下代码达到预期的结果

-(IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
UIImageView *view = [recognizer view];
float scale = recognizer.scale;
view.bounds = CGRectMake(0,  0, view.bounds.size.height*scale, view.bounds.size.width*scale);
recognizer.scale = 1;
}

问题是,当我使用此代码时,图像在调整大小时会出现故障和断断续续。知道为什么会发生这种情况吗?

4

1 回答 1

0

你可能会很难做到这一点。建议您考虑将您添加UIImageView到 aUIScrollView中,并让滚动视图为您完成工作。例如:

标题UIScrollViewDelegate

#import <UIKit/UIKit.h>

@interface MyScrollViewController : UIViewController <UIScrollViewDelegate>

@end

显示允许缩放的滚动视图设置的实现:

#import "MyScrollViewController.h"

#define MIN_ZOOM_FACTOR 1
#define MAX_ZOOM_FACTOR 5

@interface MyScrollViewController () {
    UIScrollView *scrollView;
    UIImageView *imageView;
}

@end

@implementation MyScrollViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * MAX_ZOOM_FACTOR,
                                    self.view.bounds.size.height * MAX_ZOOM_FACTOR);
    scrollView.minimumZoomScale = MIN_ZOOM_FACTOR;
    scrollView.maximumZoomScale = MAX_ZOOM_FACTOR;

    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"your_image_name.png"]];
    imageView.frame = scrollView.frame;
    imageView.contentMode = UIViewContentModeScaleAspectFit;

    [scrollView addSubview:imageView];
}

#pragma mark - Scroll view delegate methods

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return imageView;
}

@end

如果您想知道何时发生平移和缩放、缩放比例是多少等,请实现其他UIScrollViewDelegate方法。例如:

-(void)scrollViewDidZoom:(UIScrollView *)zoomedScrollingView{
    float zoomScale = scrollView.zoomScale;
    // Do something with zoomScale...
}
于 2012-11-08T00:27:16.630 回答