通过scrollerview自定义collectionview(常用于推荐关注界面)

最近项目上要实现一个效果,先把效果图摆上来吧:

刚看到效果图的时候  我觉得很简单  用UICollectionview就可以了 但是后来发现collectionview只有两种布局方式  比较单一 :

箭头代表滑动方向

            

或者           

 

需求效果图:

方法:要实现刚才那个效果方法可能有很多解决方案  比如说重写UICollectionViewFlowLayout等方法 但这次用的方法相比于自定义UICollectionViewFlowLayout我觉得要简单一点那就是在添加一个scrollerview  在scrollerview添加view  在view上添加button:

 

附代码:

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIPageControl *myPageControl;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self addScrollerviewOnView];
}
//添加scrollerview和UIPageControl
- (void)addScrollerviewOnView{
    UIScrollView *scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    scrollview.delegate = self;
    
    scrollview.pagingEnabled = YES;
    
    scrollview.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width*3, [UIScreen mainScreen].bounds.size.height);
    scrollview.backgroundColor = [UIColor redColor];
    for (int i = 0; i<3; i++) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width*i, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        view.tag = i;
        [self addBtnWithView:view];
        view.backgroundColor = [UIColor colorWithWhite:1-i*0.3 alpha:1];
        [scrollview addSubview:view];
    }
    
    [self.view addSubview:scrollview];
    
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    
    self.myPageControl = pageControl;
    
    pageControl.center = CGPointMake(200,700);
    
    pageControl.bounds = CGRectMake(0, 0, 150, 50);
    
    pageControl.numberOfPages = 3;
    
    pageControl.pageIndicatorTintColor = [UIColor greenColor];
    
    pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    
    [self.view addSubview:pageControl];


}
//在scrollerview上的viwe上面添加按钮
- (void)addBtnWithView:(UIView *)view{
        for (int i = 0; i<(view.tag<2?6:3); i++) {
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake((10+(i%2)*(([UIScreen mainScreen].bounds.size.width-30)/2+10)), (10+(i/2)*(([UIScreen mainScreen].bounds.size.height-40)/3+10)), ([UIScreen mainScreen].bounds.size.width-30)/2,  ([UIScreen mainScreen].bounds.size.height-40)/3)];
            btn.backgroundColor = [UIColor yellowColor];
            [view addSubview:btn];
        }
}

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

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pageWidth = sender.frame.size.width;
    int page = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.myPageControl.currentPage = page;

}

肯定会有更简单更好的实现方法  希望能够不吝赐教,联系我

 

posted @ 2016-03-21 19:58  高晓牛  阅读(256)  评论(0编辑  收藏  举报