欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實現(xiàn)簡單的二級菜單效果

 更新時間:2016年10月28日 08:55:03   作者:LYSNote  
這篇文章給大家主要介紹的是利用iOS如何實現(xiàn)簡單的菜單效果,文中給出了詳細的示例代碼,而且實現(xiàn)的比較簡單,適合新人學習使用。感興趣的朋友們可以參考借鑒,下面來一起看看吧。

首先來看看要實現(xiàn)的效果圖

示例代碼如下

Untitled.gif
#import "ViewController.h"
#import "CollectionViewCell.h"
#import "CollectionSectionView.h"
@interface ViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,copy)NSString *leftOrRight;
@property (nonatomic,strong)NSIndexPath *clickIndexPath;
@end
static NSString *cellIdentifier = @"CollectionViewCell";
static NSString *sectionIdentifier = @"CollectionSectionView";
@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
 flowLayout.itemSize = CGSizeMake(50, 25);
 flowLayout.minimumLineSpacing = 0;
 flowLayout.minimumInteritemSpacing = 0;
 flowLayout.headerReferenceSize = CGSizeMake(0, 40);
 flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
 self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
 _collectionView.backgroundColor = [UIColor whiteColor];
 _collectionView.delegate = self;
 _collectionView.dataSource = self;
 [_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
 [_collectionView registerClass:[CollectionSectionView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:sectionIdentifier];
 [self.view addSubview:_collectionView];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
 return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
 if (self.clickIndexPath.section == section) {
  if (self.clickIndexPath.section == 0 && [self.leftOrRight isEqualToString:@"left"]) {
   return 3;
  }
  if (self.clickIndexPath.section == 0 && [self.leftOrRight isEqualToString:@"right"]) {
   return 4;
  }
  if (self.clickIndexPath.section == 1 && [self.leftOrRight isEqualToString:@"left"]) {
   return 10;
  }
  if (self.clickIndexPath.section == 1 && [self.leftOrRight isEqualToString:@"right"]) {
   return 8;
  }
  if (self.clickIndexPath.section == 2 && [self.leftOrRight isEqualToString:@"left"]) {
   return 33;
  }
  if (self.clickIndexPath.section == 2 && [self.leftOrRight isEqualToString:@"right"]) {
   return 6;
  }
 }
 return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
 CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
 cell.label.text = [NSString stringWithFormat:@"%ld-%ld",indexPath.section,indexPath.row];
 return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
 if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
  CollectionSectionView *sectionView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:sectionIdentifier forIndexPath:indexPath];
  sectionView.backgroundColor = [UIColor lightGrayColor];
  sectionView.leftStr = @"家具";
  sectionView.rightStr = @"家電";
  [sectionView customBtnHandelAction:^(NSString *leftOrRight) {
   self.clickIndexPath = indexPath;
   self.leftOrRight = leftOrRight;
   [collectionView reloadData];
  }];
  return sectionView;
 }
 return nil;
}
- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}
#import <UIKit/UIKit.h>
typedef void(^BtnHandleAction)(NSString *leftOrRight);
@interface CollectionSectionView : UICollectionReusableView
@property (nonatomic,copy)NSString *leftStr;
@property (nonatomic,copy)NSString *rightStr;
- (void)customBtnHandelAction:(BtnHandleAction)action;
@end
#import "CollectionSectionView.h"
@interface CollectionSectionView ()
@property (nonatomic,strong)UIButton *leftBtn;
@property (nonatomic,strong)UIButton *rightBtn;
@property (nonatomic,copy)BtnHandleAction btnHandelAction;
@end
@implementation CollectionSectionView
- (instancetype)initWithFrame:(CGRect)frame
{
 self = [super initWithFrame:frame];
 if (self) {
  [self setup];
 }
 return self;
}
- (UIButton *)leftBtn{
 if (!_leftBtn) {
  self.leftBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
  _leftBtn.tag = 2000;
  _leftBtn.frame = CGRectMake(1, 1, self.frame.size.width / 2 - 2, self.frame.size.height - 2);
  _leftBtn.backgroundColor = [UIColor whiteColor];
  [_leftBtn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
 }
 return _leftBtn;
}
- (UIButton *)rightBtn{
 if (!_rightBtn) {
  self.rightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
  _rightBtn.tag = 2001;
  _rightBtn.frame = CGRectMake(self.frame.size.width / 2 + 1, 1, self.frame.size.width / 2 - 2, self.frame.size.height - 2);
  _rightBtn.backgroundColor = [UIColor whiteColor];
  [_rightBtn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
 }
 return _rightBtn;
}
- (void)btnAction:(UIButton *)sender{
 NSInteger tag = sender.tag;
 if (tag == 2000) {
  self.btnHandelAction(@"left");
 }
 if (tag == 2001) {
  self.btnHandelAction(@"right");
 }
}
- (void)customBtnHandelAction:(BtnHandleAction)action{
 self.btnHandelAction = action;
}
- (void)setup{
 [self addSubview:self.leftBtn];
 [self addSubview:self.rightBtn];
}
- (void)setLeftStr:(NSString *)leftStr{
 [self.leftBtn setTitle:leftStr forState:(UIControlStateNormal)];
 [self.leftBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
}
- (void)setRightStr:(NSString *)rightStr{
 [self.rightBtn setTitle:rightStr forState:(UIControlStateNormal)];
 [self.rightBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
}
@end

總結

以上就是這篇文章的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能有所幫助,如果有疑問大家可以留言交流。

相關文章

  • IOS如何使用CAShapeLayer實現(xiàn)復雜的View的遮罩效果

    IOS如何使用CAShapeLayer實現(xiàn)復雜的View的遮罩效果

    這篇文章主要為大家詳細介紹了IOS如何使用CAShapeLayer實現(xiàn)復雜的View的遮罩效果,感興趣的小伙伴們可以參考一下
    2016-03-03
  • 解析iOS10中的極光推送消息的適配

    解析iOS10中的極光推送消息的適配

    這篇文章主要介紹了解析iOS10中的極光推送消息的適配的相關資料,我們需要先安裝Xcode8.0版本,接下來本文分步驟詳細給大家介紹,需要的朋友可以參考下
    2016-09-09
  • iOS10適配問題收集整理

    iOS10適配問題收集整理

    本文是小編給大家收集整理些有關iOS10適配問題的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • iOS 中 使用UITextField格式化銀行卡號碼的解決方案

    iOS 中 使用UITextField格式化銀行卡號碼的解決方案

    今天小編給大家分享ios中使用UITextField格式化銀行卡號碼的實現(xiàn)思路詳解,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2016-12-12
  • iOS組件封裝與自動布局自定義表情鍵盤

    iOS組件封裝與自動布局自定義表情鍵盤

    這篇文章主要介紹了iOS組件封裝與自動布局自定義表情鍵盤的相關資料,需要的朋友可以參考下
    2016-04-04
  • iOS實現(xiàn)UIButton的拖拽功能

    iOS實現(xiàn)UIButton的拖拽功能

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)UIButton的拖拽功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • iOS 10 使用相機相簿閃退的bug修正方法

    iOS 10 使用相機相簿閃退的bug修正方法

    iOS 10 中使用相機相簿閃退的問題需要我們在Info.plist 加入指定的 key,下面小編給大家介紹下,一起看看吧
    2016-10-10
  • iOS開發(fā)之APP相關

    iOS開發(fā)之APP相關

    本文給大家介紹的是IOS開發(fā)系列文章的第一篇,給大家分享一些APP相關的知識點,非常的實用,有需要的小伙伴可以參考下
    2016-04-04
  • ios動態(tài)設置lbl文字標簽的高度

    ios動態(tài)設置lbl文字標簽的高度

    本文給大家分享的是ios動態(tài)設置lbl文字標簽的高度寬度的方法,一共給大家匯總了3種方法,小伙伴們根據(jù)自己的項目需求自由選擇。
    2015-05-05
  • iOS橫屏彈鍵盤的高度錯誤異常解決

    iOS橫屏彈鍵盤的高度錯誤異常解決

    這篇文章主要給大家介紹了關于iOS橫屏彈鍵盤的高度錯誤異常解決的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04

最新評論