thumbtackthief
thumbtackthief

Reputation: 6211

Use buttons inside subview

I'm making a multiple choice quiz game, and my goal right now is to have four buttons that refresh by spinning around with new answer choices. I think that means I need a subview that animates and re-populates with new buttons--if that's incorrect or not best, please stop me here.

At any rate, I created the subview in my storyboard, put the buttons inside it (background is blue just to see it now):

enter image description here

I dragged that over to my ViewController to make an IBOutlet (buttonContainer) and added this code to my ViewDidLoad:

view.addSubview(buttonContainer)
let buttonTap = UITapGestureRecognizer(target:self, action: Selector("checkAnswer"))
buttonTap.numberOfTapsRequired = 1
buttonContainer.addGestureRecognizer(buttonTap)
buttonContainer.userInteractionEnabled = true

However: When I run it in the simulator, the blue background does not appear at all, but the buttons are still disabled.

Before creating the subview, both the buttons and the function (checkAnswer) they called all worked perfectly.

Upvotes: 1

Views: 315

Answers (1)

Allan Chau
Allan Chau

Reputation: 703

You don't need any of this code if you are creating everything in storyboard. Just create a new class for the containerview and connect the buttons as an outlet collection.

For example, your button container class might look something like this:

class ButtonContainerView: UIView {

    @IBOutlet var answerButtons: [UIButton]!

    func rotateButtons() {
        for button in answerButtons {
            var context = UIGraphicsGetCurrentContext()
            UIView.beginAnimations(nil, context: &context)
            UIView.setAnimationCurve(UIViewAnimationCurve.Linear)
            UIView.setAnimationDuration(5.0)
            button.transform = CGAffineTransformRotate(button.transform, CGFloat(M_PI))
            UIView.commitAnimations()
        }
    }
}

Upvotes: 1

Related Questions