Monday, May 11, 2026
HomeiOS Developmentios - Change lazy button opacity from outdoors of the category

ios – Change lazy button opacity from outdoors of the category

[ad_1]

I would like to alter alpha worth of the button in one other class. However the issue is button created as “lazy var” so I cannot change that worth.

lazy var middleButton: UIButton = {
    let button = UIButton(kind: .customized)
    button.body.dimension = CGSize(width: 56, peak: 56)
    button.layer.cornerRadius = button.body.width / 2
    button.layer.masksToBounds = true
    button.backgroundColor = .white
    button.setImage(UIImage(named: "iconBasket"), for: .regular)
    button.contentMode = .heart
    button.imageView?.contentMode = .scaleAspectFit
    button.imageEdgeInsets = UIEdgeInsets(high: 0, left: 0, backside: 0, proper: 0)
    button.addTarget(self, motion: #selector(btnBasketClicked))
    addSubview(button)
    return button
}()

I would like this button’s alpha as 0.2 when view is scrolling. Right here is the code

extension ProductListView: UIScrollViewDelegate{
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
        // alpha = 0.2
        MyTabBar.shared.scrollDown()

    }
    else{
         // alpha = 1.0
         MyTabBar.shared.scrollUp()
        }
    }

}

    func scrollDown(){
    middleButton.alpha = 0.2
}

I’ve tried plenty of manner however does not work. Calling “addSubView()” operate in “layoutSubviews()” remedy my downside however this causes one other downside which my operate “basketButtonClicked()” usually are not known as. I used Delegate sample and right here it’s.

protocol BasketButtonDelegate: class {
func basketButtonClicked()

}

@objc personal func btnBasketClicked(_ sender: UIButton!){
    
    delegate?.basketButtonClicked()
}

override func layoutSubviews() {
    tremendous.layoutSubviews()
    addSubview(middleButton)
}

Once I name “addSubView” operate in “layoutSubviews()”, “basketbuttonClicked” by no means known as.

extension TabCoordinator : BasketButtonDelegate
{
    func basketButtonClicked(){
        log.debug("basketButtonClicked")
        let coor = CartCoordinator(navigationController, hidesBar: true)
        childCoordinators.append(coor)
        coor.begin()
    }
}

(I assigned delegate so the issue is just not about it.)

A bit sophisticated however I hope we will determine it out.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments