仿iOS新浪新闻 slider

效果图:

slide.gif

GitHub:
https://github.com/boundlessocean/sliderViewController.git

主要思路

第一种情况,手滑动childViewController的View时改变选项卡

1.监听控制器的ScrollView滑动,将偏移量传给 标题选项卡(optionalView)
#pragma mark - - scrollView
/** 偏移量控制显示状态 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.optionalView.contentOffSetX = scrollView.contentOffset.x;
}
2.标题选项卡(optionalView)收到偏移量变化后,根据偏移量contentOffSetX来改变 标题(item)的大小,和(滑条)slider的位置及宽度
-(void)setContentOffSetX:(CGFloat)contentOffSetX{
    _contentOffSetX = contentOffSetX;
    NSInteger index = (NSInteger)contentOffSetX / (NSInteger)[UIScreen mainScreen].bounds.size.width;
    // progress 0(屏幕边缘开始) -  1 (满屏结束)
    CGFloat progress =( _contentOffSetX - index * [UIScreen mainScreen].bounds.size.width )/ [[UIScreen mainScreen]bounds].size.width;
    // 左右选项卡(item)
    BLTitleItem *leftItem = [self viewWithTag:index + 100];
    BLTitleItem *rightItem = [self viewWithTag:index + 101];
    // item 根据progress改变状态
    leftItem.progress = 1 - progress;
    rightItem.progress = progress;
    // 滑条sliderView 根据progress 改变状态
    CGRect frame = _sliderView.frame;
    frame.origin.x = index * itemWidth + (itemWidth - sliderViewWidth)/2;
    _sliderView.frame = frame;
    _sliderView.progress = progress;
}
3.item 和slider根据progress改变状态
item
// 滑动进度
-(void)setProgress:(CGFloat)progress {
    _progress = progress;
    [self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    if (_progress > 0 && _progress <= 1) {
        [self setTitleColor:[UIColor colorWithRed:0 + 1*_progress green:0 blue:0 alpha:1] forState:UIControlStateNormal];
        self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 + 0.1 * _progress, 1 + 0.1 * _progress);
    }
}
slider
// 滑动进度
- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    if (_progress > 0 && _progress <= 1) {
        CGRect frame = self.frame;
        frame.size.width = 15 + _itemWidth * (_progress > 0.5 ? 1 - _progress : _progress);
        frame.origin.x = frame.origin.x + _itemWidth * _progress;
        self.frame = frame;
    }
}

第二种情况 点击item的变化,比较简单

#pragma mark - - button event

-(void)itemClicked:(BLTitleItem *)sender{
    NSInteger index = (NSInteger)_contentOffSetX / (NSInteger)[UIScreen mainScreen].bounds.size.width;
    if (sender.tag - 100 == index) return;
    BLTitleItem *currentItem = [self viewWithTag:index + 100];
    
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [currentItem setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    CGRect frame = _sliderView.frame;
    frame.origin.x = (sender.tag - 100) * itemWidth + (itemWidth - sliderViewWidth)/2;
    
    [UIView animateWithDuration:0.2 animations:^{
        sender.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
        currentItem.transform = CGAffineTransformIdentity;
        _sliderView.frame = frame;
    }];
    !_titleItemClickedCallBackBlock ? : _titleItemClickedCallBackBlock(sender.tag);
}

选项卡超出屏幕时自动滑动到可视位置

主要是监听slider(滑条)的位置来改变选项卡(optionalView)的contentOffset

/** 监听frame改变 */
        [self addObserver:self forKeyPath:@"_sliderView.frame" options:NSKeyValueObservingOptionNew context:nil];
/** 接收通知 */
-(void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context{
    CGFloat itemVisionblePositionMax = _sliderView.frame.origin.x - (itemWidth - sliderViewWidth)/2 + 2*itemWidth;
    CGFloat itemVisionblePositionMin = _sliderView.frame.origin.x - (itemWidth - sliderViewWidth)/2 - itemWidth;
    
    // 右滑
    if (itemVisionblePositionMax >= self.frame.size.width + self.contentOffset.x &&
        itemVisionblePositionMax <= self.contentSize.width) {
        [UIView animateWithDuration:0.2 animations:^{
            self.contentOffset = CGPointMake(itemVisionblePositionMax - self.frame.size.width, 0);
        }];
    }
    // 左滑
    if (itemVisionblePositionMin < self.contentOffset.x &&
        itemVisionblePositionMin >= 0) {
        [UIView animateWithDuration:0.2 animations:^{
            self.contentOffset = CGPointMake(itemVisionblePositionMin, 0);
        }];
    }
}

你可能感兴趣的:(仿iOS新浪新闻 slider)