【问题标题】:animating button Allowuserinteraction not working动画按钮 Allowuserinteraction 不起作用
【发布时间】:2016-03-21 15:26:58
【问题描述】:

我在 NSObject 类的代码中创建了一个 UIbutton,它控制 UIViewController 类中的游戏。该按钮在整个游戏的大部分时间都可以正常工作,但在某些时候我希望该按钮淡入/淡出。一旦淡入/淡出开始动画按钮不再是交互式的。我已经设置了 .AllowUserInteraction 选项,但仍然没有运气。我被这个难住了,所以非常感谢任何帮助!

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

}

【问题讨论】:

    标签: ios swift animation uibutton


    【解决方案1】:

    Swift 3 Xcode 8:

    AllowUserAction 现在被弃用为 allowUserAction.... 看图

    func nextButtonWink() {
    UIView.animateWithDuration(1.5, delay: 0, options: [.allowUserInteraction,
        animations: { 
    

    【讨论】:

      【解决方案2】:

      拿出self.nextButton.alpha = 0来测试一下,我相信当alpha为零时按钮事件不会触发。当涉及到动画时,实际对象的 alpha 将在动画开始之前设置为零,您所看到的就像动画时视图的渲染交互式屏幕截图

      如果是这种情况,那么您需要将 alpha 设置为 0.0100000003 或覆盖按钮以在 alpha 0 处进行交互

      【讨论】:

      • @Knight0fDragon 很好的答案,但是将 alpha 设置为 0.0..1 不会使按钮再次交互。你还有什么建议吗?我不知道如何覆盖 UIBUtton
      • @theAlse,我发现的最小允许 alpha 是 0.0100000003
      • @Knight0fDragon 所以将 alpha 设置为 0.01 会使按钮再次交互?
      • @theAlse,不,这比我给你的数字要少
      • @Knight0fDragon 我没有得到神奇的数字 0.0100000003,但会试一试
      猜你喜欢
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      相关资源
      最近更新 更多