2012-01-17 13 views
23

क्या मुझे वास्तव में UIScrollView के अंदर एक UIPinchGestureRecognizer की आवश्यकता है ताकि चुटकी काम कर सके? यदि हाँ, मैं इसे कैसे कर सकता हूं? मैं फ्लिपबोर्ड को लागू करने की कोशिश कर रहा हूं, जहां यह मूल रूप से एक छवि में ज़ूम करता है और ज़ूम इन करने के बाद स्क्रॉल क्षमता है। मैं यह कैसे कर सकता हूं?UIImageView और 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

अपने maximumZoomScale को अपने UIScrollView पर 1.0f से अधिक किसी भी मान पर सेट करें।

अपने UIScrollView के प्रतिनिधि के रूप में स्वयं को सेट करें और दृश्य में UIImageView को वापस करने के लिए प्रतिनिधि विधि को वापस करें।

यही है। कोई चुटकी इशारा की जरूरत नहीं, कुछ भी नहीं। UIScrollView आपके लिए चुटकी ज़ूमिंग संभालता है।

+0

हाँ, मैंने वह सब किया लेकिन प्रतिनिधि को – adit

+0

आह कहा नहीं जाता है! मैंने maxZoomScale सेट नहीं किया और इसलिए यह काम नहीं किया! धन्यवाद – adit

+0

इग्नासिओ कृपया आप छवि दृश्य और scrollView पर बाधाओं को बता सकते हैं। मैंने वही किया लेकिन यह काम नहीं कर रहा है। मेरा छवि दृश्य पहलू भरने के लिए सेट है और चौड़ाई छवि से बाहर जा रही है फ्रेम देखें इसलिए मुझे छवि दृश्य फ्रेम बढ़ाने के साथ-साथ सामग्री आकार चौड़ाई को स्क्रॉल करने की आवश्यकता है। मेरे स्क्रॉल व्यू में मेरे पास केवल एक छवि है –

2

फोन नहीं करता है जब मैं एक UIScrollView में मेरी ज़ूम व्यूअर के विकसित कर रहा था मुझे लगता है कि जब पता चला है इसे फोन पर चलाना वास्तव में पहले से ही ज़ूमिंग को फोन के हिस्से के रूप में लागू किया गया है। लेकिन आप इसे देख सकते हैं, http://developer.apple.com/library/ios/#samplecode/ZoomingPDFViewer/Introduction/Intro.html यह नमूना कोड का एक टुकड़ा है जो ज़ूमिंग के लिए कार्यक्षमता को लागू करने का प्रयास करता है, यह मुझे शुरू करना चाहता है।

+0

मैं वास्तव में उस को देखा और यह दर्शाता है कि मैं एक प्रतिनिधि viewForZoomingInScrollView कहा जाता है की आवश्यकता होगी, लेकिन इस बुलाया कभी नहीं है, हालांकि मैं पहले से ही आत्म करने के लिए प्रतिनिधि सेट – adit

+1

तरह @Novarg hassaid यह UIScrollViewDelegate से बुलाया जाता है में तो आपकी .h फ़ाइल Popeye

+0

जोड़ने के लिए सुनिश्चित करें मैंने इसे जोड़ा है और इसे अभी भी – adit

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

+0

@adit इसे UIScrollViewDelegate द्वारा बुलाया जाता है मुझे विश्वास है कि – Novarg

+0

@adit ने यह काम किया है? या फिर आपके पास कोई भी अन्य प्रश्न हैं? – Novarg

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 scrollview कदम में पिंच-ज़ूम छवि (स्विफ्ट 2.0) देखा था

1. स्क्रॉल देखें प्रतिबन्ध

enter image description here

2।Scrollview में 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; 
}