UIScrollView 图片循环滚动

1:假如有6个图片:那个,Scrollview的大小加 7 个图片的大小

2:

复制代码
  //ImageScrollView;
    UIScrollView *imageScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 210)];
    imageScroll.bounces = YES;
    imageScroll.pagingEnabled = YES;
    imageScroll.userInteractionEnabled = YES;
    imageScroll.showsVerticalScrollIndicator = NO;
    imageScroll.showsHorizontalScrollIndicator = NO;
    imageScroll.delegate = self;
    imageScroll.contentSize = CGSizeMake(320 * 7, 210);
    [theScrollView addSubview:imageScroll];
    [imageScroll release];
    
    //加上图片;
    for (int i = 0; i < 7; i++) {
        UIImageView *aImageView = [[UIImageView alloc]initWithFrame:kCR(0 + 320*i, 0, 320, 210)];
        aImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"keenLC%d.jpg",i+1]];
        if (i==6) {
            aImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"keenLC%d.jpg",1]];
        }

        
        [imageScroll addSubview:aImageView];
        [aImageView release];
        
    }
    //图片数字label
    imageNumLabel = [[UILabel alloc]initWithFrame:kCR(130, imageScroll.bottom - 27, 60, 25)];
    imageNumLabel.text = @"1/6";
    imageNumLabel.textAlignment = NSTextAlignmentCenter;
    imageNumLabel.textColor = [UIColor whiteColor];
    imageNumLabel.backgroundColor = [UIColor clearColor];
    [theScrollView addSubview:imageNumLabel];
    [imageNumLabel release];
复制代码

3: ScrollView的代理

复制代码
#pragma mark Scroll delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;{
    int currentPage = floor((scrollView.contentOffset.x - scrollView.frame.size.width / 2) /  scrollView.frame.size.width) + 1;
    imageNumLabel.text = [NSString stringWithFormat:@"%d/6",currentPage + 1];
    //imageScroll.transform = CGAffineTransformMakeScale(1,1);
    if (currentPage==6) {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        imageNumLabel.text = [NSString stringWithFormat:@"%d/6",1];
    }
}
复制代码

 

posted @   cocoajin  阅读(473)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示