2012-04-20 11 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をご覧ください。

関連する問題