天天看點

關于ScrollView的知識

1. 在Interface Builder中ScrollView設定auto layout 時出現需要設定确定高寬(當scrollView有子View時)的問題--解決方案是:在scrollview中添加一個View作為container,不僅設定container view的上下左右與scrollview對齊外,還要設定container view的高寬等于scrollView的高寬,或者container view中的子view高寬确定。

關于ScrollView的知識

包含Container View。      

關于ScrollView的知識

Container View的Width限制

2. 在Interface Builder中建立的ScrollView中嵌套View,實作如“天貓”APP首頁效果。。。。(important)

    tableView添加head

關于ScrollView的知識

3. 使用ScrollView實作圖檔的滾動展示,參考:http://www.cnblogs.com/wendingding/p/3763527.html

關于ScrollView的知識

代碼如下:

#import "AZScrollPictureViewController.h"
#define kImageCounts 4  // 圖檔數量

@interface AZScrollPictureViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
//   頁碼
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@property (nonatomic, strong) NSTimer *timer;

@end


@implementation AZScrollPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //  圖檔寬度
    CGFloat imageWidth = self.scrollView.frame.size.width;
    //  圖檔高度
    CGFloat imageHeight = self.scrollView.frame.size.height;
    //  圖檔Y
    CGFloat imageY = 0;

    //  1.依次添加圖檔
    for (int i = 0 ; i < kImageCounts ; ++i) {
        UIImageView *imageView = [[UIImageView alloc]init];
        //  各圖檔的X
        CGFloat imageX = i * imageWidth;
        //  設定frame
        imageView.frame = CGRectMake(imageX, imageY, imageWidth, imageHeight);
        
        //  設定圖檔
        NSString *imageName = [NSString stringWithFormat:@"imageScroll_0%d", i + 1];
        imageView.image = [UIImage imageNamed:imageName];
        
        //  隐藏scrollView的滾動訓示條
        self.scrollView.showsHorizontalScrollIndicator = NO;
        
        [self.scrollView addSubview:imageView];
    }
    
    //  2.設定scrollView的滾動範圍,不允許在垂直方向上進行滾動
    CGFloat contentWidth = imageWidth * kImageCounts;
    self.scrollView.contentSize = CGSizeMake(contentWidth, 0);
    
    //  3.設定分業
    self.scrollView.pagingEnabled = YES;
    
    // 4.監聽srollView的滾動
    self.scrollView.delegate = self;
    
    [self addTimer];
    
    
}


- (void)nextImage
{
    int page = (int)self.pageControl.currentPage;
    if (page == kImageCounts) {
        page = 0;
    }
    else{
        ++page;
    }
    
    //  滾動scrollView
    CGFloat x = page * self.scrollView.frame.size.width;
    self.scrollView.contentOffset = CGPointMake(x, 0);
}


//  scrollView滾動的時候 自動 調用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //  計算頁碼
    //  頁碼 = (contentOffset.x + scrollView的一半寬度) / scrollView的寬度
    CGFloat contentOffsetX = scrollView.contentOffset.x;
    CGFloat scrollViewWidth = scrollView.frame.size.width;
    int page = (contentOffsetX + scrollViewWidth / 2) / scrollViewWidth;
    self.pageControl.currentPage = page;
}


//  拖拽scrollView時 自動 調用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //  關閉定時器
    [self removeTimer];
}


//  拖拽結束時 自動 調用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //  開啟定時器
    [self addTimer];
}


/**
 *  開啟定時器
 */
- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                  target:self
                                                selector:@selector(nextImage)
                                                userInfo:nil
                                                 repeats:YES];
}


/**
 *  關閉定時器
 */
- (void)removeTimer
{
    [self.timer invalidate];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
           

4. 涉及的小知識點

contentSize: The size of the content view. 其實就是scrollview可以滾動的區域,比如frame = (0 ,0 ,320 ,480) contentSize = (320 ,960),代表你的scrollview可以上下滾動,滾動區域為frame大小的兩倍。

contentOffset:The point at which the origin of the content view is offset from the origin of the scroll view. 是scrollview目前顯示區域頂點相對于frame頂點的偏移量,比如上個例子你拉到最下面,contentoffset就是(0 ,480),也就是y偏移了480 

contentInset:The distance that the content view is inset from the enclosing scroll view.是scrollview的contentview的頂點相對于scrollview的位置,例如你的contentInset = (0 ,100),那麼你的contentview就是從scrollview的(0 ,100)開始顯示 

繼續閱讀