2011-05-11 113 views
1

它是一個熟悉的主題我使用UIScrollview創建圖形小說。該計劃是能夠與頂級uiscrollview沿着小說頁面。其中有一系列嵌套的uiscrollviews,每個嵌入uimageview。這個想法是,大圖像可以平移並放大,但​​仍然可以分頁。我有這個工作的大部分,但我不能讓縮放工作。我已經實現了縮放代理方法,但無濟於事,縮放只是平移圖像。我懷疑這是由於嵌套滾動視圖的嵌入性,但我不知道我缺少什麼。我看了一下Apple的scrollview套件示例,但無法找到答案。縮放嵌套的UIScrollViews

這是我迄今爲止的工作:

- (void)viewDidLoad 

{

[super viewDidLoad]; 


UIScrollView *scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)]; 
scrollview.pagingEnabled = YES; 
scrollview.scrollEnabled =YES; 
scrollview.clipsToBounds = NO; 
scrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
scrollview.showsHorizontalScrollIndicator = YES; 
scrollview.backgroundColor = [UIColor blackColor]; 

NSInteger viewcount=4; 
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:@"mars.png"],[UIImage imageNamed:@"mercury.png"],[UIImage imageNamed:@"neptune.png"],[UIImage imageNamed:@"venus.png"],nil]; 

for (int i = 0; i <viewcount; i++) 
{ 
    CGFloat x = i * self.view.frame.size.width; 

    subView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)]; 


    [subView setBackgroundColor:[UIColor blackColor]]; 
    [subView setCanCancelContentTouches:NO]; 
    subView.clipsToBounds = NO; // default is NO, we want to restrict drawing within our scrollview 
    subView.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
    aImageView = [[UIImageView alloc ] initWithImage:[images objectAtIndex:i]]; 
    [self.aImageView setTag:ZOOM_VIEW_TAG]; 
    [subView addSubview:aImageView]; 
    [subView setContentSize:CGSizeMake(aImageView.frame.size.width, subView.frame.size.height)]; 
    subView.minimumZoomScale = 1; 
    subView.maximumZoomScale = 3; 
    subView.delegate = self; 
    [subView setScrollEnabled:YES]; 
    subView.contentSize = aImageView.frame.size; 
    [scrollview addSubview:subView]; 
    [subView release]; 
} 



[self.view addSubview:scrollview]; 

} 



-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView { 
NSLog (@"test"); 
UIView * view = nil; 
view = [subView viewWithTag:ZOOM_VIEW_TAG]; 

return view; 
} 
+0

無法立即檢查,但是看看userInteractionEnabled對UIScrollView的默認值是否爲YES。我記得添加子視圖的方式與你完全一樣,因爲我想實現同樣的目的。同時檢查可能會干擾觸摸事件的其他標誌。 – TheBlack 2011-05-12 01:10:35

回答

3

曾爲這一點,其他任何人試圖做到這一點。

有一件事我錯過了上面的這個帖子上面的代碼行,這是相當基礎的,在viewdidLoad方法 之上[self.view addSubview:scrollview];應該有其計算所有的子視圖

scrollview.contentSize = CGSizeMake(self.view.frame.size.width*viewcount,self.view.frame.size.height); 

在該代碼的寬度以下 - (UIView的*)viewForZoomingInScrollView:(UIScrollView中*)滾動視圖{ 方法應該是:

return [scrollView.subviews objectAtIndex:0]; 

這使您可以縮放和平移圖像,並可以很好地瀏覽一組圖像。剛開始研究如何在移動到下一個圖像時自動縮放到原始大小,因爲其頁面沿着頁面放大時放大。

+0

你知道如何調整縮放的子視圖嗎? – malaki1974 2012-07-24 21:03:06