按钮动画中的Allowuserinteraction不起作用

8

我在一个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

}

2个回答

11

为了测试这一点,请取消 self.nextButton.alpha = 0。我认为当 alpha 值为零时,按钮事件将不会被触发。对于动画效果,实际对象的 alpha 值将在动画开始之前设置为零,你所看到的就像是一个渲染交互式屏幕截图的视图动画。

如果事实如此,那么你需要将 alpha 值设置为类似于 0.0100000003 的值,或者重写按钮以在 alpha 为 0 时仍然具有交互性。


您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - theAlse
1
@theAlse,我找到的最小允许 alpha i 是0.0100000003。 - Knight0fDragon
@theAlse 我写了一个演示应用程序,我发现比那个数字小的任何东西都不起作用。 - Knight0fDragon
如果您发现低 alpha 值仍然存在轻微混合问题,您可以使用 UIImageView 来显示按钮的图形,并使按钮本身具有 .clear 的背景颜色,同时将按钮的 alpha 设置为 1。 - Knight0fDragon
任何严格大于0.1的值都将使按钮再次变得可交互。@Knight0fDragon似乎发现0.0100000003是最低的CGFloat值。 - Simon Pickup
显示剩余3条评论

3

Swift 3 Xcode 8:

AllowUserAction已被弃用,改为allowUserAction.... 想一想都觉得离奇。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接