UIScrollView

本文介绍了UIScrollView的基本使用,包括如何将内容添加到UIScrollView并设置contentSize。接着详细讲解了UIScrollView的常见属性,特别是其delegate机制,用于在滚动过程中监听和响应事件。还强调了设置代理时的注意事项,如避免循环引用。最后,讨论了UIScrollView的应用,如内容缩放和实现轮播图的分页功能。
摘要由CSDN通过智能技术生成

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 = 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值