2011-07-26 128 views
0

我有UIScrollView包含3 * UIImageView,每個UIImageView都有一個圖像。 碼我用:UIScrollView + UIImageView放大/縮小

scrollView.delegate = self; 

[self.scrollView setBackgroundColor:[UIColor blackColor]]; 
[scrollView setCanCancelContentTouches:NO]; 

scrollView.maximumZoomScale = 4.0; 
scrollView.minimumZoomScale = 0.75; 

scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
scrollView.clipsToBounds = YES; 
scrollView.scrollEnabled = YES; 
scrollView.pagingEnabled = YES; 

NSUInteger nimages = 0; 
CGFloat cx = 0; 
for (; ; nimages++) { 
    NSString *imageName = [NSString stringWithFormat:@"%d.jpg", (nimages + 1)]; 

    UIImage *image = [UIImage imageNamed:imageName]; 

    if (image == nil) { 
    break; 
    } 

    imageView = [[UIImageView alloc] initWithImage:image];   
    CGRect rect = imageView.frame; 
    rect.size.height = image.size.height; 
    rect.size.width = image.size.width; 
    rect.origin.x = ((scrollView.frame.size.width - image.size.width)/2) + cx; 
    rect.origin.y = ((scrollView.frame.size.height - image.size.height)/2); 
    [imageView setFrame:CGRectMake(cx, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

    [scrollView addSubview:imageView]; 
    [imageView release]; 

    cx += scrollView.frame.size.width; 
} 

[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)]; 

我怎樣才能使滾動型使用變焦能夠多點觸控? 我用這個代碼:

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

但它並沒有正常工作,圖像重疊得到,因爲滾動型包含多張圖片。

我該如何解決? 感謝

回答

2

不要添加圖像視圖滾動視圖直接,而是做這種方式:

分別爲每位UIImageView的一個新的UIScrollView對象,並設置框架與圖像視圖相同。將圖像視圖作爲子視圖添加到滾動視圖。請記住,這些滾動視圖對象僅用於縮放圖像,因此請設置zoomScale,minimumZoomScale和maximumZoomScale,並設置滾動代理。在委託控制器:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
return [scrollView.subViews objectAtIndex:0]; 
} 

然後,所有的滾動視圖對象添加到您在代碼中使用的滾動視圖。此滾動視圖僅用於滾動上面創建的子滾動視圖。

有想法嗎?

+0

子視圖沒有子視圖;) – Bejil

0

你可以使用PinchGesture到你的圖像視圖... 沒有必要使用3個滾動視圖。

0

嘗試此爲您3張圖片:

**.h Controller:** 

@interface MyViewController : UIViewController{ 

IBOutlet UIScrollView *scrollView; 
UIImageView *imageView; 

} 

@property (nonatomic, retain) UIScrollView *scrollView; 
@property (nonatomic, retain) UIImageView *imageView; 

.M控制器:

@synthesize scrollView, imageView; 


-(UIView *)viewForZoomingInScrollView:(UIScrollVi­ew *)scrollView{ 

return imageView; 

} 

- (void)viewDidLoad 
{ 

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YourImageName.png"]]; 
self.imageView = tempImageView; 
[tempImageView release]; 
scrollView.maximumZoomScale = 3.0; 
scrollView.minimumZoomScale = 0.6; 
scrollView.clipsToBounds = YES; 
scrollView.delegate = self; 
[scrollView addSubview:imageView]; 
} 

此代碼爲我工作:)