Skip to content

Commit

Permalink
修改文件目录结构
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoyuanng committed Jun 16, 2017
1 parent 4a90632 commit 204ecbe
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 323 deletions.
16 changes: 0 additions & 16 deletions LYSheetController/LYSheetCell.h

This file was deleted.

31 changes: 0 additions & 31 deletions LYSheetController/LYSheetCell.m

This file was deleted.

42 changes: 19 additions & 23 deletions LYSheetController/LYSheetController.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
//
// LYSheetController.h
// NicelandCenterControl
//
// Created by Ju Liaoyuan on 17/4/14.
// Copyright © 2017年 Leo. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "LYSheetModel.h"
#import "LYSheetProtocol.h"

NS_ASSUME_NONNULL_BEGIN

@class LYSheetController;
@class LYSheetCell;

@protocol LYSheetControllerDelegate <NSObject>

- (void)sheetController:(LYSheetController *)sheetController didSelectRowAtIndexPath:(NSInteger)indexPath;

@end
typedef NS_ENUM(NSInteger, LYSheetControllerStyle) {
LYSheetControllerStylePlain,
LYSheetControllerStyleGroup
};

@interface LYSheetController : UIViewController
/**
Expand All @@ -31,7 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
sheet controller data source
*/
@property (nonatomic, copy, nullable) NSArray<__kindof LYSheetModel *> *dataSource;
@property (nonatomic, copy, nullable) NSArray *dataSource;

/**
separator color
Expand All @@ -44,7 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong) UIColor *sheetBackgroundColor;

/**
tap view dismiss. default is YES, if YES, - dismissSheetController: method `animated` always is YES whatever you set animated YES or NO.
tap background view(not sheet view) dismiss. default is YES, if YES, - dismissSheetController: method `animated` always is YES whatever you set animated YES or NO.
*/
@property (nonatomic, assign, getter=isGestureEnable) BOOL gestureEnable;

Expand All @@ -70,40 +65,41 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign, getter=isDismissWhenSelected) BOOL dismissWhenSelected;

/**
dismissWhenSelected YES 时,选择某个 cell 后,sheet 消失后的回调
when dismissWhenSelected is YES , callback after selected cell
*/
@property (nonatomic, copy) void(^dismissHandler)(BOOL);

/**
初始化方式
initialization
@param dataSource 数据源
@param dataSource datasource
@return instance object
*/
- (instancetype)initWithDataSource:(NSArray<__kindof LYSheetModel *> * _Nullable)dataSource;
- (instancetype)initWithDataSource:(NSArray * _Nullable)dataSource;

/**
刷新 sheet
reload sheet
*/
- (void)reloadSheet;

/**
注册 cell
default is LYSheetCell, subclass LYSheetCell to custom.
@param cell 要注册的 cell
regist cell
default is LYSheetCell, inherit `LYSheetCell` protocol to custom.
@param cell custom cell
@param style cell's style
*/
- (void)registSheetControllerCell:(__kindof LYSheetCell *)cell forStyle:(LYSheetStyle)style;
- (void)registSheetControllerCell:(Class<LYSheetCell>)cell forStyle:(LYSheetStyle)style;

/**
展示 sheet
show sheet
@param animated animated
@param completionHandler completionHandler
*/
- (void)showSheetControllerWithAnimated:(BOOL)animated completionHandler:(void(^ __nullable)(BOOL success))completionHandler;

/**
移除 sheet
remove sheet
@param animated animated
@param completionHandler completionHandler
Expand Down
123 changes: 77 additions & 46 deletions LYSheetController/LYSheetController.m
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
//
// LYSheetController.m
// NicelandCenterControl
//
// Created by Ju Liaoyuan on 17/4/14.
// Copyright © 2017年 Leo. All rights reserved.
//

#import "LYSheetController.h"
#import "LYSheetCell.h"
#import "LYSheetModel.h"
#import "UIViewController+Active.h"
#import "UIViewController+LYActive.h"

#define LYScreenWidth [[UIScreen mainScreen] bounds].size.width
#define LYScreenHeight [[UIScreen mainScreen] bounds].size.height

static NSString * const LYSheetControllerStyleDefault = @"LYSheetControllerStyleDefault";
static NSString * const LYSheetControllerStyleCancel = @"LYSheetControllerStyleCancel";
static const NSTimeInterval animationDuration = 0.2;

#define LYScreenWidth [[UIScreen mainScreen]bounds].size.width
#define LYScreenHeight [[UIScreen mainScreen] bounds].size.height

static CGFloat LYSheetRowHeight = 50.f;

@interface LYSheetController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
Expand Down Expand Up @@ -62,33 +58,52 @@ - (void)viewWillDisappear:(BOOL)animated {
}
}

/// 初始化默认值
#pragma mark - private
#pragma mark

/// default config
- (void)defaultConfig {
self.presenting = NO;
self.scrollEnableAuto = YES;
self.gestureEnable = YES;
self.scrollEnableAuto = YES;
self.dismissWhenSelected = YES;
self.rowHeight = LYSheetRowHeight;
self.maxSheetHeight = LYScreenHeight * 2 / 3;
self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}

- (void)tableViewScrollEnable {
if (!self.scrollEnableAuto) {
self.tableView.scrollEnabled = self.scrollEnable;
return;
}
if ([self tableViewHeight] > self.maxSheetHeight) {
self.tableView.scrollEnabled = YES;
} else {
self.tableView.scrollEnabled = NO;
}
}

- (CGFloat)tableViewHeight {
return self.dataSource.count * self.rowHeight + [self tableView:self.tableView heightForHeaderInSection:0];
}

#pragma mark - public
#pragma mark
- (instancetype)initWithDataSource:(NSArray<__kindof LYSheetModel *> *)dataSource {
- (instancetype)initWithDataSource:(NSArray *)dataSource {
self.dataSource = dataSource;
return [self init];
}

- (void)registSheetControllerCell:(__kindof LYSheetCell *)cell forStyle:(LYSheetStyle)style {
if (style == kLYSheetStyleDefault) {
[self.tableView registerClass:[cell class] forCellReuseIdentifier:LYSheetControllerStyleDefault];
} else if (style == kLYSheetStyleCancel) {
[self.tableView registerClass:[cell class] forCellReuseIdentifier:LYSheetControllerStyleCancel];
} else if (style == (kLYSheetStyleCancel | kLYSheetStyleDefault)) {
[self.tableView registerClass:[cell class] forCellReuseIdentifier:LYSheetControllerStyleDefault];
[self.tableView registerClass:[cell class] forCellReuseIdentifier:LYSheetControllerStyleCancel];
- (void)registSheetControllerCell:(Class<LYSheetCell>)cell forStyle:(LYSheetStyle)style {
if (style == LYSheetStyleDefault) {
[self.tableView registerClass:cell forCellReuseIdentifier:LYSheetControllerStyleDefault];
} else if (style == LYSheetStyleCancel) {
[self.tableView registerClass:cell forCellReuseIdentifier:LYSheetControllerStyleCancel];
} else if (style == (LYSheetStyleCancel | LYSheetStyleDefault)) {
[self.tableView registerClass:cell forCellReuseIdentifier:LYSheetControllerStyleDefault];
[self.tableView registerClass:cell forCellReuseIdentifier:LYSheetControllerStyleCancel];
}
}

Expand All @@ -97,16 +112,17 @@ - (void)reloadSheet {
}

- (void)showSheetControllerWithAnimated:(BOOL)animated completionHandler:(void (^ _Nullable)(BOOL))completionHandler {
if (self.isPresenting) return;
UIViewController *active = [UIViewController activeViewController];
if (self.isPresenting) return; // if current sheet view is showing, do nothing.
[self tableViewScrollEnable]; // Determine whether the tableView can scroll
UIViewController *active = [UIViewController activeViewController]; // Get the top of the navigation stack
if (active && [active isKindOfClass:[UIViewController class]]) {
[active presentViewController:self animated:NO completion:^{
[active presentViewController:self animated:NO completion:^{ // cancel present animation.
if (animated) {
[UIView animateWithDuration:animationDuration animations:^{
self.tableView.frame = CGRectMake(0, LYScreenHeight - MIN(self.maxSheetHeight, self.dataSource.count * self.rowHeight), LYScreenWidth, MIN(self.maxSheetHeight, self.dataSource.count * self.rowHeight));
[UIView animateWithDuration:animationDuration animations:^{ // add tableview animation.
self.tableView.frame = CGRectMake(0, LYScreenHeight - MIN(self.maxSheetHeight, [self tableViewHeight]), LYScreenWidth, MIN(self.maxSheetHeight, [self tableViewHeight]));
}];
} else {
self.tableView.frame = CGRectMake(0, LYScreenHeight - MIN(self.maxSheetHeight, self.dataSource.count * self.rowHeight), LYScreenWidth, MIN(self.maxSheetHeight, self.dataSource.count * self.rowHeight));
self.tableView.frame = CGRectMake(0, LYScreenHeight - MIN(self.maxSheetHeight, [self tableViewHeight]), LYScreenWidth, MIN(self.maxSheetHeight, [self tableViewHeight]));
}
!completionHandler ? : completionHandler(YES);
}];
Expand All @@ -121,13 +137,14 @@ - (void)dismissSheetControllerWithAnimated:(BOOL)animated completionHandler:(voi
return;
}
[UIView animateWithDuration:animationDuration animations:^{
self.tableView.frame = CGRectMake(0, LYScreenHeight, LYScreenWidth, MIN(self.maxSheetHeight, self.dataSource.count * self.rowHeight));
self.tableView.frame = CGRectMake(0, LYScreenHeight, LYScreenWidth, MIN(self.maxSheetHeight, [self tableViewHeight]));
} completion:^(BOOL finished) {
[self dismissViewControllerAnimated:NO completion:nil];
!completionHandler ? : completionHandler(finished);
}];
}


#pragma mark - custom
#pragma mark

Expand All @@ -141,26 +158,23 @@ - (void)setSheetBackgroundColor:(UIColor *)sheetBackgroundColor {
self.tableView.backgroundColor = sheetBackgroundColor;
}

- (void)setDataSource:(NSArray<__kindof LYSheetModel *> *)dataSource {
- (void)setDataSource:(NSArray *)dataSource {
_dataSource = dataSource;
[self.tableView reloadData];
}

- (void)setScrollEnable:(BOOL)scrollEnable {
_scrollEnable = scrollEnable;
self.tableView.scrollEnabled = scrollEnable;
[self tableViewScrollEnable];
}

- (void)setScrollEnableAuto:(BOOL)scrollEnableAuto {
_scrollEnableAuto = scrollEnableAuto;
if (!scrollEnableAuto) return;
if (self.dataSource.count * self.rowHeight > self.maxSheetHeight) {
self.tableView.scrollEnabled = YES;
} else {
self.tableView.scrollEnabled = NO;
}
[self tableViewScrollEnable];
}



#pragma mark - delegate && datasource
#pragma mark

Expand All @@ -173,16 +187,19 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
LYSheetCell *cell = nil;
LYSheetModel *model = self.dataSource[indexPath.row];
NSAssert(model.style, @"必须要指定 cell 的 style");
if (model.style == kLYSheetStyleDefault) {
UITableViewCell<LYSheetCell> *cell = nil;
id<LYSheetModel> model = self.dataSource[indexPath.row];
NSAssert(model.sheetStyle, @"You must specify a type");
if (model.sheetStyle == LYSheetStyleDefault) {
cell = [tableView dequeueReusableCellWithIdentifier:LYSheetControllerStyleDefault forIndexPath:indexPath];
}
if (model.style == kLYSheetStyleCancel) {
if (model.sheetStyle == LYSheetStyleCancel) {
cell = [tableView dequeueReusableCellWithIdentifier:LYSheetControllerStyleCancel forIndexPath:indexPath];
}
[cell bindModel:model];

if ([cell respondsToSelector:@selector(bindModel:)]) {
[cell bindModel:model];
}
return cell;
}

Expand All @@ -200,6 +217,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
}
}

/// full screen.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
Expand All @@ -215,14 +233,30 @@ -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cel
}
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (self.delegate && [self.delegate respondsToSelector:@selector(headerHeightForSheetContoller:)]) {
return [self.delegate headerHeightForSheetContoller:self];
}
return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (self.delegate && [self.delegate respondsToSelector:@selector(headerViewForSheetContoller:)]) {
return [self.delegate headerViewForSheetContoller:self];
}
return nil;
}

- (void)tapGestureAction {
if (!self.isGestureEnable) return;
[self dismissSheetControllerWithAnimated:YES completionHandler:nil];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:NSClassFromString(@"UITableViewCellContentView")]) {
return NO;
// if touch point location in tableview , not response tap gesture.
CGPoint location = [touch locationInView:self.tableView];
if (CGRectContainsPoint(self.tableView.bounds, location)) {
return NO;
}
return YES;
}
Expand All @@ -231,17 +265,14 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceive

- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LYScreenHeight, LYScreenWidth, MIN(LYScreenHeight * 2 / 3, self.dataSource.count * LYSheetRowHeight)) style:UITableViewStylePlain];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LYScreenHeight, LYScreenWidth, MIN(LYScreenHeight * 2 / 3, 0)) style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[LYSheetCell class] forCellReuseIdentifier:LYSheetControllerStyleDefault];
[_tableView registerClass:[LYSheetCell class] forCellReuseIdentifier:LYSheetControllerStyleCancel];
[self.view addSubview:_tableView];
}
return _tableView;
}


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

0 comments on commit 204ecbe

Please sign in to comment.