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;
    
    }

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

  • 相关阅读:
    CF1137FMatches Are Not a Child‘s Play【LCT】
    P4491[HAOI2018]染色【多项式,二项式反演】
    P3170[CQOI2015]标识设计【插头dp】
    log4j 使用教程说明
    log4j中Logger.getLogger()加载一个类提示错误
    编程基础 0x00008 的0x代表什么?
    编程基础 快速的进行 2进制,10进制,16进制 的 相互转换
    Java 基础 equals,hashcode和==的区别
    位运算 左移右移运算符 >>, <<, >>>
    Java 虚拟机 2.2 运行时数据区 Runtime Data Area
  • 原文地址:https://www.cnblogs.com/gaoxiaoniu/p/5303405.html
Copyright © 2011-2022 走看看