滚动视图(UIScrollView)无限无缝左右切换图片,自动顺序加逆序循环播放图片
2014-08-31 22:12
645 查看
// // LoopView.h // Test_LoopScrollView // // Created by lanouhn on 14-8-30. // Copyright (c) 2014年 vaercly@163.com 陈聪雷. All rights reserved. // #import <UIKit/UIKit.h> @interface LoopView : UIView @property (nonatomic, retain) UIScrollView *scrollView; @property (nonatomic, retain) UIPageControl *pageControl; @end // // LoopView.m // Test_LoopScrollView // // Created by lanouhn on 14-8-30. // Copyright (c) 2014年 vaercly@163.com 陈聪雷. All rights reserved. // #import "LoopView.h" @implementation LoopView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code [self setupScrollView]; [self setupPageControl]; } return self; } - (void)setupScrollView { self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds]; _scrollView.tag = 200; _scrollView.contentSize = CGSizeMake(320 * 7, [UIScreen mainScreen].bounds.size.height); // [_scrollView scrollRectToVisible:CGRectMake(320,0,320,[UIScreen mainScreen].bounds.size.height) animated:NO]; [_scrollView setContentOffset:CGPointMake(320, 0) animated:NO]; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.pagingEnabled = YES; [self addSubview:_scrollView]; [_scrollView release]; for (int i = 0; i < 7; i++) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, [UIScreen mainScreen].bounds.size.height)]; if (i == 0) { imageView.image = [UIImage imageNamed:@"girl_5.jpg"]; } else if (i == 6) { imageView.image = [UIImage imageNamed:@"girl_1.jpg"]; } else { imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"girl_%d", i]ofType:@"jpg"]]; } [_scrollView addSubview:imageView]; [imageView release]; } } - (void)setupPageControl { self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height - 40, 300, 20)]; _pageControl.tag = 100; _pageControl.numberOfPages = 5; _pageControl.currentPage = 0; _pageControl.pageIndicatorTintColor = [UIColor grayColor]; _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor]; [self addSubview:_pageControl]; [_pageControl release]; } - (void)dealloc { self.scrollView = nil; self.pageControl = nil; [super dealloc]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end // // RootViewController.m // Test_LoopScrollView // // Created by lanouhn on 14-8-30. // Copyright (c) 2014年 vaercly@163.com 陈聪雷. All rights reserved. // #import "RootViewController.h" #import "LoopView.h" @interface RootViewController ()<UIScrollViewDelegate> { BOOL _isOrder; } @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization _isOrder = YES; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changePage) userInfo:nil repeats:YES]; } return self; } - (void)loadView { LoopView *loopView = [[LoopView alloc] initWithFrame:[UIScreen mainScreen].bounds]; loopView.backgroundColor = [UIColor grayColor]; self.view = loopView; [loopView release]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. LoopView *loopView = ( LoopView *)self.view; loopView.scrollView.delegate = self; [loopView.pageControl addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:100]; int currentPage = scrollView.contentOffset.x / 320; if (currentPage == 0) { // [scrollView scrollRectToVisible:CGRectMake(320 * 5, 0, 320, [UIScreen mainScreen].bounds.size.height) animated:NO]; [scrollView setContentOffset:CGPointMake(320 * 5, 0) animated:NO]; pageControl.currentPage = 4; } else if (currentPage == 6) { // [scrollView scrollRectToVisible:CGRectMake(320, 0, 320, [UIScreen mainScreen].bounds.size.height) animated:NO]; [scrollView setContentOffset:CGPointMake(320, 0) animated:NO]; pageControl.currentPage = 0; } else { pageControl.currentPage = currentPage - 1; } } - (void)handlePageControl:(UIPageControl *)pageControl { NSLog(@"%d", pageControl.currentPage); UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:200]; // [scrollView scrollRectToVisible:CGRectMake(320 * (pageControl.currentPage + 1),0,320,[UIScreen mainScreen].bounds.size.height) animated:NO]; [scrollView setContentOffset:CGPointMake(320 * (pageControl.currentPage + 1), 0) animated:YES]; } - (void)changePage { //单序版 /* LoopView *loopView = ( LoopView *)self.view; int page = loopView.pageControl.currentPage; page++; page = page > 4 ? 0 : page; loopView.pageControl.currentPage = page; // [loopView.scrollView scrollRectToVisible:CGRectMake(320 * (page + 1), 0, 320, [UIScreen mainScreen].bounds.size.height) animated:NO]; if (page) { [loopView.scrollView setContentOffset:CGPointMake(320 * (page + 1), 0) animated:YES]; } else { [loopView.scrollView setContentOffset:CGPointMake(320 * (page + 1), 0) animated:NO]; } */ //双序版 LoopView *loopView = ( LoopView *)self.view; int page = loopView.pageControl.currentPage; if (_isOrder) { page++; page = page > 4 ? 0 : page; if (!page) { _isOrder = NO; page = 3; } } else { page--; page = page < 0 ? 0 : page; if (!page) { _isOrder = YES; page = 0; } } loopView.pageControl.currentPage = page; [loopView.scrollView setContentOffset:CGPointMake(320 * (page + 1), 0) animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
相关文章推荐
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动 - 郭晓
- UIScrollView+UIpageControl实现图片的循环滚动切换
- OCUI界面设计:滚动视图与分页控件结合NSTimer实现图片自动循环与无限滚动展示
- ios UIScrollView自动滚动 循环滚动视图实现
- UIScrollView(滑动切换图片,图片的循环切换(自动))
- Android 使用ViewPager实现图片左右循环滑动自动播放
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- 图片自动滚动,循环播放Android auto-scroll-view-pager(转载)
- 网易新闻首页的实现,可以添加,移除标签,自动滚动图片,无限左右切换图片
- UIScrollView自动滚动 循环滚动视图实现
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- 利用UIScrollView实现展示图片的无限滚动及自动滚动
- UIScrollView+UIpageControl实现图片的循环滚动切换
- UIScrollView 的图片循环+自动播放
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- UIscrollView图片滚动循环
- iOS解析--自动无限循环UIScrollView原理
- UIScrollview 无缝循环滚动实现
- xSlider 可左右、上下图片切换的无缝滚动效果
- 带左右箭头切换的自动滚动图片JS特效