Question

Je suis en train de créer une application basée sur le " Défilement " exemple de code fourni par Apple. Tout fonctionne très bien. La nature des images que je veux afficher rendrait cela souhaitable, si l'ordre des images est inversé et que la première image visible est celle qui se trouve le plus à droite, et non la plus à gauche. Fondamentalement, l'utilisateur doit faire défiler en arrière, de droite à gauche, plutôt que de gauche à droite. Mais maintenant: je ne comprends pas la syntaxe utilisée par Apple et j'espère que quelqu'un pourra m'expliquer ce qui se passe. Voici les parties pertinentes de l'exemple d'application:

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

    // 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
        [scrollView1 addSubview:imageView];
        [imageView release];
    }

    [self layoutScrollImages];  // now place the photos in serial layout within the scrollview

}

- (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)];
}
Était-ce utile?

La solution

Donc, voici ce que j'ai fini par faire: J'ai d’abord inversé l’ordre des vues secondaires, puis j’ai fait passer la vue à la dernière image, en ajoutant les lignes suivantes:

CGPoint lastFrame = CGPointMake(((kNumImages -1) * kScrollObjWidth), 0.0f);
[scrollview setContentOffset:lastFrame];

J'espère que cela servira en quelque sorte à quelqu'un ...

Autres conseils

Il semble que vous ayez besoin de modifier layoutScrollImages. Initialisez curXLoc sur le nombre maximal requis et décrémentez-le dans la boucle.

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

    // reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = kNumImages * kScrollObjWidth;
    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)];
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top