在我的UIScrollview中,我有一个黑色背景的UIImageView。在里面有另一个UIImageView里面有一个UIImage,我使用这段代码来启用缩放:
fullImageView = [[UIView alloc] initWithFrame:CGRectMake(0,0,screenWidth,screenHeight)];
fullImageView.backgroundColor = [UIColor blackColor];
[fullImageView addSubview:copyImageToShowImageView];
zoomFullImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,screenWidth,screenHeight)];
[zoomFullImageScrollView addSubview:fullImageView];
[self.view addSubview:zoomFullImageScrollView];
UIPinchGestureRecognizer *twoFingerTapRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapImage:)];
[copyImageToShowImageView addGestureRecognizer:twoFingerTapRecognizer];
//Then..
- (void)doubleTapImage:(UIPinchGestureRecognizer *)gesture{
if (gesture.state == UIGestureRecognizerStateEnded
|| gesture.state == UIGestureRecognizerStateChanged) {
CGFloat currentScale = copyImageToShowImageView.frame.size.width / copyImageToShowImageView.bounds.size.width;
CGFloat newScale = currentScale * gesture.scale;
NSLog(@"gesture.scale = %f", currentScale * gesture.scale);
if (newScale < 1) {
newScale = 1;
}
if (newScale > 100) {
newScale = 100;
}
CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
copyImageToShowImageView.transform = transform;
gesture.scale = 1;
copyImageToShowImageView.transform = CGAffineTransformTranslate(copyImageToShowImageView.transform, newScale, newScale);
zoomFullImageScrollView.contentSize=CGSizeMake(copyImageToShowImageView.frame.size.width, copyImageToShowImageView.frame.size.height);
}
}
//and the methods:
- (void)viewDidLoad {
NSLog(@"super");
[super viewDidLoad];
zoomFullImageScrollView.minimumZoomScale=0.5;
zoomFullImageScrollView.maximumZoomScale=6.0;
zoomFullImageScrollView.delegate=self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return copyImageToShowImageView;
}
此代码有效,但存在问题,因为scrollview内容大小与UIImageView大小不同。也许他们是平等但不一致的。然后我想在用户进行缩放时,图像放大,而不是总是从中心放大,但这并不重要。
我希望自己解释一下。
我该怎么办?
答案 0 :(得分:1)
如果您将UIImageView放在UIScrollView中,它将使您的生活更轻松。
UIScrollView委托实现特定于Zoom的方法。因为它是可滚动的,所以你可以“移动”整个图像。
在UIScrollView的documentation中,您可以找到与缩放相关的优秀示例。