uiscrollview_uiscrollviewdelegate - CSDN
精华内容
参与话题
  • UIScrollView 实现完美缩放

    千次阅读 2017-02-23 10:13:47
    UIScrollView实现缩放非常简单。 1、首先定义缩放的最大比例和最小比例; 比如:#define minScale 1 #define maxScale 22、添加代理UIScrollViewDelegate,并且设置自己创建的UIScrollView对象的代理scrollView....

    UIScrollView实现缩放非常简单。
    1、首先添加代理UIScrollViewDelegate,并且设置自己创建的UIScrollView对象的代理scrollView.delegate = self;;

    2、定义缩放的最大比例和最小比例;
    比如:

    #define minScale  1
    #define maxScale  2
    
    
    [_scrollView setMinimumZoomScale:minScale];
    [_scrollView setMaximumZoomScale:maxScale];
    [_scrollView setZoomScale:minScale animated:YES];

    3、实现代理方法

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
    {
        return scrollVew中要缩放的视图;   
    }
    
    
    // called before the scroll view begins zooming its content缩放开始的时候调用
    - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2){
        NSLog(@"%s",__func__);
    }
    
    // scale between minimum and maximum. called after any 'bounce' animations缩放完毕的时候调用。
    - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
    
    {
        //把当前的缩放比例设进ZoomScale,以便下次缩放时实在现有的比例的基础上
        NSLog(@"scale is %f",scale);
        [_scrollView setZoomScale:scale animated:NO];
    }
    
    // 缩放时调用
    - (void)scrollViewDidZoom:(UIScrollView *)scrollView{
        // 可以实时监测缩放比例
        NSLog(@"......scale is %f",scrollView.zoomScale);
    
    }
    展开全文
  • UIScrollView基本用法详解

    千次阅读 2015-12-01 16:51:07
    UIScrollView基本用法详解

    1、UIScrollView可以提供在屏幕尺寸不够的情况下滑动浏览大区域画面

    基本用法如下:

        UIScrollView * scrollView = [[UIScrollView alloc] init];
        scrollView.frame = CGRectMake(0, 100, width, height - 300);//UIScrollView大小
        [self.view addSubview:scrollView];
        scrollView.backgroundColor = [UIColor orangeColor];
        
        UIImage * image = [UIImage imageNamed:@"yuan.png"];
        UIImageView * imageView = [[UIImageView alloc] initWithImage:image];
        [scrollView addSubview:imageView];
        
        //自动调整宽高
        scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        scrollView.contentSize = imageView.bounds.size;  //重要,这个是设置UIScrollView滚动区域的大小,否则不能滚动

    2、滚动条样式:

    默认:灰色线包围黑色条

        scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
    

    UIScrollViewIndicatorStyleBlack : 黑色条

    UIScrollViewIndicatorStyleWhite : 白色条

    样式枚举:

    typedef NS_ENUM(NSInteger, UIScrollViewIndicatorStyle) {
        UIScrollViewIndicatorStyleDefault,     // black with white border. good against any background
        UIScrollViewIndicatorStyleBlack,       // black only. smaller. good against a white background
        UIScrollViewIndicatorStyleWhite        // white only. smaller. good against a black background
    };

    3、放大缩小:

    须遵守UIScrollViewDelegate协议

        scrollView.delegate = self;
        scrollView.minimumZoomScale = 0.5;
        scrollView.maximumZoomScale = 2.0;
    还需要实现如下方法:

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
        for (id subView in scrollView.subviews) {
            if ([subView isKindOfClass:[UIImageView class]]) {
                return subView;
            }
        }
        return nil;
    }

    最大放大两倍,最小缩小一倍

    4、以页为单位滚动:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = [UIColor whiteColor];
        
        CGFloat width = [[UIScreen mainScreen] bounds].size.width;
        CGFloat height = [[UIScreen mainScreen] bounds].size.height;
        
        
        UIScrollView * scrollView = [[UIScrollView alloc] init];
        scrollView.frame = CGRectMake(0, 100, width, height - 300);//UIScrollView大小
        [self.view addSubview:scrollView];
        scrollView.backgroundColor = [UIColor orangeColor];
        
    
        UIView * view1 = [[UIView alloc] init];
        view1.frame = scrollView.bounds;
        view1.backgroundColor = [UIColor purpleColor];
        [scrollView addSubview:view1];
        
        UIView * view2 = [[UIView alloc] init];
        view2.frame = CGRectMake(width, 0, width, scrollView.frame.size.height);
        view2.backgroundColor = [UIColor redColor];
        [scrollView addSubview:view2];
        
        UIView * view3 = [[UIView alloc] init];
        view3.frame = CGRectMake(width * 2, 0, width, scrollView.frame.size.height);
        view3.backgroundColor = [UIColor greenColor];
        [scrollView addSubview:view3];
        
        scrollView.pagingEnabled = YES;
        scrollView.showsHorizontalScrollIndicator = NO; //隐藏横向滚动条
        scrollView.showsVerticalScrollIndicator = NO; //隐藏竖向滚动条
        
        scrollView.contentSize = CGSizeMake(width * 3, scrollView.frame.size.height);
        
            
    }

    5、UIScrollViewDelegate

    @protocol UIScrollViewDelegate<NSObject>
    
    @optional
    
    //滚动时调用,可以实时监测滚动变化
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView;                                               // any offset changes
    
    //实时监测缩放
    - (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2); // any zoom scale changes
    
    //开始拖动的时候调用
    // called on start of dragging (may require some time and or distance to move)
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
    
    //停止拖动的时候调用(注:停止拖动时,拖动效果会持续一段,慢慢停下)
    // called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest
    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);
    
    
    //结束拖动时调用
    // called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
    
    //开始减速的时候调用
    - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;   // called on finger up as we are moving
    
    //结束减速的时候调用
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;      // called when scroll view grinds to a halt
    
    //设置scrollview动画,动画结束后调用
    - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
    
    
    //返回缩放的可以view,如上例UIImageView
    - (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;     // return a view that will be scaled. if delegate returns nil, nothing happens
    
    //开始缩放的时候调用
    - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2); // called before the scroll view begins zooming its content
    
    //结束缩放的时候调用
    - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale; // scale between minimum and maximum. called after any 'bounce' animations
    
    
    //是否可以拖动到顶部,yes可以否则不可以
    - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;   // return a yes if you want to scroll to the top. if not defined, assumes YES
    
    //可以拖动到顶部时,拖动结束后调用
    - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;      // called when scrolling animation finished. may be called immediately if already at top
    
    @end







    展开全文
  • UIScrollView 属性(全)

    2016-04-13 17:10:35
    UIScrollView 字数779 阅读19 评论0 喜欢0 1.contentOffset 默认CGPointZero,用来设置scrollView的滚动偏移量。 //设置scrollView的滚动偏移量 scrollView.contentOffset = CGPointMake(0,200);...

    UIScrollView

    字数779 阅读19 评论0 

    1.contentOffset

    默认CGPointZero,用来设置scrollView的滚动偏移量。

    //设置scrollView的滚动偏移量

    scrollView.contentOffset = CGPointMake(0,200);

    2.contentSize

    默认CGSizeZero,用来设置scrollView的滚动范围

    //设置scrollView.contentSize = CGSizeMake(self.view.bounds.size.width,self.view.bounds.size.height*2);

    3.contentlnset

    默认UIEdgeLnsetsZero,用来设置scrollViewd的额外滚动区域

    //设置scorllView的额外顶部滚动区域:(UIEdgeInsetsMake是逆时针设置,上左下右)

    scrollView.contentInset = UIEdgeInsetsMake
    (100,0,0,0);

    4.bounces

    默认为YES,用来设置scrollView的弹簧效果

    //取消scrollView的弹簧效果

    scrollView.bounces=NO;

    5.pagingEnabled

    默认为NO,用来设置scrollView是否开启分页.

    //开启分页

    scrollView.pagingEnabled=YES;

    6.scrollEnabled

    默认为YES,用来设置scrollView是否允许滚动.

    // scrollView禁止滚动

    scrollView.scrollEnabled=NO;

    7.showsHorizontalScrollIndicator

    默认为YES,用来设置scrollView是否显示水平滚动条.

    //隐藏水平滚动条

    scrollView.showsHorizontalScrollIndicator=NO;

    8.showsVerticalScrollIndicator

    默认为YES,用来设置scrollView是否显示垂直滚动条.

    //隐藏垂直滚动条

    scrollView.showsVerticalScrollIndicator=NO;

    9.minimumZoomScale

    默认1.0,用来设置scrollView最少缩小比例.

    //设置scrollView允许子视图的最大放大比例

    scrollView.maximumZoomScale=2;

    10.maximumZoomScale

    默认1.0,用来设置scrollView最大放大比例.

    //设置scrollView允许子视图的最小缩放比例

    scrollView.minimumZoomScale=0.8;

    11. delegate

    类要遵守UIScrollViewDelegate协议

    scrollView.delegate = self;声明scrollView的代理是我,这个方法在UIScrollViewDelegate里所以我们要遵守UIScrollViewDelegate这个协议

    11.1当scrollView的偏移量一改变就会调用这个方法,即滚动scrollView就会调用。

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

    11.2当scrollView缩放时,就会调用这个方法

    - (void)scrollViewDidZoom:(UIScrollView *)scrollViewNS_AVAILABLE_IOS(3_2);

    11.3即将拖拽的时候调用.

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

    11.4即将停止拖拽的时候调用

    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView

    withVelocity:(CGPoint)velocity targetContentOffset:(inoutCGPoint *)targetContentOffsetNS_AVAILABLE_IOS(5_0);

    11.5停止拖拽的时候调用。

    - (void)scrollViewDidEndDragging:(UIScrollView*)scrollView

    willDecelerate:(BOOL)decelerate;

    11.6即将减速完成的时候调用。

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

    11.7减速完成的时候调用

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

    11.8返回需要缩放哪个视图,这个视图必须是scrollView里的子视图。

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

    //列如:

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

    {

    returnscrollView.subviews[0];

    }

    11.9 scrollView即将开始缩放

    - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView

    withView:(UIView *)viewNS_AVAILABLE_IOS(3_2);

    11.10 scrollView完成缩放

    - (void)scrollViewDidEndZooming:(UIScrollView*)scrollView withView:(UIView*)view atScale:(float)scale;

    11.11没有开启分页的时候,代理方法调用顺序:

    scrollViewWillBeginDragging –> scrollViewDidScroll-> scrollViewWillEndDragging -> scrollViewDidEndDragging

    11.12开启分页的时候,代理方法调用顺序

    scrollViewWillBeginDragging -> scrollViewDidScroll-> scrollViewDidEndDragging ->scrollViewWillBeginDecelerating ->scrollViewDidScroll-> scrollViewDidEndDecelerating

    12. directionalLockEnabled

    指定控件是否只能在一个方向上滚动

    13. decelerationRate

    改变scrollerView的减速点位置

    14.tracking

    监控当前目标是否正在被跟踪

    15. dragging

    改变scrollerView的减速点位置

    16. delaysContentTouches

    控制视图是否延时调用开始滚动的方法

    17. canCancelContentTouches

    控制控件是否接触取消touch的事件

    18.indicatorStyle

    设定滚动条的样式

    19.decelerating

    监控当前目标是否正在减速


    图片循环查看的实例:








    结果:


    展开全文
  • UIScrollView : UIView &lt;NSCoding&gt;   1.创建一个UIScrollView对象 UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 20, 280, 280)];   2.backgroundColor ...

    UIScrollView : UIView <NSCoding>

     

    1.创建一个UIScrollView对象

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 20, 280, 280)];

     

    2.backgroundColor

    背景颜色

     

    3.contentSize

    设置滚动范围

    eg:scrollView.contentSize = CGSizeMake(1000, 1000);

     

    4.contentOffset

    偏移量 (初始显示)(scrollView  相对于滚动范围的坐标)

    eg:scrollView.contentOffset = CGPointMake(500 , 50);

     

    5.contentInset

    可滚动的范围距离上左下右的距离

    eg:scrollView.contentInset = UIEdgeInsetsMake(100, 50, 0, 0);

     

    6.bounces

    到达边缘之后没有弹动效果

    eg:scrollView.bounces = NO;

     

    7.scrollsToTop

    允许用户点击状态栏返回顶部

    eg:scrollView.scrollsToTop = YES;

     

    8.scrollEnabled

    是否允许用户滚动

    eg:scrollView.scrollEnabled = NO;

     

    9.pagingEnabled

    按页滚动

    eg:scrollView.pagingEnabled = YES;

     

    10.showsHorizontalScrollIndicator

    隐藏横向滚动条    滚动条实际上是加在scrollView上的两个imageView

    eg:scrollView.showsHorizontalScrollIndicator = NO;

     

    11.showsVerticalScrollIndicator

    隐藏纵向滚动条

    eg:scrollView.showsVerticalScrollIndicator = NO;

     

     

    UIScrollViewDelegate

     

    12.maximumZoomScale

    设置scrollView缩放的范围

    eg:scrollView.maximumZoomScale = 2.0;

     

    13.minimumZoomScale

    设置scrollView缩放的范围

    eg:scrollView.minimumZoomScale = 0.5;

     

    14.- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; 

    eg:

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

    {

        //返回一个view  跟着scrillView缩放

        //会把scrollView的contentSize设置为imageView的大小

        //同时只能有一个view跟着scrollView缩放

        return [scrollView.subviews firstObject];

     

    }

     

    15.- (void)scrollViewDidScroll:(UIScrollView *)scrollView; 

    eg:

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView

    {

    //    NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));

    //    NSLog(@"偏移量改变了");

     

        //在偏移量改变的时候,计算当前的页数

        int pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width;

       //通过偏移量改变pageControl的值

        self.pageControl.currentPage = pageNumber;

     

    }

     

    16.- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

    {

        NSLog(@"将要开始拖拽");

    }

     

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

    {

        NSLog(@"已经结束拖拽");

    }

     

    18.- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView

    {

        NSLog(@"将要开始减速");

    }

     

    19.- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

    {

        NSLog(@"已经减速停止");

     

    }

    展开全文
  • UIScrollView 新手教程

    2019-05-19 01:33:05
    本文讲的是UIScrollView 新手教程, Ray的温馨提示:这是本站原先 Objective-C 热门教程的 Swift 升级版。Corinne Krych 将教程升级到了Swift, iOS 9 和 Xcode 7.1.1;原文由教程团队成员Matt Galloway编写。阅读...
  • UIScrollView的基本使用

    千次阅读 2018-06-22 11:49:12
    UIScrollView作为iOS开发里基本控件,是我们第六个需要学习的。下面我来为大家介绍一下UIScrollView的一些常用属性和它们的用法。这里附上UI控件演示的源码地址:https://github.com/LOLR2017/UIKitDemo。源码持续...
  • 第二、UIScrollView的使用大全

    万次阅读 热门讨论 2012-08-03 11:33:07
    UIScrollView 原理详解 ScrollView   UIScrollView   UIScrollView为了显示多于一个屏幕的内容或者超过你能放在内存中的内容。   Scroll View为你处理缩小放大手势,UIScrollView实现...
  • UIScrollViewUIScrollView基本用法、UIScrollView详解,UIScrollView常用方法
  • UIScrollView

    2016-09-19 16:02:41
    UIScrollView的常见属性 • UIScrollView的常用代理方法 • UIScrollView的缩放 • UIScrollView和UIPageControl的分页 • NSTimer的使用  UIScrollView的用法很简单,只需将要展示的内容...
  • UIScrollView 实践经验

    2016-04-26 10:50:05
    UIScrollView(包括它的子类 UITableView 和 UICollectionView)是 iOS 开发中最常用也是最有意思的 UI 组件,大部分 App 的核心界面都是基于三者之一或三者的组合实现。UIScrollView 是 UIKit 中为数不多...
  • UIScrollView的使用大全

    2013-05-06 18:32:22
    Scroll View为你处理缩小放大手势,UIScrollView实现了这些手势,并且替你处理对于它们的探测和回应。其中需要注意的子类是UITableView以及UITextView(用来显示大量的文字)。还有一个UIWebView,尽管那不是...
1 2 3 4 5 ... 20
收藏数 26,517
精华内容 10,606
关键字:

uiscrollview