我解决了我的问题。
这个小代码段的功能:
- 宽度是动态的
- 它是动画的
- 它对于将来的功能更具可定制性
class FirstViewController: UIViewController { let rectShape = CAShapeLayer() let indicatorHeight: CGFloat = 5 var indicatorWidth: CGFloat! let indicatorBottomMargin: CGFloat = 2 let indicatorLeftMargin: CGFloat = 2 override func viewDidLoad() { super.viewDidLoad() // setup tabbar indicator rectShape.fillColor = UIColor.redColor().CGColor indicatorWidth = view.bounds.maxX / 2 // count of items self.tabBarController!.view.layer.addSublayer(rectShape) self.tabBarController?.delegate = self // initial position updateTabbarIndicatorBySelectedTabIndex(0) } func updateTabbarIndicatorBySelectedTabIndex(index: Int) -> Void { let updatedBounds = CGRect( x: CGFloat(index) * (indicatorWidth + indicatorLeftMargin), y: view.bounds.maxY - indicatorHeight, width: indicatorWidth - indicatorLeftMargin, height: indicatorHeight) let path = CGPathCreateMutable() CGPathAddRect(path, nil, updatedBounds) rectShape.path = path } } extension FirstViewController: UITabBarControllerDelegate { func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) { updateTabbarIndicatorBySelectedTabIndex(tabBarController.selectedIndex) } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)