Reputation: 2562
I created a button and set some animations too it but now I can not tap it or interact with it while it is in motion and if it lets me tap it I get an error. My goal is to make the button disappear while it is moving when you click on it. Please help!! Thanks in advance!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Create button
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
UIView.animateWithDuration(4, delay: 3, options: UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.LayoutSubviews | UIViewAnimationOptions.Autoreverse, animations: {
button.frame = CGRectMake(100, 300, 100, 100)
}, completion: nil)
// Create a function that makes the button dissapear when it is tapped
func buttonAction() {
button.alpha = 0
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Upvotes: 0
Views: 1483
Reputation: 324
Perhaps you could track if your object is moving using a boolean variable.
Then, try using "hidden" a property of button:
var moving: Bool!
if moving == true {
button.hidden = true
} else {
button.hidden = false
}
Hope you can figure it out.
Upvotes: 0
Reputation: 104082
You can't use animateWithDuration to animate your button's position if you want to interact with it while it moves. The touch point of the button actually moves immediately to the final position (you can touch there when it's in motion, and you will see that it calls its action method). You need to use a timer (or a CADisplayLink) to incrementally move your button with each call.
Also, as others have pointed out, you need to change your button's action to "buttonAction:" so the button will send itself as the argument. You can use a CADisplayLink like so,
import UIKit
class ViewController: UIViewController {
var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
var displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
}
func handleDisplayLink(displayLink: CADisplayLink) {
var buttonFrame = button.frame
buttonFrame.origin.y += 1
button.frame = buttonFrame
if button.frame.origin.y >= 300 {
displayLink.invalidate()
}
}
func buttonAction(sender: UIButton) {
sender.alpha = 0
}
}
Upvotes: 0
Reputation: 2478
In order to fix crash you are having you need to move buttonAction func out of viewDidLoad scope and modify it like this:
func buttonAction(button: UIButton) {
button.alpha = 0
}
Also you need to change button.addTarget(...) param action from this "buttonAction" to this "buttonAction:". See code below
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
Upvotes: 2
Reputation: 33
You will have to create an outlet by 'ctrl + drag' your button from IB to your ViewController. Next you can create an IBaction() function.
@IBAction func likedThis(sender: UIButton) {
//your code
}
The code you put inside this function will automatically be triggered when you press the button.
Upvotes: 0