在 3 个位置之间平滑动画 swift 3

smooth animate between 3 locations swift 3

我还是 swift 和 ios

我正在构建我的大学项目,但我遇到了一些动画问题,首先这是我的代码:

    @IBAction func tab1(_ sender: Any) {
 
    UIView.animate(withDuration: 0.5, animations:{
        self.slider.center.x -= self.tab1.bounds.width
    })
}

@IBAction func tab2(_ sender: Any) {
    
    
    UIView.animate(withDuration: 0.5, animations:{
        self.slider.center.x += self.tab2.bounds.width
    })
}
@IBAction func tab3(_ sender: Any) {
       
    UIView.animate(withDuration: 0.5, animations:{
        self.slider.center.x += self.tab3.bounds.width
    })
}

你可以看到它有多基本,并且有很多问题,比如如果在 tab1 中的滑块并单击 tab 3 滑块将移动一步(到 tab2)而不是用户需要再按一次,+ 如果你在tab2 并再次按下 tab2 滑块再次移动,我的代码中有很多错误,我搜索解决方案但没有运气。 我想将它调整为动画以正确的位置并验证它,这意味着如果滑块在正确的位置滑块将不会移动。 我希望能从 swift 开发人员

那里得到一些帮助

你可以直接用按钮框来计算x:

let tab = sender as! UIView
UIView.animate(withDuration: 0.5, animations:{
    self.slider.center.x = tab.frame.midX
})