HMSegmentedControl将分段控件绑定到Swift中的滚动视图?

时间:2016-02-09 12:25:52

标签: ios swift cocoapods

我正在使用流行的HMSegmentedControl在我的项目中呈现细分。但是我现在面临一个灾难性的问题,因为我无法将我的片段绑定到滚动视图。由于示例是在客观C中。在Swift中这样做变得很困难。代码是这样的 -

 self.segmentedControl4 = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, 260, viewWidth, 50)];
self.segmentedControl4.sectionTitles = @[@"Worldwide", @"Local", @"Headlines"];
self.segmentedControl4.selectedSegmentIndex = 1;
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 = HMSegmentedControlSelectionIndicatorLocationUp;
self.segmentedControl4.tag = 3;

__weak typeof(self) weakSelf = self;

[self.segmentedControl4 setIndexChangeBlock:^(NSInteger index)
{
    [weakSelf.scrollView scrollRectToVisible:CGRectMake(viewWidth * index, 0, viewWidth, 200) animated:YES];
}];

[self.view addSubview:self.segmentedControl4];

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 310, viewWidth, 210)];
self.scrollView.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1];
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.contentSize = CGSizeMake(viewWidth * 3, 200);
self.scrollView.delegate = self;
[self.scrollView scrollRectToVisible:CGRectMake(viewWidth, 0, viewWidth, 200) animated:NO];
[self.view addSubview:self.scrollView];

任何参与过第三方工作的人都能给我一些解决方案吗?

2 个答案:

答案 0 :(得分:1)

以下是滚动分段控件的代码。您也可以从here下载源代码。

require(['sinon-chrome'], function (chrome) { 
    var popup = require('../../source/scripts/popup');

    describe('sumit', function(){
        before(function () {
            global.chrome = chrome;
        });
        it('Should return 1', function(){
            assert(popup.sum(0,1) === 1);
        });
    })
});

答案 1 :(得分:0)

只需正常设置分段控件并设置indexChangeBlock以滚动滚动视图:

segmentedControl4.indexChangeBlock = {
    let frame = CGRectMake(viewWidth * index, 0, viewWidth, 200)
    scrollView.scrollRectToVisible(frame, animated:true)
}

每次分段控件的索引发生变化时,这将滚动滚动视图。