TouchUpInside按钮不起作用

时间:2016-03-19 20:21:54

标签: ios swift uibutton

我的代码如下:

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

然后在signInButtonClicked:joinButtonClicked:方法中我有这个代码,我将UITextField设置为我想要的特定x和y位置:

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

出于某种原因,每当我点击signInButtonjoinButton时,文本字段的动画都不会发生。我在这里做错了吗?我将两个按钮的目标添加到自己,并为每个按钮设置正确的操作。

CustomButton类:

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

3 个答案:

答案 0 :(得分:2)

你的代码是(发言:应该)工作;什么是无效的是你在这些动作方法中设置动画位置的方式。

通过查看设置UITextFields和动画后位置的初始位置的方式,我发现您希望UITextFields最初在屏幕外显示然后,一旦点击按钮,它就会在屏幕上显示动画。

鉴于此,问题在于以下几行代码:

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)

记录这些数字,可以看到:

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

-512.500000 

这些功能确实有效,动画也是如此;只是你从最初离屏的位置动画UITextField,再到另一个屏幕外的动画。

通过简单地在动画块中将线条更改为以下内容:

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)

允许我们在点击按钮后看到UITextField进入屏幕。

答案 1 :(得分:1)

使用

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

而不是

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

答案 2 :(得分:0)

检查当前和父视图控制器:

  1. 触摸范围必须位于视图框中。
  2. view.userInteractionEnabled。它们必须是true