UIScrollView

限于喜欢 提交于 2020-01-31 23:00:46

UIScrollView的基本使用

将需要显示的内容添加到UIScrollView中
设置UIScrollView的contentSize属性,告诉UIScrollView所有内容的尺寸,也就是告诉它滚动的范围

//ViewController.m
#import "ViewController.h"
@interface ViewController ()
//连线
@property (nonatomic,weak) IBOutlet UIScrollView *scrollView;
@end

@implementation ViewController
-(void)viewDidLoad {
    [super viewDidLoad];
    //1.红色的view
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColoe];
    redView.frame = CGRectMake(0,0,30,40);
    [self.scrollView addSubviews:redView];
    //设置内容超出UIScrollView后仍然能看见
    self.scrollView.clipsToBounds = YES;
    //2.设置内容尺寸
    self.scrollView.contentSize = CGSizeMake(400,500);
    //3.是否可滚动
    self.scrollView.scrollEnable = YES;
    //4.用户交互
    self.scrollView.userInterfaceEnable = YES;
    //注意:两者的区别在于:
    //scrollEnable只是设置scrollView能否滚动,而userInterfaceEnable是设置UIScrollView包括其中的子控件是否响应用户交互,设置userInterfaceEnable为No后,则scrollView中所有的子控件都不能滚动。
}
@end

例: 显示一张大图

//ViewController.m
#import "ViewController.h"
@interface ViewController ()
//连线
@property (nonatomic,weak) IBOutlet UIScrollView *scrollView;
@end

@implementation ViewController
-(void)viewDidLoad {
    [super viewDidLoad];
   UIImageView *imageView = [[UIImageView alloc] init];
   imageView.image = [UIImage imageNamed:@"123"];
   CGFloat width = imageView.frame.size.width;
   CGFloat height = imageView.frame.size.height;
   [self.scrollView addSubviews:imageView];
   self.scrollView.contentSize = CGSizeMake(width,height);
}
@end

UIScrollView常见的属性

//1.是否有弹簧效果
self.scrollView.bounces = NO;
//2.是否显示滚动条
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
[self.scrollView subViews];   //除了自己添加的子控件外,还会显示滚动条   
需要注意千万不要通过索引去访问通过subViews数组的子控件。
//内容的偏移量,左上角距离UIScrollView的距离
//作用:1.控制内容滚动的位置
//3.得知内容滚动的位置,UIScrollView左上角-左上角的距离
self.scrollView.contentOffset = CGPointMake(200,200);
//4.内边距,内容区域距离UIScrollView的距离
self.scrollView.contentInset = UIEdgeInsetMake(10,20,30,40);

UIScrollView的delegate

很多时候,我们想在UIScrollView正在滚动或滚动到某个位置或者停止滚动时做一些特定的操作。而要想实现这些功能,前提条件就是能够监听到UISccrollView的整个滚动过程。
当UIScrollView发生一系列的滚动操作时,会自动通知它的代理对象,给它的代理对象发送消息,让代理得知它的滚动情况。

也就是说,要想监听UIscrollView的滚动过程,需要设置UIScrollView的代理对象。UIScrollView的代理必须遵守UIScrollViewDelegate协议

#import "UIScrollView.h"
@interface ViewController() <UIScrollViewDelegate>

@end
@implementation ViewControllView
-(void)viewDidLoad{
    [super viewDidLoad];
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor redColor];
    scrollView.frame = CGRectMake(0,20,300,400);
    [self.view addSubview:scrollView];
    //注意:通过代码创建的scrollView,一开始subviews这个数组为nil,并没有滚动条。其实只有在view显示出来的时候才会有滚动条
    NSLog(@"%@",scrollView.subviews);
    //设置contentSize
    scrollView.contentSize = image.size;
    //设置代理属性为当前控制器对象
    scrollView.delegate = self;
    
}
//可以实现UIScrollViewDelegate当中的方法,当scrollView发生滚动的时候,会自动调用这些方法
@end

UIScrollView代理的注意点
(1)任何类型的对象都可以成为UIScrollView的代理
该对象必须遵守UIScrollViewDelegate协议
UIScrollView.delegate是弱指针引用,因此delegate属性的赋值对象一创建出来就会被销毁,这并不符合我们的使用原则,因此,我们需要给delegate赋值一个强指针引用。
why?因为一般情况下我们会把控制器作为UIScrollView的代理,如果代理属性不用weak,就会形成循环引用,导致内存泄露
UIScrollView中的循环引用关系

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!