0

i have a problem with my custom UIButton. When I add a target, there is no action performed when I click. I tried to move function into ViewController class, change selector name and type, in vain. There is my code. Hope you can help me. (Hierarchy) CustomButton → MenuButtonGroup → ViewController

import UIKit

class CustomButton: UIButton {

/** vue 1 */
private let view1 = UIImageView()

/** vue 2 */
private let view2 = UIView()

/** couleur du bouton */
private var color = ""

/** décalage y pour le 3d */
private let relief = screenHeight*0.013


init(frame: CGRect, color: String){
    super.init(frame: frame)


    self.color = color
    self.layer.cornerRadius = 10

    //définir la couleur de l'ombre
    switch color {
    case "red":
        self.backgroundColor = shadowRedColor
    case "green":
        self.backgroundColor = shadowGreenColor
    case "blue":
        self.backgroundColor = shadowBlueColor
    case "yellow":
        self.backgroundColor = shadowYellowColor
    default:
        self.backgroundColor = shadowRedColor
    }

    mainColorView()

self.addSubview(view2)

    self.addTarget(self, action: #selector(downAction), for: .touchDown)
    self.addTarget(self, action: #selector(touchUp), for: .touchUpInside)
    self.addTarget(self, action: #selector(touchUp), for: .touchUpOutside)

}

/** vue de couleur principale*/
func mainColorView(){

    view2.frame = CGRect(x: 0, y: -relief, width: self.frame.width, height: self.frame.height)
    view2.layer.cornerRadius = 10

    //définir la couleur de l'ombre
    switch color {
    case "red":
        view2.backgroundColor = redColor
    case "green":
        view2.backgroundColor = greenColor
    case "blue":
        view2.backgroundColor = blueColor
    case "yellow":
        view2.backgroundColor = yellowColor
    default:
        view2.backgroundColor = redColor
    }
}

/** appuis sur le bouton */
func downAction(sender: UIButton){
    print("prout")
    UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseOut, animations: {
        self.view2.transform = CGAffineTransform(translationX: 0, y: self.relief)
    })
}

/**relacher le bouton*/
func touchUp(){
    animUp()
}

/** animer le relachement du bouton*/
func animUp(){
    UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.3, options: .curveEaseInOut, animations: {
        self.view2.transform = CGAffineTransform.identity
    })
}


required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

Adding the button to MenuButtonGroup :

playButton = CustomButton(frame: CGRect(x: 0, y: 0, width: 300, height: 300), color: "green")

The button is showed on screen but no action at all. I tried bringSubviewToFront but with no effect.

2
  • Have you tried to addTarget to it? Commented Apr 12, 2017 at 7:21
  • Yes, it's in shared code. Commented Apr 12, 2017 at 7:42

3 Answers 3

2

When you are adding the so called view2 to your UIButton, your are setting it up to be the same size as the button with this:

view2.frame = CGRect(x: 0, y: -relief, width: self.frame.width, height: self.frame.height)

This means, that the view2 covers the whole button beneath and it swallows the touch events from your button. Do the following:

view2.isUserInteractionEnabled = false

And life will be all good.

EDIT:

After looking at your project, it is clear, that you are adding this button to a UIImageView as a subview. The problem is still kind of the same, by default, UIImageView isUserInteractionEnabled is set to false.

So, make sure, you set it to true in the initialiser of MenuButtonGroup.

self.isUserInteractionEnabled = true

So, disable user interaction for the UIView instance, and enable user interaction for the UIImageView subclass.

And life will be even better.

Sign up to request clarification or add additional context in comments.

4 Comments

I would like to say that it works, but even if I do not add view2 to my view and / or isUserInteractionEnabled = false, the action does not happen
Can you please try to download my project ? May be the error is in other file , there is juste 5 smalls files : (500ko) http : / / we.tl/rMCasf0f8e
When I add my CustomButton to ViewController view, it's work. But my CustomButton is in one view which is added to ViewController. I tried to add isUserInteractionEnabled to it, but still not work. But thanks we progress !
Thank you a lot ! I know it now. You save me ^^
0

You are assigning incorrect action.

Your action declaration is func downAction(sender: UIButton), however you are adding func downAction(). You need to add it like this:

self.addTarget(self, action: #selector(downAction(sender:)), for: .touchDown)

5 Comments

@MatthieuBravo probably view2 blocks the touches. Try to add action to it. view2.addTarget(self, action: #selector(downAction(sender:)), for: .touchDown).
Still no react. I can't see where is the problem.
@MatthieuBravo with correct action assigned it works. There is a problem with your layout - only top left quarter of the button responds to touches. Try tapping on the top left corner.
@MaxPevser With your answers and tapping on the top left corner, I can't see any action. view2 = UIButton, addTarget to view2, adding view2 to CustomButton.
@MaxPevser can you download my project (500ko) ? http : / / we.tl/rMCasf0f8e
0

Try to change this:

init(frame: CGRect, color: String) {
    super.init(frame: frame)

    self.color = color
    self.layer.cornerRadius = 10

    //définir la couleur de l'ombre
    switch color {
    case "red":
        self.backgroundColor = shadowRedColor
    case "green":
        self.backgroundColor = shadowGreenColor
    case "blue":
        self.backgroundColor = shadowBlueColor
    case "yellow":
        self.backgroundColor = shadowYellowColor
    default:
        self.backgroundColor = shadowRedColor
    }    

    mainColorView()

    self.addSubview(view2)

    self.addTarget(self, action: #selector(downAction), for: .touchDown)
    self.addTarget(self, action: #selector(touchUp), for: .touchUpInside)
    self.addTarget(self, action: #selector(touchUp), for: .touchUpOutside)
}

To this:

init(frame: CGRect, color: String) {
    super.init(frame: frame)

    self.color = color
    self.layer.cornerRadius = 10

    //définir la couleur de l'ombre
    switch color {
        case "red":
             self.backgroundColor = shadowRedColor
        case "green":
             self.backgroundColor = shadowGreenColor
        case "blue":
             self.backgroundColor = shadowBlueColor
        case "yellow":
             self.backgroundColor = shadowYellowColor
        default:
             self.backgroundColor = shadowRedColor
    }

    mainColorView()

    self.view2.addTarget(self, action: #selector(CustomButton.downAction), for: .touchDown)
    self.view2.addTarget(self, action: #selector(CustomButton.touchUp), for: .touchUpInside)
    self.view2.addTarget(self, action: #selector(CustomButton.touchUp), for: .touchUpOutside)

    self.addSubview(view2)
}

EDIT: Try do delete sender: UIButton from function definition:

func downAction() {
    print("prout")
    UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseOut, animations: {
        self.view2.transform = CGAffineTransform(translationX: 0, y: self.relief)
    })
}

EDIT2: Try to change view2 type from UIView() to UIButton()

8 Comments

@MatthieuBravo are u sure that u r adding subview after added targets now?
No change, still does not react.
Yes I'm sure, no action.
@MatthieuBravo check upate
Nothing change at all
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.