如何在不手动滚动的情况下将HMSegmentedControl水平滚动到所选索引

时间:2017-05-15 08:08:04

标签: ios objective-c iphone

enter image description here我正在使用HMSegmentedControl 6个章节标题(星期一,星期二,星期三,星期四,星期五,星期六)。 当今天是星期五,那么我想将HMSegmentedControl滚动到星期五的索引(即索引4)。 我已将HMSegmentedControl.selectedSegmentIndex设置为星期五的索引,但它不会滚动到该索引。 当我手动点击星期五然后HMSegmentedControl滚动。 有谁知道我能做到这一点。

segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[NSLocalizedString(@"MONDAY", nil), NSLocalizedString(@"TUESDAY", nil), NSLocalizedString(@"WEDNESDAY", nil), NSLocalizedString(@"THURSDAY", nil), NSLocalizedString(@"FRIDAY", nil), NSLocalizedString(@"SATURDAY", nil)]];

    segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
    CGFloat viewWidth = CGRectGetWidth(self.view.frame);
    segmentedControl.frame = CGRectMake(0, 44, viewWidth, 45);
    segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10);
    segmentedControl.backgroundColor = [Nav_Bar_Color toColor];
    segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
    segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    segmentedControl.selectionIndicatorColor = [UIColor whiteColor];
    segmentedControl.verticalDividerEnabled = YES;
    segmentedControl.verticalDividerColor = [UIColor clearColor];
    segmentedControl.verticalDividerWidth = 1.5f;
    [segmentedControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
        NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont fontWithName:FONT_NAME size:FONT_SIZE]}];
        return attString;
    }];
    [segmentedControl addTarget:self action:@selector(loadTimeTableData:) forControlEvents:UIControlEventValueChanged];

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:[NSDate date]];
    int weekdayNumber = [components weekday];
    weekdayNumber = weekdayNumber - 1;

    switch (weekdayNumber) {
        case 1:
        case 7:
            segmentedControl.selectedSegmentIndex = 0;
            break;
        case 2:
            segmentedControl.selectedSegmentIndex = 1;
            break;
        case 3:
            segmentedControl.selectedSegmentIndex = 2;
            break;
        case 4:
            segmentedControl.selectedSegmentIndex = 3;
            break;
        case 5:
            segmentedControl.selectedSegmentIndex = 4;
            break;
        case 6:
            segmentedControl.selectedSegmentIndex = 5;
            break;
        default:
            break;
    }

    [segmentedControl setSelectedSegmentIndex:segmentedControl.selectedSegmentIndex animated:YES];

    [self.view addSubview:segmentedControl];

1 个答案:

答案 0 :(得分:0)

您应该在segementControl selected at index方法中调用loadTimeTableData:方法。