2012-01-17 27 views
23

Tôi có thực sự cần một UIPinchGestureRecognizer bên trong UIScrollView để pinch hoạt động không? Nếu có, tôi làm cách nào? Tôi đang cố gắng triển khai Flipboard có gì, ở đâu về cơ bản sẽ phóng to hình ảnh và có khả năng cuộn sau khi phóng to. Làm cách nào để thực hiện điều đó?UIImageView và UIScrollView zoom

UPDATE:

Dưới đây là một số mã mà tôi có mà không gọi xem di chuyển đại biểu

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

Trả lời

79

Tất cả bạn cần làm là thêm bạn (xem hoặc bất kỳ mà bạn muốn phóng to) UIImageView bên trong của bạn UIScrollView.

Đặt maximumZoomScale trên UIScrollView của bạn thành bất kỳ giá trị nào cao hơn 1.0f.

Đặt mình là đại biểu của số UIScrollView và trả lại UIImageView trong phương thức ủy quyền xemForZooming.

Vậy đó. Không cần cử chỉ véo, không có gì cả. UIScrollView xử lý thu phóng pinch cho bạn.

+0

yea, tôi đã làm tất cả điều đó nhưng đại biểu không được gọi là – adit

+0

aha! Tôi đã không thiết lập maxZoomScale và do đó nó không hoạt động! cảm ơn – adit

+0

Ignacio, bạn có thể cho biết các ràng buộc trên imageView và scrollView hay không. Tôi đã làm như vậy nhưng nó không hoạt động. Chế độ xem hình ảnh của tôi được đặt ở chế độ lấp đầy và chiều rộng sẽ nằm ngoài khung hình Xem khung hình vì vậy tôi cần tăng khung xem hình ảnh cũng như chiều rộng kích thước xem cuộn nội dung. Tôi chỉ có một hình ảnh trong chế độ xem cuộn –

2

Khi tôi đang phát triển xem Zooming PDF của tôi trong một UIScrollView tôi phát hiện ra rằng khi chạy nó trên điện thoại nó thực sự đã có zooming thực hiện như là một phần của điện thoại. Nhưng bạn có thể xem xét điều này, http://developer.apple.com/library/ios/#samplecode/ZoomingPDFViewer/Introduction/Intro.html nó là một đoạn mã mẫu cố gắng triển khai chức năng thu phóng, điều này muốn tôi bắt đầu.

+0

tôi thực sự nhìn vào đó và nó nói rằng tôi sẽ cần một đại biểu gọi viewForZoomingInScrollView, tuy nhiên điều này không bao giờ được gọi là mặc dù tôi đã thiết lập các đại biểu để tự – adit

+1

Giống như @Novarg hassaid nó được gọi bằng các UIScrollViewDelegate như vậy trong tệp .h của bạn đảm bảo thêm Popeye

+0

Tôi đã thêm nó và nó vẫn không được gọi là – adit

14

Tôi đã thực hiện một trình xem hình ảnh tùy chỉnh cách đây không lâu mà không có trình nhận dạng pinch. Chỉ UIImageView trên đầu trang của UIScrollView. Ở đó bạn vượt qua một chuỗi với một liên kết đến hình ảnh và nó cũng có một thanh tiến trình. Khi hình ảnh đó đã tải xong hình ảnh được hiển thị. Dưới đây là các mã:

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

Và các tập tin tiêu đề:

@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 

Hy vọng nó giúp

+0

bạn có biết tại sao viewForZoomingInScrollView đó không bao giờ được gọi? – adit

+0

@adit nó được gọi bởi UIScrollViewDelegate Tôi tin rằng – Novarg

+0

@adit đã làm nó hoạt động? Hay bạn có bất kỳ câu hỏi khác? – Novarg

9

Tôi đoán câu trả lời này có thể không cần thiết nhưng tôi đã có vấn đề tương tự như @adit và giải quyết nó một cách rất đơn giản (tôi nghĩ) và có thể một người nào đó với những thách thức tương tự có thể sử dụng nó:

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

My vấn đề là rất đơn giản tôi đã quên để thêm self.myScrollView.delegate = self;, đó là lý do tại sao tôi đã có vấn đề. Nó đã cho tôi mãi mãi để tìm thấy vấn đề đơn giản ra, tôi đoán tôi không thấy Forrest cho tất cả các cây :-)

13

Swift 2.0 ảnh Pinch-zoom trong scrollview bước (Swift 2.0)

1. Di chuyển Xem chế

enter image description here

2.Thêm ImageView Trong scrollview và thiết lập chế

enter image description here

3. Đưa IBOutlets

IBOutlet UIScrollView * bgScrollView; 
IBOutlet UIImageView * imageViewOutlet; 

4. viewDidLoad Phương pháp

- (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. Scroll Xem Đại biểu Phương pháp

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{ 
    return imageViewOutlet; 
} 
Các vấn đề liên quan