1

I am trying to get a UIButton to repeat the code over and over until the user releases the button. I have an up arrow in a game and when it is tapped, a space ship will go up until it is released. Here is the code for my button:

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

Any suggestions??

Nick P
  • 19
  • 2
  • possible duplicate of [Way to make a UIButton continuously fire during a press-and-hold situation?](http://stackoverflow.com/questions/903114/way-to-make-a-uibutton-continuously-fire-during-a-press-and-hold-situation) – Matt Gibson Dec 29 '14 at 17:08

1 Answers1

3

You need to implement your own solution for this, something along the lines

1- User touches down (UIControlEvent touchDown), you start a time that fires every x secs/millisecs

2- Timer will fire the action over and over

3- User touches up UICOntrolEvent touchUp, you cancel your timer

So you bind those 2 events to different functions, and start/kill your timers with appropriate actions

How that helps

Daniel

Daniel
  • 22,363
  • 9
  • 64
  • 71