【发布时间】:2018-01-22 08:06:42
【问题描述】:
如何实现,当我单击按钮时,我的图像会以动画形式移出屏幕,当我再次单击按钮时,又会回到原来的位置?
【问题讨论】:
-
请显示一些代码,你尝试了什么?
-
将图像添加到 uiview 并使用块为 uiview 设置动画。您可以在应用中播放您想要的动画类型
标签: ios iphone swift animation swift2
如何实现,当我单击按钮时,我的图像会以动画形式移出屏幕,当我再次单击按钮时,又会回到原来的位置?
【问题讨论】:
标签: ios iphone swift animation swift2
在button 点击事件上,只需根据您的要求为imageView 的x coordinate of origin 设置动画即可。
示例:
@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
}
上面的代码将如下工作:
当selectingbutton,imageView'sx coordinate of origin移动到屏幕的extreme right时(UIScreen.main.bounds.width)
当de-selectingbutton,imageView'sx coordinate of origin移动到屏幕的extreme left时(0.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*/
})
}
【讨论】: