Auto-scroll: automatically displays the next image. Scroll: automatically displays the image.

Source: Internet
Author: User

Auto-scroll: automatically displays the next image. Scroll: automatically displays the image.

:

Images and small dots are automatically switched every 1 s.

 

Engineering Drawing:

Code:

RootViewController. h

#import <UIKit/UIKit.h>@interface RootViewController : UIViewController<UIScrollViewDelegate,UIPageViewControllerDelegate>{    UIScrollView *imageScrollView;    UIPageControl *pageControl;    NSInteger changePage;    NSTimer *showtimer;    }@end

 

RootViewController. m

-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. self. title = @ "auto scroll, auto show next image"; // automatically switch to the showtimer page = [nst1_scheduledtimerwithtimeinterval: 1.0f target: self selector: @ selector (changePageNumber) userInfo: nil repeats: YES]; // Add the scroll bar [self addScrollView]; // Add the sliding dot [self addPageControl];} # pragma-mark-functions // Add the scroll bar-(void) addScrollView {imageScrollView = [[Export alloc] failed: CGRectMake (0,100,320,120)]; [imageScrollView setBackgroundColor: [UIColor redColor]; [imageScrollView setContentSize: CGSizeMake (320*4,120)]; [imageScrollView setBounces: NO]; [imageScrollView setDelegate: self]; imageScrollView. pagingEnabled = YES; imageScrollView. showsHorizontalScrollIndicator = NO; imageScrollView. showsVerticalScrollIndicator = NO; [self. view addSubview: imageScrollView]; int I = 0; for (I = 0; I <4; I ++) {UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (320 * I, 0,320,120)]; imageView. image = [UIImage imageNamed: [NSString stringWithFormat: @ "% I .jpg", I]; imageView. tag = 110 + I; imageView. userInteractionEnabled = YES; [imageScrollView addSubview: imageView] ;}// Add a white slider-(void) addPageControl {pageControl = [[UIPageControl alloc] initWithFrame: CGRectMake (145,190,100, 40)]; pageControl. numberOfPages = 4; pageControl. tag= 101; pageControl. currentPage = 0; pageControl. backgroundColor = [UIColor clearColor]; [self. view addSubview: pageControl] ;}# pragma-mark-UIPageViewControllerDelegate-(void) changePageNumber {pageControl. currentPage = pageControl. currentPage + 1; if (pageControl. currentPage = 3) {changePage = changePage + 1;} if (changePage = 2) {pageControl. currentPage = 0; changePage = 0;} [imageScrollView scrollRectToVisible: CGRectMake (pageControl. currentPage * 320.0, 65.0, 320.0, 218.0) animated: YES];}-(void) tap: (UITapGestureRecognizer *) sender {NSLog (@ "tap % ld image ", (long) pageControl. currentPage) ;}# pragma-mark-UIScrollViewDelegate-(void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {int current = scrollView. contentOffset. x/320; NSLog (@ "current: % d", current); pageControl. currentPage = current; if (pageControl. currentPage = 3) {changePage = changePage + 1;} if (changePage = 2) {pageControl. currentPage = 0; [imageScrollView scrollRectToVisible: CGRectMake (pageControl. currentPage * 320.0, 65.0, 320.0, 218.0) animated: YES]; changePage = 0 ;}}

 

Related Article

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.