IT虾米网

UIScrollView 滚动视图详解

itxm 2018年06月24日 手机开发 565 0
/////记得添加 代理 <UIScrollViewDelegate> 
  
UIScrollView* sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; 
    [self.view addSubview:sv]; 
    [sv release]; 
    //常用属性 
    //真正大小,在这里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]; 
          
        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); 
}

发布评论
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

UIPickerView 滚动选择器详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。