關於ScrollView的知識

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

包含Container View。      Container View的Width約束


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

    tableView添加head



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

代碼如下:

#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)開始顯示 





    


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章