UIScrollView (API+循环滚动)

本文详述了iOS开发中UIScrollView的使用,包括如何建立、设置内容大小、回弹机制、状态条显示以及特殊场景的应用。讲解了如何解决ScrollView无法正常滚动的问题,探讨了ScrollView的contentSize、bounces属性,并介绍了状态条显示的控制方法和滚动状态跟踪。同时,文中提到了UITableView和UICollectionView作为特殊类型的ScrollView。
摘要由CSDN通过智能技术生成


 ScrollView莫名其妙不能在viewController划到顶怎么办

self.automaticallyAdjustsScrollViewInsets = NO;  


-(id)initWithFrameRect:(CGRect)rect ImageArray:(NSArray *)imgArr TitleArray:(NSArray *)titArr delegate:(id<JNScrollerViewDelegate>)delegate;
{
    
    if ((self=[super initWithFrame:rect]))
    {
        [self buildUI:rect ImageArray:imgArr TitleArray:titArr delegate:delegate];
    }
    
    return self;

}

-(void)buildUI:(CGRect)rect ImageArray:(NSArray *)imgArr TitleArray:(NSArray *)titArr delegate:(id<JNScrollerViewDelegate>)delegate;
{
    self.delegate = delegate;
    
    self.userInteractionEnabled=YES;
    titleArray=titArr;
    NSMutableArray *tempArray=[NSMutableArray arrayWithArray:imgArr];
    [tempArray insertObject:[imgArr objectAtIndex:([imgArr count]-1)] atIndex:0];
    [tempArray addObject:[imgArr objectAtIndex:0]];
    imageArray=[NSArray arrayWithArray:imgArr];
    viewSize=rect;
    NSUInteger pageCount=[imageArray count];
    
    //初始化
    scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, viewSize.size.width, viewSize.size.height)];
    scrollView.delegate = self;
    [self addSubview:scrollView];

    //是否分页
    scrollView.pagingEnabled = YES;
    
    //设置滑动最大范围
    scrollView.contentSize = CGSizeMake(viewSize.size.width * pageCount, viewSize.size.height);
    
    //设置水平拖动条是否显示
    scrollView.showsHorizontalScrollIndicator = NO;
    
    //设置垂直拖动条是否显示
    scrollView.showsVerticalScrollIndicator = NO;
    
    //类似弹簧效果,超出显示内容
    scrollView.scrollsToTop = YES;
    
    
    /**
     *  ContentOffset  图片的相对坐标(偏移量),案例是循环滚动,所以第一张作为循环的缓冲区图片,第二张为初始显示的首张图片
     *
     *  @param viewSize.size.width 第二张的x坐标
     *  @param 0                   第二张的y坐标
     *  输入结果(iPhone 6 模拟器) :  scrollView.contentOffset =(375,0)
     */
    //设置初始显示位置,第一张图片x,y坐标
    [scrollView setContentOffset:CGPointMake(viewSize.size.width, 0)];
    
    
    /**
     *  contentInset 类似于下拉刷新露出的 "等待区域"
     *  UIEdgeInsetsMake(上, 左, 下, 右);  设置对应区域偏移量
     *
     */
    scrollView.contentInset = UIEdgeInsetsMake(0, 20, 0, 0); //超出显示内容回弹时候,显示出偏移20点的左边区域
    scrollView.backgroundColor = [UIColor redColor];
    
    //填充图片,支持 urlString 和 imageName
    for (int i=0; i<pageCount; i++)
    {
        NSString *imgURL=[imageArray objectAtIndex:i];
        UIImageView *imgView=[[UIImageView alloc] init];
        if ([imgURL hasPrefix:@"http://"])
        {
         
            NSURL *newImgUrl = [NSURL URLWithString:imgURL];
            //网络图片,异步加载,可以用三方带缓存图片库
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                
                UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:newImgUrl]];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [imgView setImage:img];
                });
            });
        }
        else if (imgURL)
        {
            
            UIImage *img=[UIImage imageNamed:[imageArray objectAtIndex:i]];
            [imgView setImage:img];
        }
        
        [imgView setFrame:CGRectMake(viewSize.size.width*i, 0,viewSize.size.width, viewSize.size.height)];
        imgView.tag=i;
        
        //添加单击手势
        UITapGestureRecognizer *Tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagePressed:)];
        [Tap setNumberOfTapsRequired:1];
        [Tap setNumberOfTouchesRequired:1];
        imgView.userInteractionEnabled=YES;
        [imgView addGestureRecognizer:Tap];
        [scrollView addSubview:imgView];
    }
    
    
    
    
    //================================================================
    
    //说明文字层
    UIView *noteView=[[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-33,self.bounds.size.width,33)];
    float pageControlWidth=(pageCount-2)*10.0f+40.f;
    float pagecontrolHeight=20.0f;
    
    //当前页数控制器
    pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake((self.frame.size.width-pageControlWidth),6, pageControlWidth, pagecontrolHeight)];
    [noteView addSubview:pageControl];

    //当前下标
    pageControl.currentPage=0;
    
    //总页数
    pageControl.numberOfPages=(pageCount-2);
    
    //当前页数圆点的颜色
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    
    //背景颜色
    pageControl.pageIndicatorTintColor = [UIColor blueColor];
    
    
    noteTitle=[[UILabel alloc] initWithFrame:CGRectMake(5, 6, self.frame.size.width-pageControlWidth-15, 20)];
    [noteTitle setText:[titleArray objectAtIndex:0]];
    [noteTitle setBackgroundColor:[UIColor clearColor]];
    [noteTitle setFont:[UIFont systemFontOfSize:13]];
    
    [self addSubview:noteView];
    
    UIColor *noteViewColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.5];
    
    //文字+页数显示控件
    if (titleArray.count==0)
    {
//        noteViewColor = [UIColor clearColor];
        pageControl.frame = CGRectMake((self.frame.size.width-CGRectGetWidth(pageControl.frame))*.5f, 6, pageControlWidth, pagecontrolHeight);
    }
    //无文字层
    else
    {
        [noteView addSubview:noteTitle];
    }
    [noteView setBackgroundColor:noteViewColor];
    
    
    
    
}

-(void)setAnimaTion:(BOOL)animaTion_tager time:(int)time
{
    animaTion = animaTion_tager;
    if (animaTion) {

        timer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(changeimage) userInfo:nil repeats:YES];
        [timer fire];
    }else{
        [timer invalidate];
    }
}

-(void)changeimage
{

    [UIView animateWithDuration:0.5f
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         scrollView.contentOffset=CGPointMake(scrollView.contentOffset.x+ scrollView.frame.size.width, scrollView.contentOffset.y);
                     } completion:^(BOOL finished) {
                         [self scrollViewDidScroll:scrollView];
                         [self scrollViewDidEndDecelerating:scrollView];
                     }];

}

//当手动拨到第四个时 会同时触发DidEnd,
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

    currentPageIndex=page;

    pageControl.currentPage = page-1;
    NSLog(@"didScroll%d",currentPageIndex);

    //控件pageControl 显示第几个
//    pageControl.currentPage=(page-1);
//    NSInteger titleIndex=page-1;
//    if (titleIndex==[titleArray count]) {
//        titleIndex=0;
//    }
//    if (titleIndex<0) {
//        titleIndex=[titleArray count]-1;
//    }
//    [noteTitle setText:[titleArray objectAtIndex:titleIndex]];
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
    if (currentPageIndex==0) {
        //往左 变 3
        [_scrollView setContentOffset:CGPointMake(([imageArray count]-2)*viewSize.size.width, 0)];
    }
    if (currentPageIndex==([imageArray count]-1)) {
        //往右 变 1
        [_scrollView setContentOffset:CGPointMake(viewSize.size.width, 0)];
        
    }
    
}



以下转自: http://blog.csdn.net/houseq/article/details/11575817?utm_source=tuicool&utm_medium=referral

--前言:UIScrollView使用非常广,本文研究UIScrollView各属性和方法,明白它们的意义、作用。在后面的一篇文章有整理UIScrollView一些常见用法以及一些效果的实现思路。
--参考文章:http://www.cocoachina.com/iphonedev/sdk/2010/1224/2503.html  &&  http://zjqzy03080312.blog.163.com/blog/static/18574280720121121105928687  &&  http://blog.csdn.net/wzzvictory/article/details/9264335  
--官方查阅文档  https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html 
--介绍:UIScrollView用于在一个小范围里显示很大的内容的控件。通过用户平滑、手捏手势,在这个小区域里查看不同内容。是UITableView和UITextView的父类。它是视图,但是比较特殊,可以看成把它看成2层的结构。上面是它的frame层,跟一般试图一样,是它的可见区域,下面层是contentView,可以滑动。 
============常用属性============
--CGPoint contentOffSet:contentView的偏移值(正常contentOffSet.x/y值都为正数,但有时(bounces = YES)拽出边界了或者contentInset设置了,还是会出现负数,伴随着contentView滑动,总在改变);
--CGSize contentSize:contentView的大小;
--UIEdgeInsets contentInset:con
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值