2012-01-17 127 views
23

我是否真的需要在UIScrollView中使用UIPinchGestureRecognizer來進行縮放工作?如果是,我該怎麼做?我正在嘗試實現flipboard的功能,它基本放大圖像並放大後具有滾動功能。我該怎麼做?UIImageView和UIScrollView縮放

UPDATE:

下面是一些代碼,我有當我發展我的縮放PDF閱讀器在一個UIScrollView不叫滾動視圖代表

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]; 

回答

79

所有你需要做的就是添加UIImageView(或任何視圖要放大)裏面的UIScrollView

將您的UIScrollView上的maximumZoomScale設置爲高於1.0f的任何值。

將自己設置爲UIScrollView的代表並在viewForZooming委派方法中返回UIImageView

就是這樣。沒有捏手勢需要,沒有什麼。 UIScrollView爲您處理縮放縮放。

+0

是的,我做了所有,但代表不是所謂 – adit 2012-01-17 16:46:42

+0

啊!我沒有設置maxZoomScale,因此它不起作用!謝謝 – adit 2012-01-17 16:49:26

+0

伊格納西奧請你可以告訴對imageView和scrollView的限制。我做了同樣的事情,但它不起作用。我的圖像視圖設置爲縱橫填充,寬度從圖像視圖框外,因此我需要增加圖像視圖框以及滾動視圖內容大小寬度。我的滾動視圖中只有一個圖像 – 2017-11-09 10:03:43

2

我發現,當在手機上運行它實際上已經實現了作爲手機一部分的縮放功能。但你可以看看這個,http://developer.apple.com/library/ios/#samplecode/ZoomingPDFViewer/Introduction/Intro.html這是一段試圖實現縮放功能的示例代碼,這是想讓我開始。

+0

其實我看了這一點,它說,我需要一個叫做viewForZoomingInScrollView代表,然而,這是從來沒有所謂的,雖然我已經設置委託自我 – adit 2012-01-17 08:15:48

+1

像@Novarg hassaid它就會被UIScrollViewDelegate調用,所以在你的.h文件確保添加了 Popeye 2012-01-17 10:42:48

+0

我確實添加了它,它仍然不叫 – adit 2012-01-17 16:22:43

14

不久前,我做了一個自定義圖像查看器,沒有夾點識別器。只需在UIScrollView之上的UIImageView。在那裏你傳遞一個鏈接到圖像的字符串,它也有一個進度條。一旦該圖像完成加載,圖像就會顯示出來。下面的代碼:

-(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 

希望它可以幫助

+0

你知道爲什麼viewForZoomingInScrollView永遠不會被調用嗎? – adit 2012-01-17 08:21:54

+0

@adit它被UIScrollViewDelegate調用我相信 – Novarg 2012-01-17 08:24:10

+0

@adit是否有效?或者你還有其他問題嗎? – Novarg 2012-01-17 08:57:49

9

我想這個答案可能是不必要的,但我也有類似的問題@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;,這是我有問題的原因。我花了永遠的數字,簡單的問題了,我想我沒有看到阿甘對所有的樹木:-)

13

雨燕2.0 滾動型步驟捏縮放圖像(雨燕2.0)

1.滾動視圖約束

enter image description here

2。添加ImageView的滾動型,並設置約束

enter image description here

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; 
}