二 2. UIScrollView的常见属性

UIScrollView的常见属性

  1. @property(nonatomic) CGPoint contentOffset;

    这个属性用来表示UIScrollView滚动的位置【偏移量】

    (其实就是内容左上角与scrollView左上角的间距值)[内容的左上角在scrollView左上角之上,就是正值]

  2. @property(nonatomic) CGSize contentSize;

    这个属性用来表示UIScrollView内容的尺寸,滚动范围(能滚多远)

  3. @property(nonatomic) UIEdgeInsets contentInset;

    这个属性能够在UIScrollView的4周增加额外的滚动区域,

    一般用来避免scrollView的内容被其他控件挡住

1. contentOffset的使用

  1. 代码示例:
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
    @end

    @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //这两种方式一样, 创建UIImageView的时候,这两种方式的效果是一样的 // UIImageView *imageView = [[UIImageView alloc] init]; // imageView.image = [UIImage imageNamed:@"minion"]; // imageView.frame = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height);
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minion"]]; [self.scrollView addSubview:imageView]; // 设置内容大小 self.scrollView.contentSize = imageView.image.size; } - (IBAction)left:(id)sender { // self.scrollView.contentOffset : 偏移量 // 记录UIScrollView滚动的位置,滚到哪 // 总结:内容的左上角 和 scrollView自身左上角 的 X\Y差值 不知道正负的时候打印一下就好了
    // 1.block回调动画的实现 block实现动画的方法有许多 动画完整之后执行的动作
    // [UIView animateWithDuration:2.0 animations:^{ // self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentOffset.y); // }]; [UIView animateWithDuration:2.0 animations:^{ self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentOffset.y);//y值是用原来的 } completion:^(BOOL finished) { NSLog(@"执行完毕"); }]; } - (IBAction)top:(id)sender { // self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0); CGPoint offset = CGPointMake(self.scrollView.contentOffset.x, 0);
    // 2.
    针对某一个属性特有的动画 
      [self.scrollView setContentOffset:offset animated:YES];

    }- (IBAction)bottom:(id)sender {
        //3. 设置动画的时候针对的是UIView的属性,不只是针对某一个属性的,所以要把UIView放进去
        [UIView animateWithDuration:2.0 animations:^{
            CGFloat offsetY = self.scrollView.contentSize.height - self.scrollView.frame.size.height;
    //4.重点:OC中不允许直接修改oc对象的结构体的属性成员,把原来的拿过来修改之后再赋给他既可以了 CGPoint offset
    = self.scrollView.contentOffset; offset.y = offsetY; self.scrollView.contentOffset = offset; }]; // self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, offsetY); // self.scrollView.contentOffset.y = offsetY; // OC语法细节:不允许直接修改OC对象的结构体属性的成员 } - (IBAction)right:(id)sender {

    // 5. 首尾式动画 重点是也可以设置代理 [UIView beginAnimations:nil context: nil]; [UIView setAnimationDuration:
    2.0]; [UIView setAnimationDelegate:self]; // 代理 [UIView setAnimationDidStopSelector:@selector(stop)]; [UIView setAnimationWillStartSelector:@selector(start)]; CGFloat offsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width; self.scrollView.contentOffset = CGPointMake(offsetX, self.scrollView.contentOffset.y); [UIView commitAnimations]; } - (void)start { NSLog(@"start"); } - (void)stop { NSLog(@"stop"); } @end

    查看一下官方文档,UIView其他的动画方法。

  1. 利用UIScrollView显示下面的大图片重点:

2. contentInset的使用

  1. 他有四个成员,上左下右,代码如下:
    // 设置contentInset 后期很有用
        self.scrollView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);

3. UIScrollView的其他属性

  1. @property(nonatomic) BOOL bounces;

    设置UIScrollView是否需要弹簧效果

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

    设置UIScrollView是否能滚动

  3. @property(nonatomic) BOOL showsHorizontalScrollIndicator;

    是否显示水平滚动条

  4. @property(nonatomic) BOOL showsVerticalScrollIndicator;

    是否显示垂直滚动条

  5. 滚动效果代码示例:
    #import "ViewController.h"
    
    @interface ViewController ()
    {
        CGRect _lastRect;
    }
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
    /** <#注释#> */
    @property(nonatomic,strong)UIView* gird;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    //    self.scrollView.showsHorizontalScrollIndicator = NO;
    //    self.scrollView.showsVerticalScrollIndicator = NO;
    
        NSMutableArray *girdArr = [NSMutableArray array];
        for (int i = 0; i<50; i++) {
            int row = i / 3; // 行号
            int col = i % 3; // 列号
            CGFloat x = col * (50 + 30); // 列号决定了X
            CGFloat y = row * (50 + 30); // 行号决定了Y
          self.gird =  [self addGridWithX:x y:y];
            if (i == 49) {
                 //保存最后一个的frame
                _lastRect = self.gird.frame;
            }
            [girdArr addObject:self.gird];
        }
    //    
    //    self.scrollView.subviews[49];
       UIView* lastView = [girdArr lastObject];
        
    // 1.获取最后一个控件的frame的时候,最后不好用subViews最后一个,因为系统内部会添加一些其他的控件,得到的不是想要的,还有原因如下2观点
    // UIView *lastView = [self.scrollView.subviews lastObject]; // CGFloat contentH = lastView.frame.origin.y + lastView.frame.size.height; CGFloat contentH = CGRectGetMaxY(lastView.frame); // 2. 没有设置contentsize之前和之后得到的最后一个控件是不一样的;系统会把滚动条添加上,滚动条设置为NO就没有了,但是必须发在打印之前设置; NSLog(@"%@", [self.scrollView.subviews lastObject]); self.scrollView.contentSize = CGSizeMake(0, contentH); NSLog(@"%@", [self.scrollView.subviews lastObject]);
    //3,打印subView就知道有哪些控件了【调试方法学习方法 }
    - (UIView*)addGridWithX:(CGFloat)x y:(CGFloat)y { UIView *grid = [[UIView alloc] init]; grid.frame = CGRectMake(x, y, 50, 50); grid.backgroundColor = [UIColor blueColor]; [self.scrollView addSubview:grid]; return grid; } //NSLog(@"%@", self.scrollView.subviews); // 滚动控件 // UIScrollView *scrollView = [[UIScrollView alloc] init]; // scrollView.frame = CGRectMake(30, 50, 250, 250); // scrollView.backgroundColor = [UIColor redColor]; // [self.view addSubview:scrollView]; // NSLog(@"%@", scrollView.subviews); @end

补充

1. UIImageView的使用

  1. 代码示例
       //这两种方式一样, 创建UIImageView的时候,这两种方式的效果是一样的,image的大小就是UIImageView的大小
    //    UIImageView *imageView = [[UIImageView alloc] init];
    //    imageView.image = [UIImage imageNamed:@"minion"];
    //    imageView.frame = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height);
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minion"]];
        [self.scrollView addSubview:imageView];
        
        // 设置内容大小
        self.scrollView.contentSize = imageView.image.size;
posted @ 2016-09-15 10:29  <瑾瑜>  阅读(124)  评论(0)    收藏  举报