资源说明:定时滚动和循环滚动,可点击图片和PageController
#import "ASIFormDataRequest.h"
#import "GWPublicClass.h"
@interface ViewController ()
@end
@implementation ViewController
{
UIScrollView * headScrollView;
UIPageControl * pageControl;
NSArray * colorArray;
NSTimer * myTimer;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma UIScrollView delegate
-(void)scrollToNextPage:(id)sender
{
int pageNum = pageControl.currentPage;
CGSize viewSize = headScrollView.frame.size;
CGRect rect = CGRectMake((pageNum+2)*viewSize.width, 0, viewSize.width, viewSize.height);
[headScrollView scrollRectToVisible:rect animated:NO];
pageNum++;
if (pageNum == colorArray.count) {
CGRect newRect=CGRectMake(viewSize.width, 0, viewSize.width, viewSize.height);
[headScrollView scrollRectToVisible:newRect animated:NO];
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageWidth = headScrollView.frame.size.width;
int currentPage = floor((headScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
if (currentPage == 0) {
pageControl.currentPage = colorArray.count-1;
}else if(currentPage == colorArray.count+1){
pageControl.currentPage=0;
}
pageControl.currentPage = currentPage-1;
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[myTimer invalidate];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth = headScrollView.frame.size.width;
CGFloat pageHeigth = headScrollView.frame.size.height;
int currentPage=floor((headScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
NSLog(@"the current offset==%f",headScrollView.contentOffset.x);
NSLog(@"the current page==%d",currentPage);
if (currentPage == 0) {
[headScrollView scrollRectToVisible:CGRectMake(pageWidth*colorArray.count, 0, pageWidth, pageHeigth) animated:NO];
pageControl.currentPage = colorArray.count-1;
NSLog(@"pageControl currentPage==%d",pageControl.currentPage);
NSLog(@"the last image");
return;
}else if(currentPage == [colorArray count]+1){
[headScrollView scrollRectToVisible:CGRectMake(pageWidth, 0, pageWidth, pageHeigth) animated:NO];
pageControl.currentPage=0;
NSLog(@"pageControl currentPage==%d",pageControl.currentPage);
NSLog(@"the first image");
return;
}
pageControl.currentPage=currentPage-1;
NSLog(@"pageControl currentPage==%d",pageControl.currentPage);
}
- (void)pageTurn:(UIPageControl *)sender
{
int pageNum = pageControl.currentPage;
CGSize viewSize = headScrollView.frame.size;
[headScrollView setContentOffset:CGPointMake((pageNum+1)*viewSize.width, 0)];
NSLog(@"myscrollView.contentOffSet.x==%f",headScrollView.contentOffset.x);
NSLog(@"pageControl currentPage==%d",pageControl.currentPage); [myTimer invalidate];
}
- (void)handleTapGesture:(UITapGestureRecognizer*)gesture
{
NSLog(@"UITapGesture被调用了%d",gesture.view.tag);
// ...
}
- (void)viewDidLoad
{
[super viewDidLoad];
colorArray = @[[UIColor redColor],[UIColor greenColor],[UIColor blueColor]];
headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, 320, 200)];
headScrollView.backgroundColor = [UIColor blackColor];
[self.view addSubview:headScrollView];
CGFloat Width= 320;
CGFloat Height= 200;
UIImageView * firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, Height)];
firstImageView.userInteractionEnabled = YES;
firstImageView.tag = 50+colorArray.count-1;
firstImageView.backgroundColor = colorArray[colorArray.count-1];
[headScrollView addSubview:firstImageView];
UITapGestureRecognizer * gestd = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[firstImageView addGestureRecognizer:gestd];
for (int i=0; i
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。