1

Appleのドキュメントとそのサンプルコードhttp://developer.apple.com/iphone/library/samplecode/Scrolling/index.htmlを使用してUIScrollviewを学習しようとしていますが、非常に単純なものが私を逃れています。

画面に現在表示されている画像をどのように判断しますか。水平スクロールビューで画像の1つを選択した場合、画像のファイル名、または配列内のポインターを取得して、さらに何かを行うにはどうすればよいですか。画像で?

ページコントロールを有効にすると、ページ番号を見つけて画像にマッピングできるかもしれないと思いました。ページを数えるために減速を数えることを考えましたが、十分にフリックしないとそれが増加し、誤った数になります。

私が考えることができる最後のことは、contentOffSetを取得し、画像サイズで割ることです。これにより、1、2、3が得られ、配列を指すことができます(今夜試すには疲れすぎています...多くを無駄にする前に尋ねるかもしれません)より多くの時間 ;-) )。

他のアイデアはありますか?フォトアルバムアプリのどこかで使う方法があるのではないかと思いました。

PS:コードは次のとおりです。

- (void)layoutScrollImages
{
    UIImageView *view = nil;
    NSArray *subviews = [scrollView1 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
    [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

    // 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
    [scrollView1 setBackgroundColor:[UIColor blackColor]];
    [scrollView1 setCanCancelContentTouches:NO];
    scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView1.clipsToBounds = YES;        // default is NO, we want to restrict drawing within our scrollview
    scrollView1.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.
    scrollView1.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:@"Card %d.png", 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
        [scrollView1 addSubview:imageView];
        [imageView release];
    }
    
    [self layoutScrollImages];  // now place the photos in serial layout within the scrollview
4

1 に答える 1

2

これはぐっすり眠った後は簡単でした!

CGPoint p = scrollView1.contentOffset;
NSLog(@"x = %f, y = %f", p.x, p.y);

ここで、320 で割り (水平および全画面画像の場合)、1 を追加します (0 から始まるため)。

これが他の誰かに役立つことを願っています!

ポール

于 2009-06-06T21:43:22.097 に答える