2012-04-20 114 views
0

我在滾動視圖中有一個問題。如何在UIScrollView中自定義分頁?

現在我寫了一個關於圖片庫與滾動視圖的示例。我有大量的圖像添加到滾動視圖。每次顯示3張圖像,問題是如何正確測量滾動。例如:每個滾動的最小值正在移動1個圖像。現在,我認爲每次滾動時,移動的最小圖像都是3.這使我不能停在我想看到的正確圖像上。 以下是代碼。

- (void)layoutScrollImages 
{ 
    UIImageView *view = nil; 
    NSArray *subviews = [scroll subviews]; 

    // reposition all image subviews in a horizontal serial fashion 
    CGFloat curXLoc = 0; 
    for (view in subviews) 
    { 
     if ([view isKindOfClass:[UIImageView class]] && view.tag > 0) 
     { 
      CGRect frame = view.frame; 
      frame.origin = CGPointMake(curXLoc, 0); 
      view.frame = frame; 

      curXLoc += (kScrollObjWidth); 
     } 
    } 

    // set the content size so it can be scrollable 
    [scroll setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scroll bounds].size.height)]; 
} 

#pragma mark - View lifecycle 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // 1. setup the scrollview for multiple images and add it to the view controller 
    // 
    // note: the following can be done in Interface Builder, but we show this in code for clarity 
    [scroll setBackgroundColor:[UIColor blackColor]]; 
    [scroll setCanCancelContentTouches:NO]; 
    scroll.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
    scroll.clipsToBounds = YES;  // default is NO, we want to restrict drawing within our scrollview 
    scroll.scrollEnabled = YES; 

    // pagingEnabled property default is NO, if set the scroller will stop or snap at each photo 
    // if you want free-flowing scroll, don't set this property. 
    scroll.pagingEnabled = YES; 

    // load all the images from our bundle and add them to the scroll view 
    NSUInteger i; 
    for (i = 1; i <= kNumImages; i++) 
    { 
     NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i]; 
     UIImage *image = [UIImage imageNamed:imageName]; 
     UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

     // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
     CGRect rect = imageView.frame; 
     rect.size.height = kScrollObjHeight; 
     rect.size.width = kScrollObjWidth; 
     imageView.frame = rect; 
     imageView.tag = i; // tag our images for later use when we place them in serial fashion 
     [scroll addSubview:imageView]; 
     [imageView release]; 
    } 

    [self layoutScrollImages]; 
} 

回答

1

使用此代碼......

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
if(scroll.contentOffset.y> 320) 
{ 

    int y = scrollView.contentOffset.y; 
    y = y/3; 
    [scrollView setContentOffset:CGPointMake(0, y)]; 
} 
} 

希望,這將幫助你......寒

0

子類的內容視圖,並覆蓋該功能:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { 
if ([self pointInside:point withEvent:event]) { 
    if ([[self subviews] count] > 0) { 
     //force return of first child, if exists 
     return [[self subviews] objectAtIndex:0]; 
    } else { 
     return self; 
    } 
} 
return nil; } 

請參閱https://github.com/taufikobet/ScrollViewCustomPaging