动画按钮允许用户交互不起作用 [英] animating button Allowuserinteraction not working
问题描述
我在NSObject类的代码中创建了一个UI按钮,它控制UIViewController类中的游戏。该按钮在游戏的大部分时间内都能正常工作,但在某个时刻,我希望按钮能够淡出/淡出。一旦淡入/淡出开始动画,按钮就不再是交互式的。我已经设置了.AllowUserInteraction选项,但仍然没有运气。我对此感到难过,所以任何帮助都非常感激!
I have a UIbutton created in code in an NSObject class which controls a game in a UIViewController class. The button works fine throughout most of the game, but at a certain point I want the button to fadein/out. Once the fadein/out starts animating the button is no longer interactive. I have set the .AllowUserInteraction option, but still no luck. I am stumped on this one, so any help much appreciated!
Class GameController: NSObject {
var gameView: UIView!
var nextButton: UIButton!
func gameViewSetUp() {
// create the nextButton
let imageNextButton = UIImage(named: "rightArrow") as UIImage?
self.nextButton = UIButton(type: .Custom)
nextButton.frame = CGRectMake(ScreenWidth-100,ScreenHeight-250,60,60)
nextButton.setTitle("", forState: UIControlState.Normal)
nextButton.setImage(imageNextButton, forState: .Normal)
nextButton.contentMode = .ScaleAspectFill
nextButton.backgroundColor = UIColor.clearColor()
nextButton.layer.cornerRadius = 5.0
nextButton.layer.borderWidth = 2.5
nextButton.layer.borderColor = UIColor.clearColor().CGColor
nextButton.addTarget(self, action: "nextPressed", forControlEvents: .TouchUpInside)
nextButton.userInteractionEnabled = true
gameView.addSubview(nextButton)
func playStageX() {
nextButtonWink()
}
}
func nextButtonWink() {
UIView.animateWithDuration(1.5, delay: 0, options: [.AllowUserInteraction, .Repeat, .CurveEaseInOut, .Autoreverse],
animations: {
// self.nextButton.userInteractionEnabled = true // I tried this as well but no impact.
self.nextButton.alpha = 0
}, completion: nil)
}
class GameViewController: UIViewController {
private var controller: GameController
required init?(coder aDecoder: NSCoder) {
controller = GameController()
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
let gameView = UIView(frame: CGRectMake(0, 0, ScreenWidth, ScreenHeight))
self.view.addSubview(gameView)
controller.gameView = gameView
}
推荐答案
取出 self.nextButton.alpha = 0
来测试它,我相信当alpha为零时按钮事件不会触发。当涉及到动画时,实际对象的alpha将在动画开始之前设置为零,并且您所看到的就像是动画的渲染交互式屏幕截图,因为它动画
Take out self.nextButton.alpha = 0
to test this out, I believe that button events will not fire when alpha is zero. When it comes to animations, the actual object's alpha will get set to zero before the animation starts, and what you are seeing is like a rendered interactive screenshot of your view as it animates
如果是这种情况,那么你需要将alpha设置为0.0100000003,或者将按钮覆盖为alpha 0的交互式
If that is the case, then you need to set the alpha to something like 0.0100000003 or override the button to be interactive at alpha 0
这篇关于动画按钮允许用户交互不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!