[英]Move UIImage horizontally with animation
我想從左到右和從右到左水平移動UIImageView
。 我設計如下。
編輯 1
感謝您的回答
if sender.isSelected {
UIView.animate(withDuration: 1.0, animations: {
self.imgVWInOut.frame.origin.x = (self.vwPunchInOut.bounds.maxX - self.imgVWInOut.bounds.width) - 10
}) { (done) in
}
} else {
UIView.animate(withDuration: 1.0, animations: {
self.imgVWInOut.frame.origin.x = 10
}) { (done) in
}
}
但是當我嘗試更改UIView
背景顏色和UIImageView
圖像時,動畫無法正常工作。
@IBAction func btnPunchInOutTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
UIView.animate(withDuration: 1.0, animations: {
self.imgVWInOut.frame.origin.x = (self.vwPunchInOut.bounds.maxX - self.imgVWInOut.bounds.width) - 10
}) { (done) in
self.imgVWInOut.image = #imageLiteral(resourceName: "ic_punchout")
self.lblPunchInOut.text = "Punch out".localized()
self.vwPunchInOut.backgroundColor = Colors.punchOutColor
}
} else {
UIView.animate(withDuration: 1.0, animations: {
self.imgVWInOut.frame.origin.x = 10
}) { (done) in
self.imgVWInOut.image = #imageLiteral(resourceName: "ic_punchout")
self.lblPunchInOut.text = "Punch out".localized()
self.vwPunchInOut.backgroundColor = Colors.punchOutColor
}
}
}
要求
默認看起來像這樣。 在此處輸入圖片說明
當用戶按下按鈕時,它會在動畫完成后看起來像這樣。
GIF 鏈接: https : //drive.google.com/file/d/1R2hfcwfhyO5JA9CQt1_6Auto3YBRj3yn/view?usp=sharing
演示項目鏈接: https : //drive.google.com/file/d/1H0b3D61fPIxWqSZL8tdvp0RP8juVdr_Z/view?usp=sharing
我怎樣才能做到這一點。 你能幫我嗎。 謝謝
您需要將arrowImageView
wrt 的frame
設置為customView
(綠色視圖),即
@IBAction func onTapButton(_ sender: UIButton) {
self.arrowImageView.frame.origin.x = self.customView.bounds.minX
UIView.animate(withDuration: 1.0) {
self.arrowImageView.frame.origin.x = self.customView.bounds.maxX - self.arrowImageView.bounds.width
}
}
根據您的要求提供動畫duration
。
編輯:
您需要在按鈕點擊時更改sender
的isSelected
狀態,
@IBAction func onTapButton(_ sender: UIButton) {
sender.isSelected = !sender.isSelected //here...
//rest of the code...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.