[置顶] ios 无限循环翻页源码例子

原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9923053

demo功能:ios 无限循环翻页源码例子。iphone 6.1测试通过。

demo说明:MyScrollView.m中又主要的页面控制代码。使用 UIScrollView和 UIPageControl控件。通过在滚动结束时设置uiscrollview的内容起始坐标来实现无限制循环。

demo截屏:

[置顶] ios 无限循环翻页源码例子 [置顶] ios 无限循环翻页源码例子


demo主要代码:MyScrollView.m

 

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        

        //UIScrollView

        carScroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 748)];

		[carScroll setContentSize:CGSizeMake(1024*(TOTLE_PAGE+1), 100)];

		[carScroll setShowsHorizontalScrollIndicator:NO];

		[carScroll setPagingEnabled:YES];

		[carScroll setDelegate:self];

        

		for(int i=0; i<TOTLE_PAGE+1; i++) {

            UIImageView *scrollImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]] ;

            scrollImg.frame = CGRectMake(1024*i, 0, 1024, 748);

            scrollImg.tag = 2220+i;

            scrollImg.userInteractionEnabled = YES;

            UITapGestureRecognizer *tapGestrue = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

            [scrollImg addGestureRecognizer:tapGestrue];

            [tapGestrue release], tapGestrue = nil;

            [carScroll addSubview:scrollImg];

            [scrollImg release], scrollImg = nil;

		}

		[self addSubview:carScroll];

		

		

		//carPageControl

		carPageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(380, 700, 350, 26)];

		[carPageControl setNumberOfPages:TOTLE_PAGE];

		[carPageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];

        [self addSubview:carPageControl];

        

    }

    return self;

}



#pragma mark - 

#pragma ScrollView Delegate



-(void) scrollViewDidScroll:(UIScrollView *)scrollView 

{

    int offsetX = scrollView.contentOffset.x;

	if(offsetX < 0){

		[scrollView setContentOffset:CGPointMake(1024*TOTLE_PAGE, 0)];

	}

	if(offsetX > 1024*TOTLE_PAGE){

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

	}

}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    int offsetX = scrollView.contentOffset.x;

	if(offsetX/1024 == TOTLE_PAGE){

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

        carPageControl.currentPage = 0;

	}

	if(offsetX%1024 == 0){

		[carPageControl setCurrentPage:scrollView.contentOffset.x/1024];

	}

}

 


demo下载地址:http://download.csdn.net/detail/donny_zhang/5914159

 

你可能感兴趣的:(ios)