The scroll view (UIScrollView) allows you to seamlessly switch between left and right images, and automatically play back images in reverse order.

Source: Internet
Author: User

The scroll view (UIScrollView) allows you to seamlessly switch between left and right images, and automatically play back images in reverse order.

//// LoopView. h // Test_LoopScrollView /// Created by lanouhn on 14-8-30. // Copyright (c) 2014 vaercly@163.com Chen conglei. all rights reserved. // # import
 
  
@ 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 Chen conglei. 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 Chen conglei. all rights reserved. // # import "RootViewController. h "# import" LoopView. h "@ interface RootViewController ()
  
   
{BOOL _ isOrder;} @ end @ implementation RootViewController-(id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) handle {self = [super initWithNibName: bundle: role]; 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: @ selec Tor (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: N O]; [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) h AndlePageControl :( 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 {// single-order edition/* 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];} * // double-order 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 didreceivemorywarning]; // 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
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.