UIScrollView常见属性

设置内容尺寸(滚动范围)

@property(nonatomic) CGSize contentSize;

//可滚动尺寸:contentSize的尺寸减去scrollView的尺寸

//注意点:contentSize的尺寸小于或者等于scrollView的尺寸也是不可以滚定的

设置是否能够滚动

@property(nonatomic,getter=isScrollEnabled)BOOL scrollEnabled;

//仅仅是不能滚动

@property(nonatomic,getter=isUserInteractionEnabled)BOOLuserInteractionEnabled;

//UIView的属性,所有的操作都不能响应

设置边界是否有弹簧效果

@property(nonatomic) BOOL bounces;

下拉刷新

@property(nonatomic) BOOL alwaysBounceVertical;

@property(nonatomic) BOOL alwaysBounceHorizontal;

是否显示滚动条

@property(nonatomic) BOOL showsHorizontalScrollIndicator;

@property(nonatomic) BOOL showsVerticalScrollIndicator;

//注意点:千万不要通过索引去subviews数组访问scrollView子控件,因为包含滚动条在内总共三个子控件

// [self.scrollView.subviews.firstObject removeFromSuperview];

内容偏移量

@property(nonatomic) CGPoint contentOffset;

作用:

1.控制内容的偏移量

2.获取内容的偏移量

内边距

@property(nonatomic) UIEdgeInsets contentInset;


示例:展示大图片

ViewController.m文件

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.UIImageView
    UIImage *image = [UIImage imageNamed:@"minion"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.scrollView addSubview:imageView];

    // 2.设置contentSize
    self.scrollView.contentSize = image.size;

    // 3.内容的偏移量
    // 作用1:控制内容滚动的位置
    // 作用2:得知内容滚动的位置
    self.scrollView.contentOffset = CGPointMake(0, -100);

    // 4.内边距
    self.scrollView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);
}

/**
 *  点击控制器的view会自动调用这个方法
 */
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    self.scrollView.contentOffset = CGPointMake(-100, -100);
}


#pragma mark - 按钮的点击

- (IBAction)top {
    /*
    [UIView animateWithDuration:2.0 animations:^{
//        CGPoint offset = self.scrollView.contentOffset;
//        offset.y = 0;
//        self.scrollView.contentOffset = offset;
        self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
    }];
     */

    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x, 0) animated:YES];

}

- (IBAction)bottom {
    CGFloat offsetX = self.scrollView.contentOffset.x;
    CGFloat offsetY = self.scrollView.contentSize.height - self.scrollView.frame.size.height;
    CGPoint offset = CGPointMake(offsetX, offsetY);
    [self.scrollView setContentOffset:offset animated:YES];
}

- (IBAction)left {
    [self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y) animated:YES];
}

- (IBAction)right {

    CGFloat offsetY = self.scrollView.contentOffset.y;
    CGFloat offsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width;
    CGPoint offset = CGPointMake(offsetX, offsetY);
    [self.scrollView setContentOffset:offset animated:YES];
}

- (IBAction)rightTop {

}

- (IBAction)leftBottom {

}
@end

UIScrollView的代理

ViewController.m 文件

#import "ViewController.h"

@interface ViewController () <UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1.UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor redColor];
    scrollView.frame = CGRectMake(0, 20, 300, 200);
    [self.view addSubview:scrollView];

    // 注意点:通过代码创建scrollView,一开始subviews这个数组为nil
//    NSLog(@"%@",scrollView.subviews);

    // 1.创建UIImageView
    UIImage *image = [UIImage imageNamed:@"minion"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [scrollView addSubview:imageView];

    // 2.设置contenSize
    scrollView.contentSize = image.size;

    // 3.设置代理
    scrollView.delegate = self;
}

#pragma mark - UIScrollViewDelegate 代理方法
/**
 *  当scrollView正在滚动的时候就会自动调用这个方法
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"scrollViewDidScroll--");
}

/**
 *  用户即将开始拖拽scrollView时就会调用这个方法
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewWillBeginDragging-");
}

/**
 *  用户即将停止拖拽scrollView时就会调用这个方法
 */
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
//    NSLog(@"scrollViewWillEndDragging");
}

/**
 *  用户已经停止拖拽scrollView时就会调用这个方法
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (decelerate == NO) {
        NSLog(@"用户已经停止拖拽scrollView,停止滚动");
    } else {
        NSLog(@"用户已经停止拖拽scrollView,但是scrollView由于惯性会继续滚动,减速");
    }
}

/**
 * scrollView减速完毕会调用,停止滚动
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   NSLog(@"scrollView减速完毕会调用,停止滚动");
}
@end

results matching ""

    No results matching ""