UIScrollView入門

UIScollView的經常使用屬性和方法:ide

- contentOffset屬性:UIScrollView滾動的位置
- contentSize屬性:UIScrollView內容的尺⼨寸(至關於滾動的範圍)
- bounces屬性:設置UIScrollView是否須要彈簧效果
- alwaysBounceVertical屬性:設置垂直方向是否有彈簧效果(必須在bounces設置爲YES的前提下設置纔有效)
- alwaysBounceHorizontal屬性:設置水平方向是否有彈簧效果(必須在bounces設置爲YES的前提下設置纔有效)
- pagingEnabled屬性:設置UIScrollView的滾動方式是否爲翻頁效果
- scrollEnabled屬性:設置UIScrollView是否能滾動
- showsHorizontalScrollIndicator屬性:是否顯示水平方向的滑動條
- showsVerticalScrollIndicator屬性:是否顯示垂直方向的滑動條
- scrollIndicatorInsets屬性:設置滾動條的位置,能夠經過UIEdgeInsetsMake函數來指定該位置
- indicatorStyle屬性:設置滾動條的風格,有三個可選項:UIScrollViewIndicatorStyleDefault(灰)、UIScrollViewIndicatorStyleBlack(黑)、UIScrollViewIndicatorStyleWhite(白)
- decelerationRate屬性:減速的速率
- scrollsToTop屬性:設置點擊狀態欄是否滑動到scrollView的頂部
- -setContentOffset:animated:方法:設置scrollView的位置
- -scrollRectToVisible:animated:方法:將指定的區域滾動到可視範圍以內,若是該地區已經在可視區域,則什麼都不作。

  下面的例子演示瞭如何顯示一張比屏幕尺寸大的圖片,同時演示瞭如何定製滾動條以及如何經過Tap手勢將點擊的位置移到靠近屏幕中心的位置,還演示了利用UIScrollView來實現縮放功能。函數

#import "ViewController.h"

@interface ViewController () <UIScrollViewDelegate> {
    UIScrollView *myScrollView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed:@"1092.jpg"];
    CGSize imageSize = image.size;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageSize.width, imageSize.height)];
    imageView.image = image;

    // 建立一個滾動視圖對象
    myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

    // 設置不容許拖拽滾動(經過Tap手勢移動)
    myScrollView.scrollEnabled = NO;
    // 設置滾動視圖中內容的尺寸
    myScrollView.contentSize = image.size;
    // 設置關閉滾動視圖的彈簧效果
    myScrollView.bounces = NO;
    // 定製滾動條的樣式
    myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    // 定製滾動條的位置
    myScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 80, 40);
    // 設置滾動條減速的速率(值越大減速越緩慢)
    myScrollView.decelerationRate = 0.5;
    // 隱藏掉水平和垂直滾動條
    myScrollView.showsHorizontalScrollIndicator = NO;
    myScrollView.showsVerticalScrollIndicator = NO;
    // 不容許點狀態欄回到視圖頂部
    myScrollView.scrollsToTop = NO;
    // 綁定委託
    myScrollView.delegate = self;
    // 設置縮放比例
    myScrollView.minimumZoomScale = 0.5;
    myScrollView.maximumZoomScale = 2;


    // 將UIImageView放到滾動視圖上
    [myScrollView addSubview:imageView];
    [self.view addSubview:myScrollView];

    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    // 用一個視圖做爲自定義滾動條
    UIView *myScrollBar = [[UIView alloc] initWithFrame:CGRectMake(screenSize.width - 30, 0, 30, 40)];
    for (int i = 0; i < 3; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 15 + 5 * i, 20, 2)];
        label.backgroundColor = [UIColor whiteColor];
        [myScrollBar addSubview:label];
    }
    myScrollBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    [self.view addSubview:myScrollBar];

    // 給自定義的滾動條添加一個拖拽手勢
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(doScroll:)];
    [myScrollBar addGestureRecognizer:pan];

    // 添加一個Tap手勢將連續兩次點擊屏幕的位置置於靠近屏幕中心的地方
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoThePoint:)];
    tap.numberOfTapsRequired = 2;
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:tap];
}

// 拖拽手勢的回調方法
- (void) doScroll:(UIPanGestureRecognizer *) sender {
    CGPoint point = [sender translationInView:self.view];
    CGPoint center = sender.view.center;
    sender.view.center = CGPointMake(center.x, center.y + point.y);
    CGPoint offsetPoint = myScrollView.contentOffset;
    // 按照目前的設置地圖的移動比例約爲自定義滾動條移動比例的7.5倍
    offsetPoint.y += point.y * 7.5;
    // 判斷滾動視圖的offsetContent有否超出邊界
    if (offsetPoint.y < 0) {
        offsetPoint.y = 0;
    }
    else if (offsetPoint.y > myScrollView.contentSize.height - self.view.bounds.size.height) {
        offsetPoint.y = myScrollView.contentSize.height - self.view.bounds.size.height;
    }
    myScrollView.contentOffset = offsetPoint;
    // 防止效果疊加(旋轉、伸縮和拖拽的手勢都須要還原來防止效果疊加)
    [sender setTranslation:CGPointMake(0, 0) inView:self.view];
}

- (void) gotoThePoint:(UITapGestureRecognizer *) sender {
    CGSize screenSize = self.view.bounds.size;
    CGPoint touchPoint = [sender locationInView:self.view];
    // 計算出點擊的位置和屏幕中心點的差值
    CGFloat dx = touchPoint.x - screenSize.width / 2;
    CGFloat dy = touchPoint.y - screenSize.height / 2;
    // 讓滾動視圖的偏移點的座標加上座標偏移量的值
    CGPoint offsetPoint = myScrollView.contentOffset;
    offsetPoint.x += dx;
    offsetPoint.y += dy;
    // 保證水平方向不會超出地圖的邊界
    if (offsetPoint.x < 0) {
        offsetPoint.x = 0;
    }
    else if (offsetPoint.x > myScrollView.contentSize.width - screenSize.width) {
        offsetPoint.x = myScrollView.contentSize.width - screenSize.width;
    }
    // 保證垂直方向不會超出地圖的邊界
    if (offsetPoint.y < 0) {
        offsetPoint.y = 0;
    }
    else if (offsetPoint.y > myScrollView.contentSize.height - screenSize.height) {
        offsetPoint.y = myScrollView.contentSize.height - screenSize.height;
    }

    [myScrollView setContentOffset:offsetPoint animated:YES];
}

#pragma mark 滾動視圖的回調方法

// 經過滾動視圖縮放其子視圖的回調方法
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)_scrollView {
    return myScrollView.subviews[0];
}

  程序運行效果以下圖所示:ui

這裏寫圖片描述

UIScrollViewDelegate

  UIScrollViewDelegate協議定義了滾動視圖對應的事件的回調方法,下面的代碼展現了其中的一部分方法。spa

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"滾動") ;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"將要開始拖拽") ;
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    NSLog(@"將要中止拖拽") ;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"已經中止拖拽") ;
}

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    NSLog(@"將要開始減速") ;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"已經中止減速") ;
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
    NSLog(@"容許點擊狀態欄滾動到頂部");
    return YES ;
}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
    NSLog(@"滾動到頂部") ;
}

UIPageControl的使用

  UIPageControl類提供指示當前顯示的是多頁面視圖的第幾頁的一排小圓點。.net

  UIPageControl的經常使用屬性:code

  • numberOfPages屬性:設置頁數orm

  • currentPage屬性:當前是頁碼對象

  • hidesForSinglePage屬性:若是設爲YES,在只有一頁的狀況下隱藏表示頁碼的點blog

  • defersCurrentPageDisplay屬性:若是你但願直到有時間執行完你的操做以後,才更新當前指示器當前指示頁,能夠將該屬性設爲YES事件

  • pageIndicatorTintColor屬性:設表示頁碼的點的顏色

  • currentPageIndicatorTintColor屬性:設置當前頁點的顏色

  UIPageControl的經常使用方法:

  • -updateCurrentPageDisplay:方法:更新頁碼指示器到當前頁

#import "ViewController.h"

@interface ViewController () <UIScrollViewDelegate> {
    UIScrollView *scrollView;
    UIPageControl *pageControl;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CGSize screenSize = [UIScreen mainScreen].bounds.size;

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, screenSize.width, screenSize.height)];
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    // 設置滾動視圖啓用翻頁模式
    scrollView.pagingEnabled = YES;
    // 設置滾動視圖內容的尺寸寬度是屏幕寬度的3倍
    scrollView.contentSize = CGSizeMake(3 * screenSize.width, screenSize.height - 20);
    // 給滾動視圖設置委託
    scrollView.delegate = self;

    // 循環建立3個水平排列的UIImageView對象
    // 屏幕的可見範圍以內只能看到UIImageView
    for (int i = 1; i <= 3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        // 每一個UIImageView的橫座標是在前一個的基礎上加上屏幕的寬度
        imageView.frame = CGRectMake((i - 1) * screenSize.width, 0, screenSize.width, screenSize.height - 20);
        [scrollView addSubview:imageView];
    }

    [self.view addSubview:scrollView];

    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, screenSize.height - 40, screenSize.width, 40)];
    pageControl.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    // 設置總共有多少頁
    pageControl.numberOfPages = 3;
    // 設置選中頁的指示器顏色
//    pageControl.currentPageIndicatorTintColor = [UIColor purpleColor];
    // 設置頁碼指示器的顏色
//    pageControl.pageIndicatorTintColor = [UIColor yellowColor];

    [self.view addSubview:pageControl];

}

// 滾動時回調此方法計算滾動到第幾頁
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView {
    pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
}

@end

  運行效果以下圖所示:

這裏寫圖片描述 這裏寫圖片描述

UITextView

  UITextView至關因而高級版本的UILabel和UITextField的合體。

  UITextView的經常使用屬性:

  • editable屬性:設置UITextView可否編輯,若是不編輯就當作UILabel顯示,能夠設置自動識別電話號碼、地址、郵箱,該屬性默認爲YES

  • dataDetectorTypes屬性:設置UITextView是否支持識別電話號碼、地址、郵箱等內容

  • inputView:當UITextView成爲第一響應者時顯示的輸入視圖

  • inputAccessoryView:當UITextView成爲第一響應者時顯示的輸入輔助視圖,在inputView的上方

  下面的例子演示瞭如何使用UITextView以及如何進行數據偵測和拼寫檢查。

#import "ViewController.h"

@interface ViewController () <UITextViewDelegate> {
    UITextView *myTextView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    myTextView = [[UITextView alloc] initWithFrame:CGRectMake(50, 100, 275, 250)];
    myTextView.editable = YES;
    myTextView.dataDetectorTypes = UIDataDetectorTypeAll;
    myTextView.text = @"姓名: 駱昊\n電話: 13812345678\n主頁: http://blog.csdn.net/jackfrued\n地址: 成都市西源大道1899號\nQQ: 12345678\n";
    myTextView.font = [UIFont systemFontOfSize:18];
    myTextView.returnKeyType = UIReturnKeyDone;
    // myTextView.scrollEnabled = NO;

    myTextView.layer.borderWidth = 1;
    myTextView.layer.borderColor = [UIColor blackColor].CGColor;
    myTextView.layer.cornerRadius = 5;

    myTextView.delegate = self;
    [myTextView becomeFirstResponder];
    // myTextView.selectedRange = NSMakeRange(10, 20);

    [self.view addSubview:myTextView];

    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeSystem];
    addButton.frame = CGRectMake(150, 380, 75, 40);
    [addButton setTitle:@"添加" forState: UIControlStateNormal];
    [addButton addTarget:self action:@selector(addButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:addButton];

    UITextChecker *checker = [[UITextChecker alloc] init];
    NSRange range = NSMakeRange(0, myTextView.text.length);
    NSString *language = [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode];
    // NSLog(@"%@", language);
    NSRange errRange = [checker rangeOfMisspelledWordInString:myTextView.text range:range startingAt:0 wrap:NO language:language];
    if (errRange.location != NSNotFound) {
        myTextView.selectedRange = errRange;
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    [textView resignFirstResponder];
}

- (void) addButtonClicked:(UIButton *) sender {
    myTextView.text = [myTextView.text stringByAppendingString:@"Hello, world!\n"];
    [myTextView scrollRangeToVisible:NSMakeRange(myTextView.text.length, 1)];
}

@end

  UITextView退出鍵盤幾種方式:

  1. 若是應用有導航條的,能夠在導航條上面加多一個Done的按鈕,用來退出鍵盤,固然要先實UITextViewDelegate

  2. 若是UITextView中不使用回車鍵,能夠把回車鍵當作退出鍵盤的響應鍵

  3. 自定義其餘加載鍵盤上面用來退出,好比在彈出的鍵盤上面加一個view來放置退出鍵盤的Done按鈕

  4. 點擊其餘區域收起鍵盤

相關文章
相關標籤/搜索