iOS仿奇鱼旅行app横向滚动选择器

demo.gif

思路分析

1.可以横向滚动,scrollView
2.能显示7个有序的文字,7个label控件
3.两边都可以滚到边,内容是个有序数组

@interface ViewController ()<UIScrollViewDelegate>{
    UIScrollView *titleScrollView;
    UILabel *circleLabel; //入住人数
    NSMutableArray *titleArr;
}

@property (weak, nonatomic) IBOutlet UIView *inNumberView;

@end

@implementation ViewController
#define numberRow  7
#define screenWidth [UIScreen mainScreen].bounds.size.width
- (void)viewDidLoad {
    [super viewDidLoad];
   
    titleArr = [NSMutableArray array];
    for (NSInteger i = 0; i<15; i++) {
        [titleArr addObjectsFromArray:@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"不限"]];
    }
    
    CGFloat labelWidth =  screenWidth/numberRow;
    titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 85)];
    titleScrollView.contentOffset = CGPointMake((titleArr.count+1)/2 * labelWidth - 10*labelWidth, 0);
    [self.inNumberView addSubview:titleScrollView];
    titleScrollView.delegate = self;
    titleScrollView.showsHorizontalScrollIndicator = NO;
    titleScrollView.contentSize = CGSizeMake(titleArr.count *labelWidth, 0);
    circleLabel = [[UILabel alloc] initWithFrame:CGRectMake(screenWidth /2 - labelWidth/2 , 85/2 - labelWidth/2, labelWidth , labelWidth)];
    [self.inNumberView addSubview:circleLabel];
    circleLabel.layer.cornerRadius = labelWidth/2;
    circleLabel.layer.borderColor = [UIColor redColor].CGColor;
    circleLabel.backgroundColor = [UIColor whiteColor];
    circleLabel.text = @"不限";
    circleLabel.textColor = [UIColor redColor];
    circleLabel.textAlignment = NSTextAlignmentCenter;
    circleLabel.layer.borderWidth = 1;
    circleLabel.font = [UIFont systemFontOfSize:13];
    for (int i = 0; i < titleArr.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i*labelWidth, 0, labelWidth, 85)];
        [titleScrollView addSubview:label];
        label.tag = i+100;
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = titleArr[i];
    }

}
// 滚动到最近的item
- (void)snapToNearestItem{
    CGFloat pageSize = screenWidth/numberRow;
    CGPoint targetOffset = [self nearestTargetOffsetForOffset:titleScrollView.contentOffset];
    [titleScrollView setContentOffset:targetOffset animated:YES];
    UILabel *label = (UILabel *)[titleScrollView viewWithTag:3+targetOffset.x/pageSize+100];
    circleLabel.text = label.text;
    circleLabel.textAlignment = NSTextAlignmentCenter;
    circleLabel.font = [UIFont systemFontOfSize:13];
    circleLabel.textColor = [UIColor redColor];
    circleLabel.backgroundColor = [UIColor whiteColor];
    
}
// 找到最近目标点
- (CGPoint)nearestTargetOffsetForOffset:(CGPoint)offset{
    CGFloat pageSize = screenWidth/numberRow;
    NSInteger page = roundf(offset.x / pageSize);
    CGFloat targetX = pageSize * page;
    return CGPointMake(targetX, 0);
}
// 即将拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    circleLabel.textColor = [UIColor clearColor];
    circleLabel.backgroundColor = [UIColor clearColor];
}

// 结束减速
- (void)scrollViewDidEndDecelerating:(nonnull UIScrollView *)scrollView{
    if (scrollView == titleScrollView) {
        [self snapToNearestItem];
    }
}

// 结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (scrollView == titleScrollView) {
        if (!decelerate) {
            [self snapToNearestItem];
        }
    }
}

swift版本传送门

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,301评论 4 61
  • “永远不要试图给你要解雇的人留下你的处境很困难的印象,因为是他们失去工作,显然他们的处境更难。要抵御过多解释甚至表...
    新月饭店大小姐阅读 133评论 0 0
  • 浩荡的出国旅游人潮已成为中国的重要竞争力。受某上市旅游企业的委托,成都大学旅游与经济管理学院设立本专题,用于发表出...
    刘学伟阅读 2,495评论 1 2
  • 皮寶寶 入園的第一周 總算結束了 入園前 我把所有能想到最壞的可能都想到了 結果還是沒考慮周全 大皮把所有都放在了...
    宝小贝贝小宝阅读 128评论 0 0
  • 如今人们的工作和生活互相融合,很难再去用准点下班、不加班来做区隔。加班也不再是一件坏事,创业者为了自己喜爱的事业加...
    涉水爬山阅读 1,075评论 0 0