Skip to main content
 首页 » 操作系统

ios之在 Swift 中点击并按住 UIButton

2024年01月01日42wuhuacong

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

    //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) 
    } 

有什么建议吗??

请您参考如下方法:

你需要为此实现你自己的解决方案,类似的东西

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

2- 计时器会一遍又一遍地触发 Action

3- 用户触摸了 UICOntrolEvent touchUp,你取消了你的定时器

所以你将这两个事件绑定(bind)到不同的函数,并通过适当的操作启动/终止你的计时器

这有什么帮助

丹尼尔