iOS-仿简书个人主页
好久没打卡啦,最近项目虽不是很忙,还有点闲呢?闲得想法多啦许多。思考着,这条技术路线是不是正确的,值不值得耗费精力时间去提升、专研......经常浏览别人的博客,细心点会发现,有些大神曾今多么热爱技术,文章见证着他的成长,然而断更几年啦,再也没有发过关于技术的文章,悄然退出这个行业,引发啦我的一些思考......
看图说话,分析需求和解法
-
最外层用tableview(mainTableview),上滑消失部分是mainTbaleview的表头,悬浮的titleView是mainTableview的sectionHead,当然mainTableview类型(UITableViewStylePlain)
-
动态和朋友的内容部分,便是mainTableview的Cell,效果是左右翻页滚动,很容易想到UIScrollView嵌套两个tableview,这里用
<ZJScrollPageView>框架实现 -
处理tableview的悬停位置和滑动冲突;上滑距离小于头部时,内容tableview的偏移量为CGPointZero,
-
内容可以左右滑动啦,但是会使滑动返回失效,系统滑动返回也是一个右滑手势,由于内容可以右滑,手势在内容上面就响应啦,没有传递下去,所以系统的滑动返回自然失效啦,想办法让这个手势传递下去,就会调用滑动返回方法啦。
关键代码
mainTbaleview 是一个可以识别多个手势的tableview
@interface ZJCustomGestureTableView : UITableView
@end
@implementation ZJCustomGestureTableView
/// 返回YES同时识别多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
@end
ZJScrollPageView 加载内容,具体用法可以去gitHub下载
- (ZJScrollSegmentView *)segmentView {
if (_segmentView == nil) {
ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
style.autoAdjustTitlesWidth = YES;
// 渐变
style.gradualChangeTitleColor = YES;
//标题一般状态颜色 --- 注意一定要使用RGB空间的颜色值
style.normalTitleColor = RGB(0, 0, 0);
//标题选中状态颜色 --- 注意一定要使用RGB空间的颜色值
style.selectedTitleColor = [UIColor colorWithRed:235.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1.0];
style.titleFont = [UIFont systemFontOfSize:15];
style.showLine = YES;
style.scrollLineColor = [UIColor redColor];
style.contentViewBounces = NO;
self.titles = @[
@"动态",
@"朋友",
];
// 注意: 一定要避免循环引用!!
__weak typeof(self) weakSelf = self;
ZJScrollSegmentView *segment = [[ZJScrollSegmentView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, segmentViewHeight) segmentStyle:style delegate:self titles:self.titles titleDidClick:^(ZJTitleView *titleView, NSInteger index) {
[weakSelf.contentView setContentOffSet:CGPointMake(weakSelf.contentView.bounds.size.width * index, 0.0) animated:YES];
}];
segment.backgroundColor = [UIColor whiteColor];
_segmentView = segment;
}
return _segmentView;
}
#pragma ZJScrollPageViewDelegate 代理方法
- (NSInteger)numberOfChildViewControllers {
return self.titles.count;
}
- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
if (!childVc) {
if (index==0) {
//动态
childVc = [[JPDynamicVController alloc] init];
JPDynamicVController *vc = (JPDynamicVController *)childVc;
vc.delegate = self;
} else {
//朋友
childVc = [[JPMyFrendVController alloc] init];
JPMyFrendVController *vc = (JPMyFrendVController *)childVc;
vc.delegate = self;
}
}
return childVc;
}
用通知控制内容在头部消失后才可滚动,子控制器继承ZJPageViewController,统一控制不同类型VC的滚动
.h文件
#import <UIKit/UIKit.h>
@protocol ZJPageViewControllerDelegate <NSObject>
- (void)scrollViewIsScrolling:(UIScrollView *)scrollView;
@end
@interface ZJPageViewController : UIViewController
// 代理
@property(weak, nonatomic)id<ZJPageViewControllerDelegate> delegate;
@property (strong, nonatomic) UIScrollView *scrollView;
@end
.m文件
#import "ZJPageViewController.h"
@interface ZJPageViewController ()<UIScrollViewDelegate>
@end
@implementation ZJPageViewController
extern NSString *const ZJParentTableViewDidLeaveFromTopNotification;
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
/// 利用通知可以同时修改所有的子控制器的scrollView的contentOffset为CGPointZero
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(leaveFromTop) name:ZJParentTableViewDidLeaveFromTopNotification object:nil];
}
- (void)leaveFromTop {
_scrollView.contentOffset = CGPointZero;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = scrollView;
// _scrollView.bounces = NO;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(scrollViewIsScrolling:)]) {
[self.delegate scrollViewIsScrolling:scrollView];
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
解决滑动返回手势冲突UIScrollView建个分类
#import "UIScrollView+PopGesture.h"
@implementation UIScrollView (PopGesture)
//此方法返回YES时,手势事件会一直往下传递,不论当前层次是否对该事件进行响应。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([self panBack:gestureRecognizer]) {
return YES;
}
return NO;
}
//location_X可自己定义,其代表的是滑动返回距左边的有效长度
- (BOOL)panBack:(UIGestureRecognizer *)gestureRecognizer
{
//是滑动返回距左边的有效长度
int location_X = 40;
if (gestureRecognizer == self.panGestureRecognizer) {
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint point = [pan translationInView:self];
UIGestureRecognizerState state = gestureRecognizer.state;
if (UIGestureRecognizerStateBegan == state || UIGestureRecognizerStatePossible == state) {
CGPoint location = [gestureRecognizer locationInView:self];
//下面的是只允许在第一张时滑动返回生效
if (point.x > 0 && location.x < location_X && self.contentOffset.x <= 0) {
return YES;
}
// 这是允许每张图片都可实现滑动返回
// int temp1 = location.x;
// int temp2 = SCREEN_WIDTH;
// NSInteger XX = temp1 % temp2;
// if (point.x > 0 && XX < location_X) {
// return YES;
// }
}
}
return NO;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([self panBack:gestureRecognizer]) {
return NO;
}
return YES;
}
@end
好吧!思考结果是,认真做好当前工作,看书学习丰富自己,学处事,经营人际关系,路少了是因为自己的见识少格局小,想找到适合自己的路再努力,可是现有知识还不足以让我找到正确的路。提升自己,便能发现机会,在机会来临时,你便能把握机会。