bon*_*ton 14 uibutton ios swift
我是iOS开发的新手,我使用Swift作为我的首选语言.我正在为我的儿子开发一个简单的小程序,在UIButtons上显示动物图像,当他选择正确的按钮时,所有按钮都会加载新图像,并且他被要求找到不同的动物.
程序的基础知识已经启动并且正在运行,他可以玩它但是现在我想继续让它变得更加漂亮.当选择了正确的图像并且是时候用新的按钮图像替换按钮图像了:
btnItem.setImage(UIImage(named: arrItems[intItemCounter]), forState: .Normal)
Run Code Online (Sandbox Code Playgroud)
我不想简单地将按钮的图像从一个图像更改为另一个图像,而是希望通过动画过渡进行更改,但我无法弄清楚如何.
我已经发现很多与UIViews上的过渡有关,并且与永久动画按钮图像有关,但没有任何过渡,例如溶解或从左到右滑动等等.
关于SO的这个问题与我的几乎相同,但答案使用了Objective-C.
所以,长话短说,有没有办法在Swift中将UIButton图像从一个转换到另一个?
感谢您提前的时间.
bey*_*ulf 39
使用Swift 4.0更新以下内容
UIView.transition(with: sender as! UIView, duration: 1.5, options: .transitionFlipFromRight, animations: {
sender.setImage(UIImage(named: arrItems[intItemCounter]), for: .normal)
}, completion: nil)
Run Code Online (Sandbox Code Playgroud)
UIButton是UIView的子类,因此您可以将UIView.animateWithDuration与按钮一起使用.例如,你可以这样说:
@IBAction func buttonPressed(sender: UIButton) {
UIView.transitionWithView(sender, duration: 1.5, options: .TransitionFlipFromRight, animations: {
sender.setImage(UIImage(named: arrItems[intItemCounter]), forState: .Normal)
}, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
在点击时,这将导致按钮翻转新图像.
如果你想要其他例子中的动画(淡出/淡出),你可以说:
@IBAction func buttonPressed(sender: UIButton) {
UIView.animateWithDuration(0.5, animations: {
sender.alpha = 0.0
}, completion:{(finished) in
sender.setImage(UIImage(named: arrItems[intItemCounter]), forState: .Normal)
UIView.animateWithDuration(0.5,animations:{
sender.alpha = 1.0
},completion:nil)
})
}
Run Code Online (Sandbox Code Playgroud)
你也可以像这样做一个交叉溶解:
@IBAction func buttonPressed(sender: UIButton) {
UIView.transitionWithView(sender, duration: 1.5, options: .TransitionCrossDissolve, animations: {
sender.setImage(UIImage(named: arrItems[intItemCounter]), forState: .Normal)
}, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9700 次 |
最近记录: |