当点击按钮时,我怎样才能实现当我点击按钮时,我的图像用动画移出屏幕,当我再次点击按钮时,它会回到原来的位置,隐藏/显示带动画的uiimageview点击按钮
答
在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
被移动到屏幕(UIScreen.main.bounds.width
)- 的
extreme right
当
de-selecting
button
,imageView's
x coordinate of origin
移动到屏幕(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*/
})
}
请出示一些代码的
extreme left
,你尝试过什么? – 3stud1ant3使用块将图像添加到uiview并使uiview动画。你可以玩你想要的应用程序中的动画 – cole