سؤال

وأنا بناء التطبيق استنادا إلى "التمرير" نموذج التعليمات البرمجية التي تقدمها شركة أبل. كل شيء يعمل بشكل جيد جدا. طبيعة الصور التي أريد عرضها، من شأنه أن يجعل من المرغوب فيه، إذا تم عكس ترتيب الصور، وأن أول صورة واضحة لأقصى اليمين، بدلا من ترك أكثر من غيرها. في الأساس، يجب على المستخدم تمرير الوراء، من اليمين إلى اليسار، وليس من اليسار إلى اليمين. ولكن الآن: أنا لا أفهم بناء الجملة أبل تستخدم، وآمل شخص ما يمكن أن يفسر لي ما يجري. وفيما يلي الأجزاء ذات الصلة من التطبيق عينة:

- (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)];
}
هل كانت مفيدة؟

المحلول

وهكذا هذا هو ما انتهى به: أنا أول المقلوب ترتيب subviews، ثم قدم لي قفزة scrollview إلى "إطار" الماضي، وذلك بإضافة الأسطر التالية:

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

وآمل أن يكون هذا هو مفيدة إلى حد ما لشخص ما ...

نصائح أخرى

ويبدو أنك تحتاج إلى تعديل layoutScrollImages. تهيئة curXLoc لعدد أقصى الحاجة، وإنقاص في الحلقة.

- (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)];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top