Пользовательский вид прокрутки - изображение вне рамки - PullRequest
1 голос
/ 29 июля 2011

Я бы хотел сделать бесконечный скроллинг для iPad.Прокрутка работает, только когда я добавляю изображения в свой настраиваемый класс scrollview, он также появляется вне рамки представления (поэтому у меня есть представление, в котором есть UIScrollView. Это представление прокрутки связано с моими CustomScroll.h: UIScrollView и CustomScroll.m файлы).Почему изображения появляются за пределами рамки вида (для рамки вида в файле nib установлено значение 320x420)?

CustomScroll.h: UIScrollView

Часть из файла CustomScroll.m:

- (id)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder])){
    self.contentSize = CGSizeMake(2000, 416);
    [self setShowsVerticalScrollIndicator:NO];

    for (int i=1; i<=4; i++){
        [self addImageAtPosition:i];
    }

    //todo: to replace with the method: addFirstElementToTheEnd
    UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.jpg"]];
    [image1 setFrame:CGRectMake(5*320, 0, 320, 416)];
    [self addSubview:image1];
    [image1 release];

    //todo: to replace with the method: addLastElementToTheBeginning
    UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image4.jpg"]];
    [image2 setFrame:CGRectMake(0*320, 0, 320, 416)];
    [self addSubview:image2];
    [image2 release];

}

return self;

}

- (void)addImageAtPosition:(NSInteger)position{
    UIImageView *tempImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg", position]]];
    [tempImage setFrame:CGRectMake((position * 320), 0, 320, 416)];
    [self addSubview:tempImage];
    [tempImage release];
}

- (void)recenterIfNecessary{
    CGFloat currentOffsetX = [self contentOffset].x;

    if (currentOffsetX > 1600.00){
        self.contentOffset = CGPointMake(320, [self contentOffset].y );
    }
    if (currentOffsetX < 320.00){
        self.contentOffset = CGPointMake(5*320, [self contentOffset].y);
    }
}

- (void) layoutSubviews{
    [super layoutSubviews];

    [self recenterIfNecessary];

}

TY.

...