IOS development-UI (10) Use UIScrollView and UIPageControl,-uiuiscrollview
Knowledge point:
1. Use UIScrollView
2. Use UIPageControl
@ Interface RootViewController () <UIScrollViewDelegate>
======================================
Use UIScrollView
1. Create UIScrollView
2. Common attributes
1) scroll content size
@ Property (nonatomic) CGSize contentSize // instantiate a rolling view UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame: self. view. bounds]; // sets the scrollView of the rolling area. contentSize = CGSizeMake (KScreenWidth * 3, KScreenHeigth * 3 );
2) Whether to scroll by PAGE
@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabledscrollView.pagingEnabled = YES;
3) set the content location
@ Property (nonatomic) CGPoint contentOffset // screen width # define KScreenWidth [UIScreen mainScreen]. bounds. size. width // screen height # define KScreenHeigth [UIScreen mainScreen]. bounds. size. height // set the offset // scrollView. contentOffset = CGPointMake (KScreenWidth, KScreenHeigth); // offset with animation settings [scrollView setContentOffset: CGPointMake (KScreenWidth, KScreenHeigth) animated: YES];
4) display the horizontal scroll bar
@property(nonatomic) BOOL showsHorizontalScrollIndicatorscrollView.showsHorizontalScrollIndicator= NO;
5) display the vertical scroll bar
@property(nonatomic) BOOL showsVerticalScrollIndicatorscrollView.showsVerticalScrollIndicator = NO;
6) Spring Effect
@ Property (nonatomic) BOOL bounces // close the Spring Effect scrollView. bounces = NO;
======================================
Common proxy methods for UIScrollView
1) dragging
-(Void) scrollViewWillBeginDragging :( UIScrollView *) scrollView
2) Stop dragging
-(Void) scrollViewWillEndDragging :( UIScrollView *) scrollView
WithVelocity :( CGPoint) velocity targetContentOffset:
(Inout CGPoint *) targetContentOffset
3) The drag and drop operation has been stopped.
ScrollViewDidEndDragging :( UIScrollView *) scrollView
WillDecelerate :( BOOL) decelerate
4) the slowdown is about to stop
-(Void) scrollViewWillBeginDecelerating :( UIScrollView *) scrollView
5) the deceleration has stopped.
-(Void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView
6) Click the status bar to return to the top.
-(BOOL) scrollViewShouldScrollToTop :( UIScrollView *) scrollView
7) slide to the top
-(Void) scrollViewDidScrollToTop :( UIScrollView *) scrollView
8) zoom in and out
-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView
9) scaled
-(Void) scrollViewDidEndZooming :( UIScrollView *) scrollView
WithView :( UIView *) view atScale :( CGFloat) scale
# Pragma mark-UIScrollViewDelegate // prepare to start dragging-(void) scrollViewWillBeginDragging :( UIScrollView *) scrollView {NSLog (@ "blank") ;}// prepare to stop dragging-(void) events :( UIScrollView *) scrollView withVelocity :( CGPoint) velocity targetContentOffset :( inout CGPoint *) targetContentOffset {NSLog (@ "events") ;}// you have stopped dragging-(void) scrollViewDidEndDragging :( UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {NSLog (@ "scrollViewDidEndDragging");} // prepare to start deceleration-(void) Restart :( UIScrollView *) scrollView {NSLog (@ "scrollViewWillBeginDecelerating");} // stopped deceleration-(void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {NSLog (@ "offset % @", NSStringFromCGPoint (scrollView. contentOffset); NSLog (@ "scrollViewDidEndDecelerating");} // proxy method that is always called during the rolling process-(void) scrollViewDidScroll :( UIScrollView *) scrollView {// NSLog (@ "offset % @", NSStringFromCGPoint (scrollView. contentOffset); // NSLog (@ "scrollViewDidScroll");} // you can click the status bar to slide to the top-(BOOL) scrollViewShouldScrollToTop :( UIScrollView *) scrollView {return YES ;} // slide to the top-(void) scrollViewDidScrollToTop :( UIScrollView *) scrollView {// NSLog (@ "blank"); UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "tip" message: @ "slide to the top" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; // display [alert show];} // sets proxy _ scrollView. delegate = self; // set the zoom-in and zoom-out range _ scrollView. min imumzoomscale = 0.5; _ scrollView. maximumZoomScale = 2; [self. view addSubview: _ scrollView]; # pragma mark-UIScrollViewDelegate // return the view to be enlarged-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView {return _ imageView ;} // callback method after zoom in and out-(void) scrollViewDidEndZooming :( UIScrollView *) scrollView withView :( UIView *) view atScale :( CGFloat) scale {// view-> current zoom in and out view object // scale-> zoom in and out coefficient NSLog (@ "scale = % f", scale ); // determine if (scale <1.0) {// zoom out the view. center = CGPointMake (scrollView. frame. size. width/2.0, scrollView. frame. size. height/2.0);} else {// enlarge the view. frame = CGRectMake (0, 0, view. frame. size. width, view. frame. size. height );}}
======================================
UIPageControl
1. UIPageControl Creation Method
2. Common attributes
1) Total number of pages @ property (nonatomic) NSInteger numberOfPages // set the total number of pages _ pageCtl. numberOfPages = 4; @ property (nonatomic, retain) UIColor * pageIndicatorTintColor // set the color of the current page number _ pageCtl. currentPageIndicatorTintColor = [UIColor greenColor]; // other page color _ pageCtl. pageIndicatorTintColor = [UIColor redColor]; 2) Current page number @ property (nonatomic) NSInteger currentPage @ property (nonatomic, retain) UIColor * currentPageIndicatorTintColor
3. Combined with UIScrollView
1) Use the proxy protocol to complete UIPageControl settings on the current page
# Pragma mark-UIScrollViewDelegate // stop deceleration-(void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {if (scrollView. contentOffset. x = self. view. frame. size. width * 5) {// cannot jump with an animation // when moving to the sixth view // instantly jump to the second view scrollView. contentOffset = CGPointMake (self. view. frame. size. width, 0);} else if (scrollView. contentOffset. x = 0) {// when moving to the first view, // instantly jump to the fifth view scrollView. contentOffset = CGPointMake (self. view. frame. size. width * 4, 0);} // set page number _ pageCtl. currentPage = scrollView. contentOffset. x/self. view. frame. size. width-1 ;}