自定义滚动视图 - 框架外的图像

时间:2011-07-29 10:05:51

标签: xcode uiscrollview scroll

我想为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。

0 个答案:

没有答案