Swift-开发者必备Tips
单例
闭包
泛型拓展
Core Data
断言和异常处理
JSON和Codeable
Swift-App架构
介绍
App设计模式概览
Model-View-Controller
Model-View-ViewModel+协调器(MVVM-C)
网络
Model-View-Controller+ViewState
Model适配器-View绑定器
UIScrollView定时滚动和循环滚动,可点击图片和PageController
定时滚动和循环滚动,可点击图片和PageController
#import "ASIFormDataRequest.h"
#import "GWPublicClass.h"
@interface ViewController ()<UIScrollViewDelegate>
@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<colorArray.count; i++) {
UIImageView * subImageView=[[UIImageView alloc] initWithFrame:CGRectMake(Width*(i+1), 0, Width, Height)];
subImageView.backgroundColor = colorArray[i];
subImageView.userInteractionEnabled = YES;
subImageView.tag = 50+i;
subImageView.frame=CGRectMake(Width*(i+1), 0, Width, Height);
[headScrollView addSubview: subImageView];
UITapGestureRecognizer * gestd = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[subImageView addGestureRecognizer:gestd];
}
UIImageView * lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(Width*(colorArray.count+1), 0, Width, Height)];
lastImageView.userInteractionEnabled = YES;
lastImageView.backgroundColor = colorArray[0];
lastImageView.tag = 50;
[headScrollView addSubview:lastImageView];
headScrollView.contentSize = CGSizeMake(Width*(colorArray.count+2), Height);
headScrollView.pagingEnabled = YES;
headScrollView.delegate = self;
[headScrollView scrollRectToVisible:CGRectMake(Width, 0, Width, Height) animated:YES];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 290, 120, 20)];
pageControl.numberOfPages = colorArray.count;
pageControl.backgroundColor = [UIColor greenColor];
pageControl.enabled = YES;
pageControl.currentPage = 0;
[pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];
}
- (void)enterDetail
{
NSLog(@"tap is ");
}