当我点击按钮时,我的图像是如何实现的,我的图像会随着动画移出屏幕,当我再次点击按钮时,会回到原位?
答案 0 :(得分:1)
在button
点击事件中,只需根据您的要求为x coordinate of origin
的{{1}}设置动画即可。
示例:强>
imageView
以上代码的工作原理如下:
当@IBAction func onTapButton(_ sender: UIButton)
{
if sender.isSelected
{
UIView.animate(withDuration: 2.0, animations: {
self.imageView.frame.origin.x = 0.0
})
}
else
{
UIView.animate(withDuration: 2.0, animations: {
self.imageView.frame.origin.x = UIScreen.main.bounds.width
})
}
sender.isSelected = !sender.isSelected
}
selecting
,button
imageView's
移至x coordinate of origin
的{{1}}时
当extreme right
UIScreen.main.bounds.width
,de-selecting
button
移至imageView's
的{{1}}时
答案 1 :(得分:0)
使用此代码向左移动 -
func moveToLeft()
{
var frame = imageView.frame
frame.origin.x = -imageView.frame.size.width //Adjust this value according to your need
UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut, animations: {() -> Void in
self.imageView.frame = frame
}, completion: {(_ finished: Bool) -> Void in
/*done*/
})
}
使用此代码向右移动 -
func moveToRight()
{
var frame = imageView.frame
frame.origin.x = 0 //Adjust this value according to your need
UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut, animations: {() -> Void in
self.imageView.frame = frame
}, completion: {(_ finished: Bool) -> Void in
/*done*/
})
}