【问题标题】:animation bar button for UISegmentedControl is not moving?UISegmentedControl 的动画栏按钮没有移动?
【发布时间】:2019-03-17 06:50:32
【问题描述】:

我在 Swift 中设计了动画按钮栏 UISegmentedControl “showUsers”,当我尝试实现动画时,该栏仍然保持第一个段而不移动到第二个段 例如https://www.codementor.io/kevinfarst/designing-a-button-bar-style-uisegmentedcontrol-in-swift-cg6cf0dok

override public func viewDidLoad(){
    super.viewDidLoad()

    showUsers.backgroundColor = .clear
    showUsers.tintColor = .clear

    showUsers.setTitleTextAttributes([
        NSAttributedString.Key.font : UIFont(name: "DINCondensed-Bold", size: 18),
        NSAttributedString.Key.foregroundColor: UIColor(red:0.44, green:0.44, blue:0.44, alpha:1.0)
        ], for: .normal)

    showUsers.setTitleTextAttributes([
        NSAttributedString.Key.font : UIFont(name: "DINCondensed-Bold", size: 18),
        NSAttributedString.Key.foregroundColor: UIColor(red:0.44, green:0.44, blue:0.44, alpha:1.0)
        ], for: .selected)

    let buttonBar = UIView()
    // This needs to be false since we are using auto layout constraints
    buttonBar.translatesAutoresizingMaskIntoConstraints = false
    buttonBar.backgroundColor = UIColor(red:0.44, green:0.44, blue:0.44, alpha:1.0)

    view.addSubview(showUsers)
    view.addSubview(buttonBar)

    // Constrain the top of the button bar to the bottom of the segmented control
    buttonBar.topAnchor.constraint(equalTo: showUsers.bottomAnchor).isActive = true
    buttonBar.heightAnchor.constraint(equalToConstant: 5).isActive = true
    // Constrain the button bar to the left side of the segmented control
    buttonBar.leftAnchor.constraint(equalTo: showUsers.leftAnchor).isActive = true
    // Constrain the button bar to the width of the segmented control divided by the number of segments
    buttonBar.widthAnchor.constraint(equalTo: showUsers.widthAnchor, multiplier: 1 / CGFloat(showUsers.numberOfSegments)).isActive = true

    showUsers.addTarget(self, action: #selector(HomeViewController.segmentedControlValueChanged(_:)),  for: UIControl.Event.valueChanged)
}


@objc func segmentedControlValueChanged(_ sender: UISegmentedControl) {
    UIView.animate(withDuration: 0.3) {
        print(self.showUsers.frame.width) //147.0
        print(self.showUsers.numberOfSegments) //2
        print(self.showUsers.selectedSegmentIndex)//1

        //let originX = (self.showUsers.frame.width / CGFloat(self.showUsers.numberOfSegments)) * CGFloat(self.showUsers.selectedSegmentIndex + 1)

        let originX = (self.showUsers.frame.width / CGFloat(self.showUsers.numberOfSegments)) * CGFloat(self.showUsers.selectedSegmentIndex + 1)
        self.buttonBar.frame.origin.x = originX
    }
}

更新: 更新计算函数,它移动了线,但不是我想要的

【问题讨论】:

  • 我没有看到任何关于 showUsers 视图的限制。能给我看看吗?
  • 让 originX = (self.showUsers.frame.width / CGFloat(self.showUsers.numberOfSegments)) * CGFloat(self.showUsers.selectedSegmentIndex + 1)
  • 它成功了,但我的计算出了点问题,我会用图片更新帖子

标签: ios swift iphone animation uisegmentedcontrol


【解决方案1】:

您的问题是您正在对具有约束的视图应用“框架”更改,因此请尝试在 segmentedControlValueChanged 方法中使用约束而不是这个

let originX = (self.showUsers.frame.width / CGFloat(self.showUsers.numberOfSegments)) * CGFloat(self.showUsers.selectedSegmentIndex + 1)
self.buttonBar.frame.origin.x = originX

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多