I'm trying to make an application where i have to touch a button
who is moving up the screen. I am using CADisplayLink
for 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