swift 有一排按钮,点击按钮时,被点击的按钮如何向中间的位置移动
@objc private func scrollCategoryBtnAction(sender: UIButton) {
if let btn = selectedScrollCategoryBtn { btn.selected = false }
sender.selected = !sender.selected
selectedScrollCategoryBtn = sender
/// 重设分类标签状态
if sender.center.x < bounds.width * 0.5 {
scrollView.setContentOffset(CGPointZero, animated: true)
} else if scrollView.contentSize.width > bounds.width && sender.center.x > bounds.width * 0.5 && sender.center.x < (scrollView.contentSize.width - bounds.width * 0.5) {
scrollView.setContentOffset(CGPointMake(sender.frame.origin.x + sender.bounds.width * 0.5 - bounds.width * 0.5, 0), animated: true)
} else {
scrollView.setContentOffset(CGPointMake(max(scrollView.contentSize.width - scrollView.bounds.size.width, 0), 0), animated: true)
}
/// 重新调用layoutSubviews
setNeedsLayout()