废话不多说直接上代码
一、头文件
#import <UIKit/UIKit.h>
@interface SliderCategoryView : UIView
- (void)showView;
@end
二、实现文件
#import "SliderCategoryView.h"
#import "HomeTableView.h"
@interface SliderCategoryView ()<UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HomeTableView *homeTableView;
@end
@implementation SliderCategoryView
- (instancetype)init{
self = [super init];
if(self){
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
tap.delegate = self;
[self addGestureRecognizer:tap];
[self setupSubviews];
}
return self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
return NO;
} else if ([NSStringFromClass([touch.view class]) isEqualToString:@"UICollectionViewCellContentView"]) {
return NO;
}
return YES;
}
- (void)showView {
self.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);
self.alpha = 0;
[UIView animateWithDuration:0.8 animations:^{
self.frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);
[[[[UIApplication sharedApplication] delegate] window] addSubview:self];
self.alpha = 1;
}];
}
- (void)dismissView {
[UIView animateWithDuration:0.8 animations:^{
self.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);
self.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
#pragma mark - SetupSubviewsUI
- (void)setupSubviews{
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
// 默认加载显示TableView
[self addSubview:self.tableView];
}
- (HomeTableView *)homeTableView {
if (!_homeTableView) {
_homeTableView = [[HomeTableView alloc] init];
}
return _homeTableView;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] init];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = UIColor.whiteColor;
_tableView.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width/5, 0, UIScreen.mainScreen.bounds.size.width * 4/5, UIScreen.mainScreen.bounds.size.height);
}
return _tableView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld+%ld",indexPath.section,indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.homeTableView.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width, 0, UIScreen.mainScreen.bounds.size.width * 4/5, UIScreen.mainScreen.bounds.size.height);
[UIView animateWithDuration:0.5 animations:^{
self.homeTableView.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width/5, 0, UIScreen.mainScreen.bounds.size.width * 4/5, UIScreen.mainScreen.bounds.size.height);
[self addSubview:self.homeTableView];
} completion:^(BOOL finished) {
}];
}
@end
HomeTableView代码
#import "HomeTableView.h"
@interface HomeTableView ()<UITableViewDelegate, UITableViewDataSource>
@end
@implementation HomeTableView
- (instancetype)init{
self = [super init];
if(self){
self.delegate = self;
self.dataSource = self;
self.backgroundColor = UIColor.whiteColor;
}
return self;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 45;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] init];
view.backgroundColor = UIColor.redColor;
UIButton *button = [[UIButton alloc] init];
[button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
button.frame = CGRectMake(15, 45/2-30/2, 30, 30);
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
[button addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
return view;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld+%ld",indexPath.section,indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self dismiss];
}
- (void)dismiss {
[UIView animateWithDuration:0.5 animations:^{
self.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width, 0, UIScreen.mainScreen.bounds.size.width * 4/5, UIScreen.mainScreen.bounds.size.height);
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
Swift版本(其实差别不大)
import UIKit
class SliderCategory_Swift: UIView {
private lazy var tableView:UITableView = {
let tableView = UITableView.init()
tableView.backgroundColor = UIColor.white
tableView.delegate = self
tableView.dataSource = self
tableView.frame = CGRect(x: UIScreen.main.bounds.width/5, y: 0, width: UIScreen.main.bounds.width*4/5, height: UIScreen.main.bounds.height)
return tableView
}()
private lazy var homeTableView:HomeTableView = {
let tableView = HomeTableView.init(frame: CGRect(x: UIScreen.main.bounds.width/5, y: 0, width: UIScreen.main.bounds.width*4/5, height: UIScreen.main.bounds.height), style: .grouped)
return tableView
}()
public func showView() {
self.frame = CGRect(x: UIScreen.main.bounds.width, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.size.height)
self.alpha = 0
UIView.animate(withDuration: 0.8) {
self.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.size.height)
UIApplication.shared.delegate?.window??.addSubview(self)
self.alpha = 1
}
}
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.init(white: 0, alpha: 0.3)
let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(dismissView))
tap.delegate = self
self .addGestureRecognizer(tap)
/// 默认加载一个tableView
self.addSubview(tableView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc private func dismissView() {
UIView.animate(withDuration: 0.8, animations: {
self.frame = CGRect(x: UIScreen.main.bounds.width, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.size.height)
self.alpha = 0
}) { (bool) in
self.removeFromSuperview()
}
}
}
extension SliderCategory_Swift: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
print(NSStringFromClass((touch.view?.classForCoder)!))
if NSStringFromClass((touch.view?.classForCoder)!) == "UITableViewCellContentView" {
return false
} else if NSStringFromClass((touch.view?.classForCoder)!) == "UICollectionViewCellContentView" {
return false
} else {
return true
}
}
}
extension SliderCategory_Swift: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell.init(style: .default, reuseIdentifier: "cell")
}
cell?.textLabel?.text = "\(indexPath.row+1)"
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.homeTableView.frame = CGRect(x: UIScreen.main.bounds.width, y: 0, width: UIScreen.main.bounds.width*4/5, height: UIScreen.main.bounds.size.height)
UIView.animate(withDuration: 0.5) {
self.homeTableView.frame = CGRect(x: UIScreen.main.bounds.width/5, y: 0, width: UIScreen.main.bounds.width*4/5, height: UIScreen.main.bounds.size.height)
self.addSubview(self.homeTableView)
}
}
}
二、HomeTableView代码
import UIKit
class HomeTableView: UITableView {
override init(frame: CGRect, style: UITableViewStyle) {
super.init(frame: frame, style: style)
self.delegate = self
self.dataSource = self
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc private func dismissView() {
UIView.animate(withDuration: 0.5, animations: {
self.frame = CGRect(x: UIScreen.main.bounds.width, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.size.height)
}) { (bool) in
self.removeFromSuperview()
}
}
}
extension HomeTableView: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView.init()
view.backgroundColor = UIColor.red
let button = UIButton.init()
button.setImage(UIImage.init(named: "back"), for: .normal)
button.frame = CGRect(x: 15, y: 15, width: 30, height: 30)
button.imageView?.contentMode = .scaleAspectFit
button.addTarget(self, action: #selector(dismissView), for: .touchUpInside)
view.addSubview(button)
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 45
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell.init(style: .default, reuseIdentifier: "cell")
}
cell?.textLabel?.text = "\(indexPath.row+1)"
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
dismissView()
}
}
效果图
效果图.gifDemo地址
https://github.com/wudan-ios/SliderCategory.git
网友评论