iOS UICollectionView 从右向左对齐的实现

简介: iOS UICollectionView 从右向左对齐的实现

前言

iOS UICollectionView 从右向左对齐(Aligning right to left on UICollectionView)

实现原理:采用setTransform对其进行水平翻转

效果图

image.png

I 、UICollectionView 从右向左对齐

本文按钮的视图结构

  1. bottomV 内部采用UICollectionView进行布局。
  1. UICollectionViewCell内部包含子视图自定义按钮ERPbtn4Radius

1.1 核心步骤

首先,在创建UICollectionView时,对其进行了水平翻转:

   [_collectionView setTransform:CGAffineTransformMakeScale(-1,1)];

在更新UICollectionViewCell的数据模型时,对它的contentView上进行相同的水平翻转

- (void)setModel:(QCTCollectionModel *)model{
    _model = model;
    [self.contentView setTransform:CGAffineTransformMakeScale(-1,1)];
}

1.2 bottomV的用法

- (ERPBottom_operation_view4btn *)bottomV{
    if (nil == _bottomV) {
        ERPBottom_operation_view4btn *tmpView = [[ERPBottom_operation_view4btn alloc]init];
        _bottomV = tmpView;
        tmpView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_bottomV];
        __weak __typeof__(self) weakSelf = self;
        [tmpView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(weakSelf);
            make.top.equalTo(weakSelf.tableView.mas_bottom);
            make.left.right.equalTo(weakSelf);
            make.height.mas_equalTo(kAdjustRatio(50));
        }];
    }
    return _bottomV;
}

1.3  bottomV的实现

  • h
#import "ERPBtnCollectionViewCell.h"
#import "QCTShadowView.h"
NS_ASSUME_NONNULL_BEGIN
@interface ERPBottom_operation_view4btn : QCTShadowView
@property (nonatomic, strong) NSMutableArray* models;
@property (nonatomic, copy) void (^block)(id sender);
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSIndexPath *indexPath;
@end
  • m
#import "ERPBottom_operation_view4btn.h"
@implementation ERPBottom_operation_view4btn
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self collectionView];
        [self setupshadowColor];//设置顶部阴影
    }
    return self;
}
- (void)setupshadowColor{
    UIView * tmpView = self;
    tmpView.layer.shadowColor = [UIColor blackColor].CGColor;//设置阴影的颜色
    tmpView.layer.shadowOpacity = 0.08;//设置阴影的透明度
    tmpView.layer.shadowOffset = CGSizeMake(kAdjustRatio(0), kAdjustRatio(-5));//设置阴影的偏移量,阴影的大小,x往右和y往下是正
    tmpView.layer.shadowRadius = kAdjustRatio(5);//设置阴影的圆角,//阴影的扩散范围,相当于blur radius,也是shadow的渐变距离,从外围开始,往里渐变shadowRadius距离
}
/**
 NSMutableArray
 */
- (void)setModels:(NSMutableArray*)models{
    _models = models;
    [self.collectionView reloadData];
}
- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        // 2.设置整个collectionView的内边距
        //分别为上、左、下、右
                flowLayout.sectionInset = UIEdgeInsetsMake(0,kAdjustRatio(20),0,kAdjustRatio(20));
        //.设置每一行之间的间距
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = kAdjustRatio(10);
        //        flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH-3*kAdjustRatio(10))/3.0, self.optionsView.height);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.bounces = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[ERPBtnCollectionViewCell class] forCellWithReuseIdentifier:@"ERPBtnCollectionViewCell"];
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior =  UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
        _collectionView.scrollEnabled = NO;
        //        UICollectionViewScrollDirectionHorizontal
        // 在UICollectionView上从右向左对齐(Aligning right to left on UICollectionView)
        //1、首先,在创建UICollectionView时,我对其进行了水平翻转:
//2、然后子类 UICollectionViewCell 在这里执行在其contentView上进行相同的水平翻转:
//[self.contentView setTransform:CGAffineTransformMakeScale(-1,1)];
         [_collectionView setTransform:CGAffineTransformMakeScale(-1,1)];
        __weak __typeof__(self) weakSelf = self;
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.right.equalTo(weakSelf);
        }];
    }
    return _collectionView;
}
#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.models.count;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size;
//    size = CGSizeMake(self.collectionView.width/ self.models.count, kAdjustRatio(k_cell_H));
    size = CGSizeMake(self.collectionView.width/ 5, kAdjustRatio(k_cell_H));
    return size;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ERPBtnCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ERPBtnCollectionViewCell" forIndexPath:indexPath];
    QCTCollectionModel *model = self.models[indexPath.row];
    cell.model =model;
    return cell;
}
@end

II、UICollectionViewCell的完整代码

UICollectionViewCell内部包含子视图自定义按钮ERPbtn4Radius

2.1 自定义UICollectionViewCell

  • h
@interface ERPBtnCollectionViewCell : UICollectionViewCell
@property (nonatomic,strong) QCTCollectionModel *model;
@property (weak, nonatomic) ERPbtn4Radius *btn;
@end
  • m
#import "ERPBtnCollectionViewCell.h"
@implementation ERPBtnCollectionViewCell
- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
}
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self selfInit];
    }
    return self;
}
/**
 */
- (void)selfInit{
    self.layer.borderWidth = 0;
    self.layer.borderColor = rgb(255,255,255).CGColor;
    [self btn];
}
- (ERPbtn4Radius *)btn{
    if (!_btn) {
        ERPbtn4Radius *b = [[ERPbtn4Radius alloc]init];
        _btn = b;
        __weak __typeof__(self) weakSelf = self;
        [self.contentView addSubview:b];
        [b mas_makeConstraints:^(MASConstraintMaker *make) {
            //            make.height.mas_equalTo(kAdjustRatio(25));
//            make.width.mas_equalTo(kAdjustRatio(70));
            make.center.equalTo(weakSelf);
        }];
        [[b rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                    if (weakSelf.model.block) {
                        weakSelf.model.block(weakSelf.model);
                    }
        }];
    }
    return _btn;
}
- (void)setModel:(QCTCollectionModel *)model{
    _model = model;
    [self.contentView setTransform:CGAffineTransformMakeScale(-1,1)];
    [self.btn setTitle:model.titleName forState:UIControlStateNormal];
    if(model.backgroundColor){
        self.backgroundColor = model.backgroundColor;
    }
    if(model.textColor){
        [self.btn setTitleColor:model.textColor forState:UIControlStateNormal];
    }
}

2.2 自定义按钮

@implementation ERPbtn4Radius
- (void)layoutSubviews{
    [super layoutSubviews];
    [self layoutIfNeeded];
    self.layer.cornerRadius = self.height*0.5;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(kAdjustRatio(-7));
        make.top.offset(kAdjustRatio(7));
        if(self.titleLabel.text.length<=2){
            make.left.offset(kAdjustRatio(24));
            make.right.offset(kAdjustRatio(-24));
        }else{
            make.left.offset(kAdjustRatio(12));
            make.right.offset(kAdjustRatio(-12));
        }
    }];
}
- (void)setHighlighted:(BOOL)highlighted{
    [super setHighlighted:highlighted];
    UIButton *_receiptBtn = self;
    if (highlighted) {
        _receiptBtn.layer.borderColor = ktabSelectedTextColor.CGColor;
        _receiptBtn.layer.borderWidth = 1;
    }else{
        _receiptBtn.layer.borderColor = rgb(231,231,231).CGColor;
        _receiptBtn.layer.borderWidth = 1;
    }
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        UIButton *_cancelBtn = self;
        _cancelBtn.layer.borderColor = rgb(231,231,231).CGColor;
        _cancelBtn.layer.borderWidth = 1;
        _cancelBtn.clipsToBounds = YES;
        _cancelBtn.titleLabel.font = kPingFangFont(12);
        [_cancelBtn setTitleColor:rgb(51,51,51) forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:ktabSelectedTextColor forState:UIControlStateHighlighted];
    }
    return self;
}

see also

目录
相关文章
|
安全 数据安全/隐私保护 iOS开发
iOS小技能:【发红包】使用tweak和lua脚本结合进行实现
我们开发的大部分越狱程序,都是编译成动态链接库(`例如:介绍的越狱程序(Tweak)开发,就是动态链接库。`),然后通过越狱平台的MobileSubstrate(iOS7上叫CydiaSubstrate)来加载进入目标程序(Target),通过对目标程序的挂钩(Hook),来实现相应的功能。
367 0
|
Android开发 iOS开发
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
447 0
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
|
存储 安全 iOS开发
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
524 0
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
|
Swift 数据安全/隐私保护 iOS开发
iOS开发 - swift通过Alamofire实现https通信
iOS开发 - swift通过Alamofire实现https通信
466 0
iOS开发 - swift通过Alamofire实现https通信
|
开发者 iOS开发
iOS开发 - 用AFNetworking实现https单向验证,双向验证
iOS开发 - 用AFNetworking实现https单向验证,双向验证
466 0
iOS开发 - 用AFNetworking实现https单向验证,双向验证
|
iOS开发
iOS小技能:自动布局实现兄弟控件N等分且宽高比例是1:N(xib 上实现)
本文为 iOS视图约束专题的第三篇:xib上使用自动布局教程
217 0
|
Linux iOS开发 开发者
实现在windows、linux下上传ios app到App Store
实现在windows、linux下上传ios app到App Store
实现在windows、linux下上传ios app到App Store
|
小程序 前端开发 Shell
接入 mPaaS 小程序并实现启动 iOS 版| 学习笔记
快速学习接入 mPaaS 小程序并实现启动 iOS 版。
接入 mPaaS 小程序并实现启动 iOS 版| 学习笔记
|
小程序 Shell 开发工具
接入 mpaas 小程序并实现启动 IOS 版|学习笔记
快速学习接入 mpaas 小程序并实现启动 IOS 版
260 0
接入 mpaas 小程序并实现启动 IOS 版|学习笔记
|
程序员 API Android开发
iOS开发:简单的Toast提示框实现
博主是以iOS开发出身,那就最后一篇博文就分享一下关于iOS的内容吧。iOS开发过程中,有些时候操作App的时候,需要给用户对应的响应提示操作,使用系统自带的提示框不是每种情况都适用的。
773 0
iOS开发:简单的Toast提示框实现

热门文章

最新文章

  • 1
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
  • 2
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 3
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 4
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 5
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 6
    iOS各个证书生成细节
  • 7
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
  • 8
    iOS开发-UIScrollView原理
  • 9
    iOS - Swift NSPoint 位置
  • 10
    iOS:应用程序的线程安全性