UIKit 框架之UIScrollView

理解UIScrollView時能夠假設這樣的一個場景:在一個漆黑的屋子裏的牆上有一副大的畫,牆是能夠上下左右滾動,一個矩形的手電筒固定着,開始時畫的左上角座標與矩形光的左上角座標對應。人能夠用手電筒的光來看這幅畫。動畫

 

//
//  ViewController.m
//  UIScrollView
//
//  Created by City--Online on 15/5/20.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong) UIScrollView *scrollView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //Frame相似矩形手電筒發出的光 眼睛每次只能看到矩形大小的東西
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 100, self.view.bounds.size.width, 100)];
    //畫布
    _scrollView.contentSize=CGSizeMake(self.view.bounds.size.width*3, self.view.bounds.size.height);
    //畫布裏牆角的距離 相似CSS的盒子模型  contentSize、contentInset共同組成整塊牆
    _scrollView.contentInset=UIEdgeInsetsMake(10, 10, 10, 10);
    //設置代理
    _scrollView.delegate=self;
    //指定控件是否只能在一個方向上滾動
    _scrollView.directionalLockEnabled=YES;
    //控制控件遇到邊框是否反彈
    _scrollView.bounces=YES;
    //控制垂直方向遇到邊框是否反彈
    _scrollView.alwaysBounceVertical=NO;
    //控制水平方向遇到邊框是否反彈
    _scrollView.alwaysBounceHorizontal=NO;
    //控制控件是否整頁翻動
    _scrollView.pagingEnabled=YES;
    //控制控件是否能滾動
    _scrollView.scrollEnabled=YES;
    //控制是否顯示水平方向的滾動條
    _scrollView.showsHorizontalScrollIndicator=YES;
    //控制是否顯示垂直方向的滾動條
    _scrollView.showsVerticalScrollIndicator=YES;
    //指定滾動條在scrollerView中的位置
    _scrollView.scrollIndicatorInsets=UIEdgeInsetsMake(0, 50, 0, 0);
    //設定滾動條的樣式
    _scrollView.indicatorStyle=UIScrollViewIndicatorStyleBlack;
    //減速速率
    _scrollView.decelerationRate=3.0;
    
    //    //設置牆左上角座標移動到指定的Point
    //    [_scrollView setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:YES];
    //    //滾動到指定矩形的邊緣 使得該矩形可見
    //    [_scrollView scrollRectToVisible:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height) animated:YES];
    //顯示滾動條,可是滾動條(ScrollBar Indicators) 只會顯示大概1.5秒左右的時間,就會自動隱藏
    //    [_scrollView flashScrollIndicators];
    
    //若是用戶已經觸及的內容視圖但可能尚未開始拖動它。這個屬性的值即是YES
    BOOL IsTracking =_scrollView.tracking;
    NSLog(@"tracking=%d",IsTracking);
    
    //是否在被拖拽
    BOOL IsDragging=_scrollView.dragging;
    NSLog(@"dragging=%d",IsDragging);
    
    //是否在減速
    BOOL  IsDecelerating=_scrollView.decelerating;
    NSLog(@"decelerating=%d",IsDecelerating);
    
    //是否延時調用開始滾動的方法 if NO, we immediately call -touchesShouldBegin:withEvent:inContentView:
    _scrollView.delaysContentTouches=YES;
    
    //是否接觸取消touch的事件
    _scrollView.canCancelContentTouches=YES;
    
    //縮小的最小值
    _scrollView.minimumZoomScale=0.5;
    //放大的最大值
    _scrollView.maximumZoomScale=2;
    //當前縮放值
    _scrollView.zoomScale=1;
    
    //縮放到指定的大小
    //    - (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated
    //縮放到指定的Rect
    //    - (void)zoomToRect:(CGRect)rect animated:(BOOL)animated
    
    //控件的大小是否正在改變
    BOOL IsZooming= _scrollView.zooming;
    NSLog(@"zooming=%d",IsZooming);
    
    //設置縮放到邊界時是否反彈
    _scrollView.bouncesZoom=YES;
    
    //是否進行縮放反彈
    BOOL IsZoomBouncing= _scrollView.zoomBouncing;
    NSLog(@"zoomBouncing=%d",IsZoomBouncing);
    
    //滾動到頂部
    BOOL IsScrollsTop= _scrollView.scrollsToTop;
    NSLog(@"scrollsToTop=%d",IsScrollsTop);
    
    //開始拖到時隱藏鍵盤
    _scrollView.keyboardDismissMode=UIScrollViewKeyboardDismissModeOnDrag;
    
    //獲取拖動手勢
    UIGestureRecognizer *panGestureRecognizer=_scrollView.panGestureRecognizer;
    //獲取撮合手勢
    UIGestureRecognizer *pinchGestureRecognizer=_scrollView.pinchGestureRecognizer;
    
    for (int i=0; i<3; i++) {
        UIImageView *imgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];
        imgView.frame=CGRectMake(self.view.bounds.size.width*i, 0, self.view.bounds.size.width, self.view.bounds.size.height) ;
        [_scrollView addSubview:imgView];
    }
    _scrollView.userInteractionEnabled=YES;
    _scrollView.scrollEnabled=YES;
    
    
    [self.view addSubview:_scrollView];
//    self.view.backgroundColor=[UIColor yellowColor];
    
}

//UIScrollViewDelegate
//視圖滾動時觸發
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidScroll");
}
//視圖縮放時觸發
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidZoom");
}
//開始拖動時觸發
- (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(@"scrollViewDidEndDecelerating");
    
}
//結束滾動動畫調用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{}
//返回要縮放的視圖
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return nil;
}
//開始縮放
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
    NSLog(@"scrollViewWillBeginZooming");
}
//縮放完成
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    NSLog(@"scrollViewDidEndZooming");
}

//是否容許滾動到頂部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewShouldScrollToTop");
    return YES;
}
//滑動到頂部
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidScrollToTop");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

相關文章
相關標籤/搜索