[英]hide/show uiimageview with animation when clicking button
当我单击按钮时,我的图像将随着动画移出屏幕,而当我再次单击该按钮时,该如何恢复呢?
在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
}
上面的代码将按以下方式工作:
selecting
button
, imageView's
x coordinate of origin
移动到屏幕的extreme right
( UIScreen.main.bounds.width
)
de-selecting
button
, imageView's
x 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*/
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.