0

I'm trying to make an application where i have to touch a button who is moving up the screen. I am using CADisplayLinkfor this. The problem in my code is that its creating a new button instead of using a specific one:

@IBOutlet var label: UILabel!
@IBOutlet var button2: UIButton!
@IBAction func button1(sender: UIButton) {
    label.hidden = true
    button2 = UIButton(type: UIButtonType.System) as UIButton
    button2.frame = CGRectMake(120, 400, 100, 100)
    button2.setTitle("Test Button", forState: UIControlState.Normal)
    button2.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(button)
    let displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
    displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
}
func handleDisplayLink(displayLink: CADisplayLink) {
    var buttonFrame = button.frame
    buttonFrame.origin.y += -2
    button2.frame = buttonFrame
    if button2.frame.origin.y <= 50 {
        displayLink.invalidate()   
    }
}
func buttonAction(sender: UIButton) {
    sender.alpha = 0
    label.hidden = false
}
override func viewDidLoad() {
    super.viewDidLoad()
  label.hidden = true
}

Thank you. Anton

Anton O.
  • 653
  • 7
  • 27

1 Answers1

0

You can delete

button2 = UIButton(type: UIButtonType.System) as UIButton
button2.frame = CGRectMake(120, 400, 100, 100)
button2.setTitle("Test Button", forState: UIControlState.Normal)

And just set these attributes in Interface Builder.

Also this line:

 self.view.addSubview(button)

No longer makes sense after you edited your question, but it is also not necessary if your buttons are IBOutlets.

This line:

var buttonFrame = button.frame

Only makes sense if button is an IBOutlet declared elsewhere.

beyowulf
  • 15,101
  • 2
  • 34
  • 40