Scrolling segment using HMSegmentControl

Download  HMSegmentControl from Github : Click Here

ScrollingViewController.m

#import "ScrollingViewController.h"
//#import <YAScrollSegmentControl/YAScrollSegmentControl.h>
#import "HMSegmentedControl.h"
//YAScrollSegmentControlDelegate
@interface ScrollingViewController () <UIScrollViewDelegate>

//@property (nonatomic, weak) IBOutlet YAScrollSegmentControl *scrollSegment;

@property (nonatomic, strong) HMSegmentedControl *segmentedControl4;

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation ScrollingViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"HMSegmentedControl Demo";
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    CGFloat viewWidth = CGRectGetWidth(self.view.frame);
    
    // Tying up the segmented control to a scroll view
    self.segmentedControl4 = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, 0, viewWidth, 50)];
    self.segmentedControl4.sectionTitles = @[@"Worldwide", @"Local", @"Headlines",@"Headlines 789",@"Headlines 7890101"];
    self.segmentedControl4.selectedSegmentIndex = 0;
    self.segmentedControl4.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1];
    self.segmentedControl4.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
    self.segmentedControl4.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1]};
    self.segmentedControl4.selectionIndicatorColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1];
    self.segmentedControl4.selectionStyle = HMSegmentedControlSelectionStyleBox;
    self.segmentedControl4.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.segmentedControl4.tag = 3;
    
    __weak typeof(self) weakSelf = self;
    [self.segmentedControl4 setIndexChangeBlock:^(NSInteger index) {
        [weakSelf.scrollView scrollRectToVisible:CGRectMake(viewWidth * index, 0, viewWidth, 600) animated:YES];
    }];
    
    [self.view addSubview:self.segmentedControl4];
    
//    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 52, viewWidth,400)];
//    self.scrollView.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(viewWidth * 5, 200);
    self.scrollView.delegate = self;
    [self.scrollView scrollRectToVisible:CGRectMake(0, 0, viewWidth, 200) animated:NO];
    [self.view addSubview:self.scrollView];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, viewWidth, 210)];
    [self setApperanceForLabel:label1];
    label1.text = @"Worldwide";
    [self.scrollView addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth, 0, viewWidth, 210)];
    [self setApperanceForLabel:label2];
    label2.text = @"Local";
    [self.scrollView addSubview:label2];
    
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth * 2, 0, viewWidth, 210)];
    [self setApperanceForLabel:label3];
    label3.text = @"Headlines";
    [self.scrollView addSubview:label3];
    
    UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth * 3, 0, viewWidth, 210)];
    [self setApperanceForLabel:label4];
    label4.text = @"Headlines 789";
    [self.scrollView addSubview:label4];
    
    
}


- (void)setApperanceForLabel:(UILabel *)label {
//    CGFloat hue = 1.0;  //  0.0 to 1.0
//    CGFloat saturation = 1;  //  0.5 to 1.0, away from white
//    CGFloat brightness = 1;  //  0.5 to 1.0, away from black
    
//    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
//    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
//    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
    UIColor *color = [UIColor whiteColor];
    label.backgroundColor = color;
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:21.0f];
    label.textAlignment = NSTextAlignmentCenter;
}
//
- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl {
    NSLog(@"Selected index %ld (via UIControlEventValueChanged)", (long)segmentedControl.selectedSegmentIndex);
}

- (void)uisegmentedControlChangedValue:(UISegmentedControl *)segmentedControl {
    NSLog(@"Selected index %ld", (long)segmentedControl.selectedSegmentIndex);
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
     NSLog(@"%f",pageWidth);
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    NSLog(@"%f",scrollView.contentOffset.x / pageWidth);
    NSLog(@"%f",scrollView.contentOffset.x);
    NSLog(@"%ld",(long)page);
    [self.segmentedControl4 setSelectedSegmentIndex:page animated:YES];
}


@end
Previous
Next Post »