user5041486
user5041486

Reputation:

TouchUpInside Button Not Working

I have this code below:

override func viewDidLoad() {
        super.viewDidLoad()
        ...
        signInButton = CustomButton(frame: CGRectMake(12.5/100.0 * self.view.bounds.size.width, 5/6.0 * self.view.bounds.size.height, 60, 23))
        signInButton.customizeButtonText("Sign in")

        joinButton = CustomButton(frame: CGRectMake((75/100.0 * self.view.bounds.size.width), 5/6.0 * self.view.bounds.size.height, 60, 23))
        joinButton.customizeButtonText("Join")

        userNameTextField = CustomTextField(frame: CGRectMake(-500, 60/100.0 * self.view.bounds.size.height, 400, 35))
        userNameTextField.placeholder = "Username"

        passwordTextField = CustomTextField(frame: CGRectMake(-500, 70/100.0 * self.view.bounds.size.height, 400, 35))
        passwordTextField.placeholder = "Password"

        signInButton.addTarget(self, action: "signInButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
        joinButton.addTarget(self, action: "joinButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
}

Then in the signInButtonClicked: and joinButtonClicked: method I have this code where I animate the UITextField to the specific x and y position I want:

func signInButtonClicked(sender: UIButton!) {
        UIView.animateWithDuration(0.25, delay: 0.25, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            self.userNameTextField.frame = CGRectMake((-500 + ((50/100.0 * self.view.bounds.size.width) - 200)), 60/100.0 * self.view.bounds.size.height, 400,35)
            }, completion: nil)
    }

    func joinButtonClicked(sender: UIButton!) {
        UIView.animateWithDuration(0.25, delay: 0.25, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            self.passwordTextField.frame = CGRectMake((-500 + ((50/100.0 * self.view.bounds.size.width) - 200)), 70/100.0 * self.view.bounds.size.height, 400,35)
            }, completion: nil)
    }

For some reason whenever I click on signInButton or the joinButton the animation of the textfields do not happen. Am I doing something wrong here? I added the target for both of the buttons to self and set the correct action for each of the buttons.

CustomButton Class:

import UIKit

class CustomButton: UIButton {

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.cornerRadius = 2.0
        self.layer.shadowColor = UIColor(red: SHAWDOW_COLOR, green: SHAWDOW_COLOR, blue: SHAWDOW_COLOR, alpha: 0.5).CGColor
        self.layer.shadowRadius = 5.0
        self.layer.shadowOpacity = 1
        self.layer.shadowOffset = CGSizeMake(0.0, 2.0)
        self.titleLabel?.font = UIFont(name: "NotoSans-Regular", size: 17.0)
        self.backgroundColor = UIColor.redColor()
        self.userInteractionEnabled = true
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }

    func customizeButtonText(text: String!) {
        self.setTitle(text, forState: UIControlState.Normal)
    }
}

Upvotes: 0

Views: 2204

Answers (3)

Unheilig
Unheilig

Reputation: 16292

Your code is (speak: should be) working; what is not working is the way you set up the animation location within those action methods.

By looking at the way you set up the initial positions of the UITextFields and the post-animation position, I came to the understanding that you would like the UITextFields to be initially off-screen and would then animate on-screen once the buttons are clicked.

Given that, the problem lies in the following lines of code:

self.userNameTextField.frame = CGRectMake((((-500 + 50/100.0 * self.view.bounds.size.width) - 200)), 60/100.0 * self.view.bounds.size.height, 400, 35)

self.passwordTextField.frame = CGRectMake((((-500 + 50/100.0 * self.view.bounds.size.width) - 200)), 70/100.0 * self.view.bounds.size.height, 400,35)

Logging these numbers, one would see that:

NSLog("%f", (-500 + ((50/100.0 * self.view.bounds.size.width) - 200)))

-512.500000 

The functions do work and so do the animations; it is just that you are animating the UITextField from a position that was initially off-screen to, well, again another that is off-screen.

By simply changing the lines to the following within the animation blocks, respectively:

self.userNameTextField.frame = CGRectMake((((50/100.0 * self.view.bounds.size.width) - 200)), 60/100.0 * self.view.bounds.size.height, 400, 35)

self.passwordTextField.frame = CGRectMake((((50/100.0 * self.view.bounds.size.width) - 200)), 70/100.0 * self.view.bounds.size.height, 400,35)

allows us to see that the UITextField do come into the screen, once the buttons are clicked.

Upvotes: 1

dimpiax
dimpiax

Reputation: 12667

Check current and parent view controllers on:

  1. Touch bounds must be in view frames.
  2. view.userInteractionEnabled. They must be true.

Upvotes: 0

Jelly
Jelly

Reputation: 4522

Use

joinButton.addTarget(self, action: Selector("joinButtonClicked:"), forControlEvents: UIControlEvents.TouchUpInside)` 

instead of

joinButton.addTarget(self, action: "joinButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside)

Upvotes: 0

Related Questions