你可能会很难做到这一点。建议您考虑将您添加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...
}