gpt4 book ai didi

ios - HMSegmentedControl 在 Swift 中将分段控件绑定(bind)到 ScrollView ?

转载 作者:行者123 更新时间:2023-12-01 16:09:12 24 4
gpt4 key购买 nike

我正在使用流行的 HMSegmentedControl 在我的项目中呈现片段。但是现在我面临着一个灾难性的问题,因为我无法将我的片段绑定(bind)到 ScrollView 。由于示例在 objective-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];

谁有做过这个第三方的能给我一些解决方案吗?

最佳答案

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

import UIKit
import HSegmentControl

class ViewController: UIViewController, HSegmentControlDataSource {

@IBOutlet weak var segmentControl: HSegmentControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


segmentControl.dataSource = self
segmentControl.numberOfDisplayedSegments = 3
segmentControl.segmentIndicatorViewContentMode = UIViewContentMode.bottom
// segmentControl.selectedTitleFont = UIFont.boldSystemFont(ofSize: 17)
segmentControl.selectedTitleColor = UIColor.white
// segmentControl.unselectedTitleFont = UIFont.systemFont(ofSize: 17)
// segmentControl.unselectedTitleColor = UIColor.darkGray
// segmentControl.segmentIndicatorImage = UIImage(named: "ind_img")
segmentControl.segmentIndicatorView.backgroundColor = UIColor(red: 31.0/255.0, green: 48.0/255.0, blue: 93.0/255.0, alpha: 1.0)

segmentControl.selectedIndex = 2
}

// MARK: - HSegmentControlDataSource protocol
func numberOfSegments(_ segmentControl: HSegmentControl) -> Int {
return 5
}

func segmentControl(_ segmentControl: HSegmentControl, titleOfIndex index: Int) -> String {
return ["Kavin","Sitharth", "Raj", "Selva", "Rajesh"][index]
}

func segmentControl(_ segmentControl: HSegmentControl, segmentBackgroundViewOfIndex index: Int) -> UIView {
let view = UIView()
// view.backgroundColor = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1)
return view
}

@IBAction func valueChanged(_ sender: AnyObject) {
print("value did change to \((sender as! HSegmentControl).selectedIndex)")
}
}

关于ios - HMSegmentedControl 在 Swift 中将分段控件绑定(bind)到 ScrollView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35292053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com