UI基于UIScrollView实现图片组手动替换和自动轮播
UI基于UIScrollView实现图片组手动轮换和自动轮播
#import "RootViewController.h" @interface RootViewController (){ UIPageControl * _pageControl; NSTimer * _timer; UIScrollView * scrV; UILabel * label; } @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization }
return self; } - (void)viewDidLoad { [super viewDidLoad]; [self creatScrollView]; [self creatPageController]; [self addTimer]; // Do any additional setup after loading the view. } -(void)addTimer{ _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(toNextPage) userInfo:nil repeats:YES]; } -(void)toNextPage{ NSInteger page= _pageControl.currentPage; if (page==0) { scrV.contentOffset=CGPointMake(0, 0); } page++; CGFloat X=page*self.view.frame.size.width; [UIView animateWithDuration:1 animations:^{ scrV.contentOffset=CGPointMake(X, 0); }]; if(page==8){ page=0; } _pageControl.currentPage=page; label.text=[NSString stringWithFormat:@"%d/8",page+1]; } -(void)creatPageController{ _pageControl =[[UIPageControl alloc]initWithFrame:CGRectMake(0, 400, 320, 40)]; _pageControl.backgroundColor=[UIColor blackColor]; _pageControl.alpha=0.5; _pageControl.numberOfPages=8; _pageControl.currentPage=0; [self.view addSubview:_pageControl]; label=[[UILabel alloc]initWithFrame:CGRectMake(270, 460, 50, 20)]; label.text=@"1/8"; label.backgroundColor=[UIColor cyanColor]; label.textAlignment=NSTextAlignmentCenter; [self.view addSubview:label]; } -(void)creatScrollView{ scrV=[[UIScrollView alloc]initWithFrame:self.view.bounds]; for(int i = 0; i < 9; i++){ UIImageView * imageV=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*i, 0, 320,self.view.frame.size.height)]; [imageV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"动漫%02d.jpg",i+1]]]; [scrV addSubview:imageV]; } scrV.contentSize=CGSizeMake(self.view.frame.size.width*9, 0); scrV.contentOffset=CGPointMake(0, 0); scrV.pagingEnabled=YES; scrV.showsVerticalScrollIndicator=NO; scrV.delegate=self; [self.view addSubview:scrV]; } #pragma mark **UIScrollViewDelegate** -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSLog(@"减速"); NSInteger page=scrV.contentOffset.x/320; if(page==8){ scrollView.contentOffset=CGPointMake(0, 0); page=0; } _pageControl.currentPage=page; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
UIScrollView滚动条控件 大多应用于手机软件中的图片展览