循环滚动一个UIScrollView

本文介绍了一个iOS应用中实现的滑动视图控制器,通过UIScrollView实现页面间的切换效果,并展示了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//  testScrollViewViewController.m

//  testScrollView

//  Created by cash on 11-7-4.

//  Copyright 2011 xbiii3s@gmail.com. All rights reserved.

#import "testScrollViewViewController.h"

@implementation testScrollViewViewController

@synthesize scrollView, slideImages;

#define WIDTH_OF_SCROLL_PAGE 320

#define HEIGHT_OF_SCROLL_PAGE 460

#define WIDTH_OF_IMAGE 320

#define HEIGHT_OF_IMAGE 460

#define LEFT_EDGE_OFSET 0

- (void)viewDidLoad {

    scrollView = [[UIScrollView alloc] init];

    CGRect scrollFrame;

    scrollFrame.origin.x = 0;

    scrollFrame.origin.y = 0;  

    scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;

    scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;

    scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];

    scrollView.bounces = YES;

    scrollView.pagingEnabled = YES;

    scrollView.delegate = self;

    scrollView.userInteractionEnabled = YES;

    slideImages = [[NSMutableArray alloc] init];

    [slideImages addObject:@"IMG_0116.PNG"];

    [slideImages addObject:@"IMG_0118.PNG"];

    [slideImages addObject:@"IMG_0119.PNG"];

    [slideImages addObject:@"main_bg.png"];

    //add the last image first

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]]

    imageView.frame = CGRectMake(LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);

    [scrollView addSubview:imageView];

    [imageView release];

    for (int i = 0;i<[slideImages count];i++) {

        //loop this bit

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:[slideImages objectAtIndex:i]]];

        imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);

        [scrollView addSubview:imageView];

        [imageView release];

    }

    //add the first image at the end

    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImagesobjectAtIndex:0]]];

    imageView.frame = CGRectMake((WIDTH_OF_IMAGE * ([slideImages count] + 1)) + LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);

    [scrollView addSubview:imageView];

    [imageView release];

    [scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] + 2), HEIGHT_OF_IMAGE)];

    [scrollView setContentOffset:CGPointMake(0, 0)];

    [self.view addSubview:scrollView];

    [self.scrollViewscrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO]; 

    [super viewDidLoad];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    int currentPage = floor((self.scrollView.contentOffset.x - self.scrollView.frame.size.width / ([slideImages count]+2)) / self.scrollView.frame.size.width) + 1;

    if (currentPage==0) {

        //go last but 1 page

        [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE * [slideImagescount],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];

    } else if (currentPage==([slideImages count]+1)) { //如果是最后+1,也就是要开始循环的第一个

        [self.scrollViewscrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];

    }

}

- (void)didReceiveMemoryWarning {

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.

}

- (void)viewDidUnload {

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}

- (void)dealloc {

    [scrollView release];

    [slideImages release];

    [super dealloc];

}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值