美文网首页
iOS-UIScrollView 滚动视图

iOS-UIScrollView 滚动视图

作者: 我是谁重要吗 | 来源:发表于2018-03-21 22:02 被阅读10次

    /////记得添加 代理 <UIScrollViewDelegate>

    UIScrollView* sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    [self.view addSubview:sv];
      
        //常用属性
        //真正大小,在这里frame仅代表可视位置和范围
        sv.contentSize = CGSizeMake(320 * 17, 460 * 2);
        //偏移量,frame左上角顶点相对于contentView左上角顶点的偏移量
        sv.contentOffset = CGPointMake(0, 0);
        //[sv setContentOffset: animated:]
        //分页
        sv.pagingEnabled = YES;
        //设置额外的空间
        sv.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        //锁定方向,左右滑动和上下滑动不能同时进行
        sv.directionalLockEnabled = YES;
        //反弹效果
        sv.bounces = YES;
        //水平方向上保持反弹效果
        sv.alwaysBounceHorizontal = YES;
        //垂直方向上保持反弹效果
        sv.alwaysBounceVertical = YES;
        //开启滑动
        sv.scrollEnabled = YES;
        //隐藏水平状态条
        sv.showsHorizontalScrollIndicator = YES;
        //隐藏垂直状态条
        sv.showsVerticalScrollIndicator = YES;
        //状态条位置
        //sv.scrollIndicatorInsets = UIEdgeInsetsMake(100, 100, 100, 100);
        //状态条类型
        sv.indicatorStyle = UIScrollViewIndicatorStyleDefault;
        //减速速率 范围0~1
        sv.decelerationRate = 0.5;
        //delaysContentTouches
        //sv.delaysContentTouches = NO;
        sv.delegate = self;
        sv.tag = 10;
        
        //添加内容  图片
        for (int i = 0; i < 17; i++) {
            UIScrollView* subSv = [[UIScrollView alloc] initWithFrame:CGRectMake(i * 320, 0, 320, 460)];
            subSv.delegate = self;
            subSv.minimumZoomScale = 0.5;
            subSv.maximumZoomScale = 2.0;
            [sv addSubview:subSv];
            [subSv release];
            //添加点击设置缩放 [sv setZoomScale:1.5 animated:YES];
            UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"10_%d.jpg",i]];
            [subSv addSubview:imageView];
            [imageView release];
        }
        //这个button 只显示在第一张图片上
        UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(100, 100, 100, 40);
        button.backgroundColor = [UIColor yellowColor];
        [sv addSubview:button];
        [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        
        
        ///事件
        - (void)buttonClick{
        NSLog(@"点击了");
    //    UIScrollView* sv = (UIScrollView*)[self.view viewWithTag:10];
    //    [sv setContentOffset:CGPointMake(320 * 5, 0) animated:YES];
        self.view.hidden=YES;
    }
    
    //缩放
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
        if (scrollView.tag == 10) {
            return nil;
        }
        for (UIView* view in scrollView.subviews) {
            if ([view isKindOfClass:[UIImageView class]]) {
                return view;
            }
        }
        return nil;
    }
    
    
    //滑动调用,contentOffSet发生偏移
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        //int page = scrollView.contentOffset.x / 320;
        //NSLog(@"%d",page);
    
    }
    
    //将要开始拖拽
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
        NSLog(@"将要开始拖拽");
    }
    
    //将要结束拖拽
    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
        NSLog(@"将要结束拖拽");
    }
    
    //结束拖拽
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
        NSLog(@"结束拖拽 %d",decelerate);
    }
    
    //将要开始减速
    - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
        NSLog(@"将要开始减速");
    }
    
    //结束减速
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        NSLog(@"结束减速");
    }
    
    - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
        NSLog(@"结束动画");
    }
    
    - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
        NSLog(@"要滑动到顶部");
        return YES;
    }
    
    - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
        NSLog(@"已经滑动到顶部");
    }
    
    
    
    //将要开始缩放
    - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
        NSLog(@"开始缩放");
    }
    
    //正在缩放
    - (void)scrollViewDidZoom:(UIScrollView *)scrollView{
        NSLog(@"缩放中");
    }
    
    //结束缩放
    - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{
        NSLog(@"缩放结束:%f",scrollView.zoomScale);
    }
    

    问题:

    1、使用UINavigationController后导致UIScrollView位置变化(下移)

    分析:不要让UIScrollView成为第一个视图。

    解决:

    <1>添加UIScrollView之前添加一个1像素的view

    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, Screen_Height, 1, 1)];
    
    [self.view addSubview:view];
    
    

    http://blog.csdn.net/b719426297/article/details/22290257

    相关文章

      网友评论

          本文标题:iOS-UIScrollView 滚动视图

          本文链接:https://www.haomeiwen.com/subject/rpwnqftx.html