【问题标题】:Tap and Hold UIButton in Swift?在 Swift 中点击并按住 UIButton?
【发布时间】:2014-12-29 16:59:48
【问题描述】:

我试图让 UIButton 一遍又一遍地重复代码,直到用户释放按钮。我在游戏中有一个向上箭头,当它被点击时,一艘太空船会上升,直到它被释放。这是我的按钮的代码:

    //at top of page
    let upArrow = UIButton()

    //in viewdidload()
         let upArrowImage = UIImage(named: "upArrow") as UIImage?
                upArrow.setImage(upArrowImage, forState: .Normal)
                upArrow.frame = CGRectMake(10, 220, 90, 40)
                upArrow.addTarget(self, action: "upArrowTouched:", forControlEvents: UIControlEvents.TouchUpInside)
                self.view?.addSubview(upArrow) 

//outside of viewDidLoad()
func upArrowTouched(sender:UIButton!){
            spaceship.position = CGPointMake(spaceship.position.x, spaceship.position.y + 3)
    }

有什么建议吗??

【问题讨论】:

标签: ios swift uibutton


【解决方案1】:

您需要为此实施自己的解决方案,类似于

1- 用户触地(UIControlEvent touchDown),您开始一个每 x 秒/毫秒触发一次的时间

2- 计时器将一遍又一遍地触发动作

3- 用户触摸 UIControlEvent touchUp,你取消你的计时器

因此,您将这两个事件绑定到不同的函数,并通过适当的操作启动/终止您的计时器

这有什么帮助

丹尼尔

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 2019-04-20
    相关资源
    最近更新 更多