IOS learning notes-scrollView and iosscrollview

Source: Internet
Author: User

IOS learning notes-scrollView and iosscrollview

Scroll view: add the UIScrollViewDelegate protocol to the root view and declare some object attributes.

@ Interface BoViewController: UIViewController <UIScrollViewDelegate> // The Rolling view object @ property (retain, nonatomic) UIScrollView * scrollView; // the page number of the small dot in the view. @ property (retain, nonatomic) UIPageControl * pageControl; // a dynamic array object that stores images @ property (retain, nonatomic) NSMutableArray * images; @ end/* Ask hovertree.com */

Import images in the program and implement the Code in the. m file:

-(Void) viewDidLoad {[super viewDidLoad]; // initialize scrollView self. scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake (0, 0,320,345)]; // initialize pageControl self. pageControl = [[UIPageControl alloc] initWithFrame: CGRectMake (0,344,320, 36)]; // initializes an array to store the image self of the scroll view. images = [NSMutableArray attributes: [UIImage imageNamed: @ "text1.png"], [UIImage imageNamed: @ "text2.png"], [UIImage imageNamed: @ "text3.png"], [UIImage imageNamed: @ "text4.png"], nil]; // Add scrollView and pageControl to the current view [self. view addSubview: self. scrollView]; [self. view addSubview: self. pageControl]; // set the view background color self. view. backgroundColor = [UIColor blackColor]; // call the setuoPage method [self setupPage: nil];} // change the method implementation of the scrolling view-(void) setupPage :( id) sender {// set delegate self. scrollView. delegate = self; // set the background color self. scrollView. backgroundColor = [UIColor blackColor]; // set to untouch self. scrollView. cancancancelcontenttouches = NO; // set the scroll bar type self. scrollView. indicatorStyle = UIScrollViewIndicatorStyleWhite; // whether to automatically crop excess self. scrollView. clipsToBounds = YES; // sets whether self can be scaled. scrollView. scrollEnabled = YES; // sets whether screen switching self can be performed. scrollView. pagingEnabled = YES; // sets whether to lock itself in the horizontal or vertical direction when dragging. scrollView. directionalLockEnabled = NO; // hide the scroll bar settings (horizontal and vertical) self. scrollView. alwaysBounceHorizontal = NO; self. scrollView. alwaysBounceVertical = NO; self. scrollView. showsHorizontalScrollIndicator = NO; self. scrollView. showsVerticalScrollIndicator = NO; // used to record the number of pages NSUInteger pages = 0; // used to record the x coordinate int originX = 0 of scrollView; for (UIImage * image in self. images) {// create a view UIImageView * pImageView = [[[UIImageView alloc] initWithFrame: CGRectZero] autorelease]; // set the view background color pImageView. backgroundColor = [UIColor colorWithRed: 0.6 green: 0.6 blue: 0.6 alpha: 1.0]; // set the background image of the imageView [pImageView setImage: image]; // set the region CGRect rect = self for imageView. scrollView. frame; rect. origin. x = origct; rect. origin. y = 0; rect. size. width = self. scrollView. frame. size. width; rect. size. height = self. scrollView. frame. size. height; pImageView. frame = rect; // set the display mode of the image content (Adaptive Mode) pImageView. contentMode = UIViewContentModeScaleAspectFill; // Add the view to the current rolling view [self. scrollView addSubview: pImageView]; // The x coordinate of the next view: offset: self. scrollView. frame. size. width. originX + = self. scrollView. frame. size. width; // record the number of imageviews in the scrollView pages ++;} // set the response method of the page number controller [self. pageControl addTarget: self action: @ selector (changePage :) forControlEvents: UIControlEventValueChanged]; // set the total number of pages self. pageControl. numberOfPages = pages; // the current page is the first page self by default. pageControl. currentPage = 0; // set the tag self for the page number controller. pageControl. tag = 110; // set the position of the scroll view [self. scrollView setContentSize: CGSizeMake (originX, self. scrollView. bounds. size. height)];} // how to change the page number-(void) changePage :( id) sender {NSLog (@ "the current index value of the indicator is: % I", self. pageControl. currentPage); // obtain the current view page number CGRect rect = self. scrollView. frame; // set the horizontal coordinates of a view. A chart is 320*460, and the horizontal coordinates increase or decrease by 320 pixel rect at a time. origin. x = self. pageControl. currentPage * self. scrollView. frame. size. width; // set the view ordinate to 0 rect. origin. y = 0; // scrollView visible area [self. scrollView scrollRectToVisible: rect animated: YES];} # pragma mark ----- UIScrollViewDelegate --------- // method for implementing the UIScrollViewDelegate protocol, required-(void) implements :( UIScrollView *) scrollView {// obtain the current view width CGFloat pageWith = scrollView. frame. size. width; // Based on the left and right sliding of the scrolView, switch the current indicator of pageCotrol (set currentPage) int page = floor (scrollView. contentOffset. x-pageWith/2)/pageWith) + 1; // switch the page number and dot self. pageControl. currentPage = page;}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} // release the created object-(void) dealloc {[_ pageControl release]; [_ scrollView release]; [super dealloc];}/* Ask hovertree.com */

Recommended: http://www.cnblogs.com/roucheng/p/3528371.html

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.