UIScrollView的基本属性

2016-07-31  本文已影响0人  junden

注:只要有学到新的知识,会不断更新

CGPoint contentOffset;                  // 显示窗口在滚动内容画布里面的左上角的偏移量,(x, y)
CGSize  contentSize;                    // 滚动内容的画布大小
UIEdgeInsets contentInset;                   // 可以在外围加入的一个边框,有距离高,底部,左边,右边的距离

关于监控滚动的代理

self.scrollView.delegate = self;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
   //利用偏移量,设置控件的当前页
  int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
   self.pageControl.currentPage = page;

}

封装自动来回滚动UIScrollView记录

首先创建一个自定义xib,和两个继承UIview的类文件

接着重写传进来的数据set方法,并重写layoutSubviews

将定时器写在setUp内

    [NSTimer scheduledTimerWithTimeInterval:1.5
        这里要写方法所在的类            target:pageview
                                     selector:@selector(changePage)
                                     userInfo:nil
                                     repeats:YES];
    }
// MARK: - 换页的功能
- (void)changePage{
    CGFloat imgW = self.scrollView.frame.size.width;
    CGFloat dx = self.scrollView.contentOffset.x + imgW;
    //记录当前的方向, static用来存储,下次进来的值保持上次
    static BOOL direction ;
    //仅当最后一页或者第一页的时候判断方向
    if(self.pageControl.currentPage == self.imageNames.count -1|| self.pageControl.currentPage == 0)
    {direction = [self checkDirection];}
    //根据判断结果进行方向滚动
    if(direction){
      [UIView animateWithDuration:1.0 animations:^{self.scrollView.contentOffset = CGPointMake(dx, 0);}];
    }
    if(!direction ){
      [UIView animateWithDuration:1.0 animations:^{self.scrollView.contentOffset = CGPointMake(dx - 2*imgW, 0);}];
    }
}
// MARK: - 方向的判断
- (BOOL) checkDirection{
    BOOL gorigth = YES ;
    if (self.pageControl.currentPage == 0) {
        gorigth = YES;
    }
    if(self.pageControl.currentPage == self.imageNames.count -1){
        gorigth = NO;
    }
    return gorigth;
}

遇到的问题

上一篇 下一篇

猜你喜欢

热点阅读