zoukankan      html  css  js  c++  java
  • 通过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;
    
    }

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

  • 相关阅读:
    File System Minifilter Drivers(文件系统微型过滤驱动)入门
    使用Windows驱动的虚拟打印机,打印Excel表格无表格线问题解决(2)
    使用Windows驱动的虚拟打印机,打印Excel表格无表格线问题解决(1)
    lettcode 上的几道哈希表与链表组合的数据结构题
    计蒜之道 百度AI小课堂-上升子序列
    Educational Codeforces Round 40 F. Runner's Problem
    第13届 广东工业大学ACM程序设计大赛 C题 平分游戏
    Educational Codeforces Round 39 (Rated for Div. 2) G
    Codeforces Round #466 (Div. 2) E. Cashback
    cf 460 E. Congruence Equation 数学题
  • 原文地址:https://www.cnblogs.com/gaoxiaoniu/p/5303405.html
Copyright © 2011-2022 走看看