UIImageView和UIScrollView缩放

我真的需要一个UIScrollView内的UIPinchGestureRecognizer来捏合工作吗? 如果是,我该怎么做? 我正在尝试实现flipboard的function,放大后基本放大图像并具有滚动function。我该怎么做?

更新:

这里有一些我没有调用滚动视图委托的代码

CGRect imgFrame; imgFrame.size.width = originalImageSize.width; imgFrame.size.height = originalImageSize.height; imgFrame.origin.x = imageOriginPoint.x; imgFrame.origin.y = imageOriginPoint.y; NSData *data = [request responseData]; UIImage * image = [UIImage imageWithData:data]; imageView = [[UIImageView alloc] initWithImage:image]; [imageView setUserInteractionEnabled:YES]; [imageView setBackgroundColor:[UIColor clearColor]]; [imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; [imageView setFrame:CGRectMake(0, 0, imgFrame.size.width, imgFrame.size.height)]; UIScrollView * imgScrollView = [[UIScrollView alloc] initWithFrame:imageView.frame]; imgScrollView.delegate = self; imgScrollView.showsVerticalScrollIndicator = NO; imgScrollView.showsHorizontalScrollIndicator = NO; [imgScrollView setScrollEnabled:YES]; [imgScrollView setClipsToBounds:YES]; [imgScrollView addSubview:imageView]; [imgScrollView setBackgroundColor:[UIColor blueColor]]; [imgScrollView setMaximumZoomScale:1.0]; 

所有你需要做的就是在你的UIScrollView添加你的UIImageView (或者你想放大的任何视图)。

将您的UIScrollViewmaximumZoomScale设置为高于1.0f的任何值。

将自己设置为UIScrollView的委托,并在viewForZooming委托方法中返回UIImageView

而已。 没有捏手势需要,没有什么。 UIScrollView为你处理缩放。

不久之前,我做了一个自定义的图像查看器,没有夹点识别器。 只需在UIScrollView之上的UIImageView。 在那里你传递一个链接到图像的string,它也有一个进度条。 一旦该图像完成加载图像显示。 代码如下:

 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return self.theImageView; } - (CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView { CGSize boundsSize = scroll.bounds.size; CGRect frameToCenter = rView.frame; // center horizontally if (frameToCenter.size.width < boundsSize.width) { frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2; } else { frameToCenter.origin.x = 0; } // center vertically if (frameToCenter.size.height < boundsSize.height) { frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2; } else { frameToCenter.origin.y = 0; } return frameToCenter; } -(void)scrollViewDidZoom:(UIScrollView *)scrollView { self.theImageView.frame = [self centeredFrameForScrollView:self.theScrollView andUIView:self.theImageView]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [self.resourceData setLength:0]; self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.resourceData appendData:data]; NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.resourceData length]]; self.progressBar.progress = [resourceLength floatValue] / [self.filesize floatValue]; } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { self.theImage = [[UIImage alloc]initWithData:resourceData]; self.theImageView.frame = CGRectMake(0, 0, self.theImage.size.width, self.theImage.size.height); self.theImageView.image = self.theImage; self.theScrollView.minimumZoomScale = self.theScrollView.frame.size.width / self.theImageView.frame.size.width; self.theScrollView.maximumZoomScale = 2.0; [self.theScrollView setZoomScale:self.theScrollView.minimumZoomScale]; self.theScrollView.contentSize = self.theImageView.frame.size; self.theLabel.hidden = YES; self.progressBar.hidden = YES; } -(void)setImageInImageView { NSURLRequest *req = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.imageLink]]; NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:req delegate:self]; if (conn) { self.resourceData = [NSMutableData data]; } else { NSLog(@"Connection failed: IMageViewerViewController"); } } -(void)loadView { self.filesize = [[NSNumber alloc]init]; self.progressBar = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar]; self.progressBar.frame = CGRectMake(20, 240, 280, 40); [self.progressBar setProgress:0.0]; self.theImageView = [[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]]autorelease]; self.theScrollView = [[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]]autorelease]; self.theScrollView.delegate = self; [self.theScrollView addSubview:self.theImageView]; self.view = self.theScrollView; self.theLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, 320, 40)]; self.theLabel.font = [UIFont boldSystemFontOfSize:15.0f]; self.theLabel.text = @"Please wait, file is being downloaded"; self.theLabel.textAlignment = UITextAlignmentCenter; self.theLabel.hidden = NO; [self.view addSubview:self.progressBar]; [self.view bringSubviewToFront:self.progressBar]; [self.view addSubview:self.theLabel]; [self.view bringSubviewToFront:self. theLabel]; [self performSelectorOnMainThread:@selector(setImageInImageView) withObject:nil waitUntilDone:NO]; } 

和头文件:

 @interface ImageViewerViewController : UIViewController<UIScrollViewDelegate, NSURLConnectionDelegate, NSURLConnectionDataDelegate> @property (nonatomic, retain) IBOutlet UIImageView *theImageView; @property (nonatomic, retain) IBOutlet UIScrollView *theScrollView; @property (nonatomic, retain) NSString *imageLink; @property (nonatomic, retain) UIImage *theImage; @property (nonatomic, retain) UILabel *theLabel; @property (nonatomic, retain) UIProgressView *progressBar; @property (nonatomic, retain) NSMutableData *resourceData; @property (nonatomic, retain) NSNumber *filesize; @end 

希望能帮助到你

Swift 2.0 在滚动视图中捏缩放图像步骤(Swift 2.0)

1.滚动查看约束

在这里输入图像说明

2.添加ImageView在ScrollView中设置约束

在这里输入图像说明

3.拿IBOutlets

 IBOutlet UIScrollView * bgScrollView; IBOutlet UIImageView * imageViewOutlet; 

4. viewDidLoad方法

 - (void)viewDidLoad { [super viewDidLoad]; float minScale=bgScrollView.frame.size.width / imageViewOutlet.frame.size.width; bgScrollView.minimumZoomScale = minScale; bgScrollView.maximumZoomScale = 3.0; bgScrollView.contentSize = imageViewOutlet.frame.size; bgScrollView.delegate = self; } 

5.滚动查看委托方法

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

我想这个答案可能是不必要的,但是我有类似的问题作为@adit和解决它在一个非常简单的方式(我认为),也许有人有类似的挑战可以使用它:

 - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; UIImage *imageToLoad = [UIImage imageNamed:@"background_green"]; self.myImageView = [[UIImageView alloc]initWithImage:imageToLoad]; self.myScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds]; [self.myScrollView addSubview:self.myImageView]; self.myScrollView.contentSize = self.myImageView.bounds.size; self.myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite; self.myScrollView.minimumZoomScale = 0.3f; self.myScrollView.maximumZoomScale = 3.0f; self.myScrollView.delegate = self; [self.view addSubview:self.myScrollView]; } - (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView { NSLog(@"viewForZoomingInScrollView"); return self.myImageView; } 

我的问题是一个非常简单的我忘了添加self.myScrollView.delegate = self; 这就是我遇到问题的原因。 我永远花了这个简单的问题,我想我没有看到所有的树的阿甘:-)

当我在UIScrollView中开发缩放PDF查看器时,发现在电话上运行它时,实际上已经将缩放作为手机的一部分实现了。 但是你可以看看这个, http://developer.apple.com/library/ios/#samplecode/ZoomingPDFViewer/Introduction/Intro.html这是一段试图实现缩放function的示例代码,这个是想让我开始。