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,200評論 4 61
  • “永遠不要試圖給你要解雇的人留下你的處境很困難的印象,因為是他們失去工作,顯然他們的處境更難。要抵御過多解釋甚至表...
    新月飯店大小姐閱讀 132評論 0 0
  • 浩蕩的出國旅游人潮已成為中國的重要競爭力。受某上市旅游企業的委托,成都大學旅游與經濟管理學院設立本專題,用于發表出...
    劉學偉閱讀 2,480評論 1 2
  • 皮寶寶 入園的第一周 總算結束了 入園前 我把所有能想到最壞的可能都想到了 結果還是沒考慮周全 大皮把所有都放在了...
    寶小貝貝小寶閱讀 124評論 0 0
  • 如今人們的工作和生活互相融合,很難再去用準點下班、不加班來做區隔。加班也不再是一件壞事,創業者為了自己喜愛的事業加...
    涉水爬山閱讀 1,062評論 0 0