淘先锋技术网

首页 1 2 3 4 5 6 7

一、

#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>协议中各个方法的触发时机 - 推酷