你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開發-UI (十)UIScrollView 和 UIPageControl運用

iOS開發-UI (十)UIScrollView 和 UIPageControl運用

編輯:IOS開發綜合

知識點:

1.UIScrollView運用

2.UIPageControl運用

@interface RootViewController ()<UIScrollViewDelegate>

=======================

UIScrollView運用

    1.UIScrollView創立

    2.常用屬性

      1)滾動內容大小

@property(nonatomic) CGSize contentSize

//實例化一個滾動視圖

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];

    //設置滾動區域的大小

    scrollView.contentSize = CGSizeMake(KScreenWidth * 3, KScreenHeigth *3);

 

 

      2)能否分頁滾動

@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled

scrollView.pagingEnabled = YES;

 

 

      3)設置內容地位

@property(nonatomic) CGPoint contentOffset

//屏幕的寬度

#define KScreenWidth [UIScreen mainScreen].bounds.size.width

//屏幕的高度

#define KScreenHeigth [UIScreen mainScreen].bounds.size.height

//設置偏移量

    //scrollView.contentOffset = CGPointMake(KScreenWidth, KScreenHeigth);

    //帶動畫設置偏移量

    [scrollView setContentOffset:CGPointMake(KScreenWidth, KScreenHeigth) animated:YES];

 

      4)顯示程度滾動條

@property(nonatomic) BOOL showsHorizontalScrollIndicator

scrollView.showsHorizontalScrollIndicator= NO;

 

      5)顯示垂直滾動條

@property(nonatomic) BOOL showsVerticalScrollIndicator

scrollView.showsVerticalScrollIndicator = NO;

 

      6)彈簧效果

@property(nonatomic) BOOL bounces

//封閉彈簧效果

    scrollView.bounces = NO;

 

=======================

UIScrollView的常用代理辦法

1)行將開端拖拽

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

2)行將中止拖拽

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView 

withVelocity:(CGPoint)velocity targetContentOffset:

(inout CGPoint *)targetContentOffset

3)曾經中止拖拽

scrollViewDidEndDragging:(UIScrollView *)scrollView 

willDecelerate:(BOOL)decelerate

4)行將中止加速

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView

5)曾經中止加速

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

6)點擊形態欄回到頂部

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView

7)曾經滑到頂部

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView

8)縮小減少

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

9)縮放終了

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView 

withView:(UIView *)view atScale:(CGFloat)scale

#pragma mark- UIScrollViewDelegate

//預備開端拖拽

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    NSLog(@"scrollViewWillBeginDragging");

}

//預備中止拖拽

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{

    NSLog(@"scrollViewWillEndDragging");

}

//曾經中止拖拽

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    NSLog(@"scrollViewDidEndDragging");

}

//預備開端加速

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{

    NSLog(@"scrollViewWillBeginDecelerating");

}

//曾經中止加速

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    NSLog(@"偏移量為%@",NSStringFromCGPoint(scrollView.contentOffset));

    NSLog(@"scrollViewDidEndDecelerating");
}

//滾動進程中不斷調用的代理辦法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

//    NSLog(@"偏移量為%@",NSStringFromCGPoint(scrollView.contentOffset));

//    NSLog(@"scrollViewDidScroll");

}

//允許點擊形態欄滑動到頂部

-(BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{

    return YES;

}

//曾經滑動到頂部

-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{

    //NSLog(@"scrollViewDidScrollToTop");

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示" message:@"曾經滑到最頂" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];

    //展現

    [alert show];
}


//設置代理

    _scrollView.delegate = self;

    //設置縮小減少的范圍

    _scrollView.minimumZoomScale = 0.5;

    _scrollView.maximumZoomScale = 2;

    [self.view addSubview:_scrollView];

#pragma mark- UIScrollViewDelegate

//前往需求縮小的視圖

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

    return _imageView;
}

//縮小減少完成之後的回調辦法

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{

    //view -> 以後縮小減少的視圖對象

    //scale -> 縮小減少的系數

    NSLog(@"scale = %f",scale);

    //判別

    if (scale < 1.0) {

        //減少

        view.center = CGPointMake(scrollView.frame.size.width/2.0, scrollView.frame.size.height/2.0);

    }else{

        //縮小

        view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    }
}

 

 

 

 

 

=======================

UIPageControl

    1.UIPageControl創立方式

    2.常用屬性

   

  1)總頁數

@property(nonatomic) NSInteger numberOfPages

//設置總頁數

    _pageCtl.numberOfPages = 4;

@property(nonatomic,retain) UIColor *pageIndicatorTintColor

//設置以後頁碼顏色

    _pageCtl.currentPageIndicatorTintColor = [UIColor greenColor];

//其他頁碼顏色

    _pageCtl.pageIndicatorTintColor = [UIColor redColor];

      2)以後頁數

@property(nonatomic) NSInteger currentPage

@property(nonatomic,retain) UIColor *currentPageIndicatorTintColor

 

 

 

    3.結合UIScrollView

      1)應用代理協議完成UIPageControl以後頁的設置

#pragma mark- UIScrollViewDelegate

//中止加速

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    if (scrollView.contentOffset.x == self.view.frame.size.width * 5) {

        //不能用帶動畫跳轉

        //當挪動到第六個視圖的時分

        //霎時跳轉到第二個視圖

        scrollView.contentOffset = CGPointMake(self.view.frame.size.width , 0);

    }else if (scrollView.contentOffset.x == 0){

        //當挪動到第一個視圖的時分

        //霎時跳轉到第五個視圖

        scrollView.contentOffset = CGPointMake(self.view.frame.size.width * 4 , 0);
    }
    //設置頁碼

    _pageCtl.currentPage = scrollView.contentOffset.x/self.view.frame.size.width - 1;
}

 

    

 

【iOS開發-UI (十)UIScrollView 和 UIPageControl運用】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved