iOS控件:UIScrollView

一、

#import "RootViewController.h"

#define SelfViewWidth        self.view.bounds.size.width
#define SelfViewHeight       self.view.bounds.size.height//view的高度不會因爲有導航條存在而發生變化
#define SelfScrollViewWidth  SelfViewWidth
#define SelfScrollViewHeight (SelfViewHeight - 64)

@interface RootViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIImageView * imagev1;
@end

二、相關屬性

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    /**
     *  以只有導航條爲例scrollView的原點座標爲(0,0)
     *  當爲yes時(默認),scrollView的原點座標爲(0,64)
     *  當爲no時,原點座標才爲(0,0)
     */
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //創建一個UIScrollView
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SelfViewWidth, SelfViewHeight-64)];//需要減去導航條的高度
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:scrollView];
    //
    UIImage * image1 = [UIImage imageNamed:@"1001"];
    UIImageView * imagev1 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,SelfScrollViewWidth,SelfScrollViewHeight)];
    self.imagev1 = imagev1;
    imagev1.image = image1;
    [scrollView addSubview:imagev1];
    //
    UIImage * image2 = [UIImage imageNamed:@"1002"];
    UIImageView * imagev2 = [[UIImageView alloc] initWithFrame:CGRectMake(SelfScrollViewWidth,0,SelfViewWidth,SelfScrollViewHeight)];
    imagev2.image = image2;
    [scrollView addSubview:imagev2];
    //
    UIImage * image3 = [UIImage imageNamed:@"1002"];
    UIImageView * imagev3 = [[UIImageView alloc] initWithFrame:CGRectMake(0,SelfScrollViewHeight,SelfScrollViewWidth,SelfScrollViewHeight)];
    imagev3.image = image3;
    [scrollView addSubview:imagev3];
    
    //1、contentSize 內容的大小 如果想滾動  contentSize的大小要大於本身Frame的大小
    scrollView.contentSize = CGSizeMake(2 * SelfScrollViewWidth,2*SelfScrollViewHeight);
    scrollView.showsHorizontalScrollIndicator = YES;
    scrollView.showsVerticalScrollIndicator = YES;
    scrollView.alwaysBounceVertical = YES;
    //2、是否可以滾動
    scrollView.scrollEnabled = YES;
    //3、是否停留在scrollView的寬度的倍數處
    scrollView.pagingEnabled = YES;
//    //4、設置內邊距
//    [scrollView setContentInset:UIEdgeInsetsMake(40, 40, 40, 40)];
//    UIView * blueView = [[UIView alloc] initWithFrame:CGRectMake(-40, -40, 10, 10)];
//    blueView.backgroundColor = [UIColor blueColor];
//    [scrollView addSubview:blueView];
//    //5、設置偏移量
//    [scrollView setContentOffset:CGPointMake(-40, -40) animated:NO];
//    //6、讓某個區域顯示
//    [scrollView scrollRectToVisible:CGRectMake(100, 100, 500, 100) animated:YES];
//    UIView * redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 500, 100)];
//    redView.backgroundColor = [UIColor redColor];
//    [scrollView addSubview:redView];
//    UIView * greenView = [[UIView alloc] initWithFrame:CGRectMake(590, 100, 10, 10)];
//    greenView.backgroundColor = [UIColor greenColor];
//    [scrollView addSubview:greenView];
//    //7、 設置最大、最小縮放比例(只有最大、最小縮放比不同縮放纔會起作用)
//    scrollView. maximumZoomScale = 4.0;
//    scrollView. minimumZoomScale = 1.0;
    
    
}

三、相關代理方法

#pragma mark - <UIScrollViewDelegate>

#pragma mark PS:Responding to Scrolling and Dragging
//偏移量發生變化就會調用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // any offset changes
}


//將要拖拽的時候
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    // called on start of dragging (may require some time and or distance to move)
    // Tells the delegate when the scroll view is about to start scrolling the content.
}


//將要結束拖拽的時候
//velocity:手指將要離開時的速度
//velocity:The velocity of the scroll view (in points) at the moment the touch was released.
//targetContentOffset:慣性滑動結束時的偏移量(目標偏移量)
//targetContentOffset:The expected offset when the scrolling action decelerates to a stop.
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    
    /*********************************************************************************************************\
     *  PS:重要說明
     *  velocity:通過這個參數可以判斷滾動的方向
     *  targetContentOffset:通過這個參數可以判斷視圖是否滾動到了邊界。
     *
     \**********************************************************************************************************/
    
    CGFloat x = fabs(velocity.x);
    CGFloat y = fabs(velocity.y);
    
    if (x > y) {
        NSLog(@"水平滑動");
        
        
    }
    else{
        NSLog(@"垂直滑動");
    }
    CGFloat targetOffsetX = targetContentOffset->x;//目標偏移量
    CGFloat targetOffsetY = targetContentOffset->y;//目標偏移量
}




//拖拽結束的時候
//decelerate:手指離開後是否有慣性滑動 yes爲有 no爲沒有
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    /*********************************************************************************************************\
     *  PS:重要說明
     *  decelerate:通過這個參數就可以確定拖拽結束之後是否還有慣性滑動
     *  yes:爲有 那麼減速的代理方法就會被調用
     *  no:爲沒有 那麼減速的代理方法就不會被調用
     *
     \**********************************************************************************************************/
    
    //下面代碼可以禁止慣性滑動
    //    if (decelerate) {
    //        dispatch_async(dispatch_get_main_queue(), ^{
    //            printf("STOP IT!!\n");
    //            [scrollView setContentOffset:scrollView.contentOffset animated:NO];
    //        });
    //    }
    
    
    
    
}


# pragma mark PS:有慣性滑動時纔會來到這兩個代理方法
//將要減速調用:拖拽結束 若有慣性滑動就來到此方法,若拖拽結束時是靜止的就不會來到此方法
//或者設置了 scrollView.pagingEnabled = YES;
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    // called on finger up as we are moving
    //Tells the delegate that the scroll view is starting to decelerate the scrolling movement.
    
}

//減速結束調用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    // called when scroll view grinds to a halt
    //Tells the delegate that the scroll view has ended decelerating the scrolling movement.
    
    CGFloat scrollViewH = scrollView.frame.size.height;
    CGFloat scrollViewW = scrollView.frame.size.width;
    CGFloat contentH = scrollView.contentSize.height;
    CGFloat contentW = scrollView.contentSize.width;
    CGFloat offsetX = scrollView.contentOffset.x;//實時偏移量
    CGFloat offsetY = scrollView.contentOffset.y;//實時偏移量
    NSLog(@"scrollViewH=%lf",scrollViewH);
    NSLog(@"scrollViewW=%lf",scrollViewW);
    NSLog(@"contentH=%lf",contentH);
    NSLog(@"contentW=%lf",contentW);
    NSLog(@"offsetX=%lf",offsetX);
    NSLog(@"offsetY=%lf",offsetY);
    if (offsetY <= 0) {
        NSLog(@"滑動到了頂部");
    }
    if (offsetX <= 0) {
        NSLog(@"滑動到了左邊");
    }
    
    if (offsetY + scrollViewH >= contentH) {
        NSLog(@"滾動到了底部");
    }
    
    if (offsetX + scrollViewW >= contentW) {
        NSLog(@"滾動到了右邊");
    }
    
    
}


#pragma mark PS:點擊狀態欄時纔會來到這兩個代理方法
//是否允許回到頂部(點擊狀態欄時)
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
    // return a yes if you want to scroll to the top. if not defined, assumes YES
    
    return NO;
}

//點擊狀態欄 scrollView 回到頂部時調用
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
    // called when scrolling animation finished. may be called immediately if already at top
}



#pragma mark PS:Responding to Scrolling Animations
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
    //Tells the delegate when a scrolling animation in the scroll view concludes.
    
}

#pragma mark PS:Managing Zooming
// any zoom scale changes 縮放發生變化就會調用
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    NSLog(@"縮放發生變化");
}

// return a view that will be scaled. if delegate returns nil, nothing happens
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    //返回要縮放的子視圖
    return self.imagev1;
}

// called before the scroll view begins zooming its content
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view{
    NSLog(@"將要開始縮放");
}

// scale between minimum and maximum. called after any 'bounce' animations
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale{
    NSLog(@"縮放已經結束");
}


四、內邊距 偏移量說明



參考文章:

1、UIScrollView的屬性總結 - woainilsr - 博客園

2、【急急急】怎麼禁止uiscrollview垂直方向滾動,只允許水平方向滾動!! | iOS開發 - CocoaChina CocoaChina_讓移動開發更簡單

3、關於scrollView禁止慣性滑動與UIScrollView左右滾動判斷 - Man_OC的專欄 - 博客頻道 - CSDN.NET

4、[轉載]有關UIScrollView zoom的一點心得_Hcat陽光_新浪博客

5、UIScrollView控件的常用屬性與<UIScrollViewDelegate>協議中各個方法的觸發時機 - 推酷

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章