1

I'm trying to find the index of the selected button within my stack view. Currently, I'm trying to do this by using tags on my buttons. The end goal is for when the long press gesture is stopped whatever value the stack view is on is selected but currently, my case switch is printing out all the values, not the selected one. I'm using this instead of a collectionView or tableView because I find it much easier to implement animations.

Button Setup

let likeButton = UIButton(type: .system)
    likeButton.setImage(#imageLiteral(resourceName: "blue_like").withRenderingMode(.alwaysOriginal), for: .normal)
    likeButton.tag = 0
    let heartButton = UIButton(type: .system)
    heartButton.setImage(#imageLiteral(resourceName: "red_heart").withRenderingMode(.alwaysOriginal), for: .normal)
    heartButton.tag = 1
    let wowButton = UIButton(type: .system)
    wowButton.setImage(#imageLiteral(resourceName: "surprised").withRenderingMode(.alwaysOriginal), for: .normal)
    wowButton.tag = 2
    let laughButton = UIButton(type: .system)
    laughButton.setImage(#imageLiteral(resourceName: "cry_laugh").withRenderingMode(.alwaysOriginal), for: .normal)
    laughButton.tag = 3
    let sadButton = UIButton(type: .system)
    sadButton.setImage(#imageLiteral(resourceName: "cry").withRenderingMode(.alwaysOriginal), for: .normal)
    sadButton.tag = 4
    let angryButton = UIButton(type: .system)
    angryButton.setImage(#imageLiteral(resourceName: "angry").withRenderingMode(.alwaysOriginal), for: .normal)
    angryButton.tag = 5
    let buttons = [likeButton, heartButton, wowButton, laughButton, sadButton, angryButton]

Long press gestured ended

else if gesture.state == .ended {

        // clean up the animation
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            let stackView = self.iconsContainerView.subviews.first
            stackView?.subviews.forEach({ (button) in
                button.transform = .identity
                switch button.tag {
                case 0:
                    print("like button")
                case 1:
                    print("Heart")
                case 2:
                    print("Wow")
                case 3:
                    print("Laugh")
                case 4:
                    print("Sad")
                case 5:
                    print("Angry")
                default:
                    fatalError("Oops, this should not happen")
                }
            })

            self.iconsContainerView.transform = self.iconsContainerView.transform.translatedBy(x: 0, y: 50)
            self.iconsContainerView.alpha = 0


        }, completion: { (_) in
            self.iconsContainerView.removeFromSuperview()
        })


    }
user
  • 345
  • 2
  • 8
  • 32

1 Answers1

0

Your best bet is probably to attach a long press gesture to each button and check gesture.view in the callback.

See: UIButton Long Press Event

Joe
  • 3,664
  • 25
  • 27