1

我想添加无限滚动图像,并且图像滚动没有尽头:

const CGFloat kScrollObjHeight  = 200.0;
const CGFloat kScrollObjWidth   = 320.0;
const NSUInteger kNumImages     = 17;

- (void)layoutScrollImages
{
    UIImageView *view = nil;
    NSArray *subviews = [scrollview1 subviews];
    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);
        }
    }

    [scrollview1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollview1 bounds].size.height)];
}

-(void)viewDidLoad
{
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
    [scrollview1 setBackgroundColor:[UIColor blackColor]];
    [scrollview1 setCanCancelContentTouches:NO];
    scrollview1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollview1.clipsToBounds = YES;
    scrollview1.scrollEnabled = YES;
    scrollview1.pagingEnabled = YES;

    for(int i=0;i< kNumImages;i++)
    {
        NSString *imgName=[[NSString alloc] initWithFormat:@"head%d.png",i];
        UIImage *img=[UIImage imageNamed:imgName];
        UIImageView *imageView=[[UIImageView alloc] initWithImage:img];

        [imageView setFrame:CGRectMake(i* kScrollObjWidth, 0, kScrollObjWidth,kScrollObjHeight)];
        [scrollview1 addSubview:imageView];
        [imageView release];
        [imgName release];
    }

    scrollview1.contentSize= CGSizeMake(kScrollObjWidth* kNumImages, kScrollObjHeight);

    [self layoutScrollImages];

    [super viewDidLoad];   
}
4

2 回答 2

1

只需使用 .h 文件创建一个简单的新项目

#import <UIKit/UIKit.h>

@interface scrollableImageViewController : UIViewController {

IBOutlet UIScrollView *sview;

}

@end

以及使用此方法的 .m 文件

- (void)viewDidLoad {
[super viewDidLoad];

for(int i=0;i<10;i++)
{
    NSString *name=[[NSString alloc] initWithFormat:@"images%d.png",i];
    NSLog(@"%@",name);
    [name    release];
    UIImage *img=[UIImage imageNamed:@"img.png"];
    UIImageView *imageView=[[UIImageView alloc] initWithImage:img];
    [imageView setFrame:CGRectMake(i*57, 0, 57, 57)];
    [sview addSubview:imageView];

    [imageView release];
}
sview.contentSize= CGSizeMake(57*10, 57);
}

并检查结果

于 2012-06-08T05:06:32.330 回答
0

您可以查看 Apples 示例代码PhotoScroller
这是他们WWDC 11 在 Session 中介绍的代码104 - Advanced ScrollView Techniques。如果您是注册的 Apple 开发人员,您可以免费观看此视频(和相应的 PDF)。

于 2012-06-08T07:01:43.607 回答