Showing label after UIPanGestureRecognizer

62 views Asked by At

I'm trying to show a label after the user dragged an image down. The problem is that the label won't show after. Everything else works fine but only the label doesn't work. I've tried to find a solution for a couple hours but I've no idea what I'm doing wrong.

Function that is called on UIPanGestureRecognizer:

func pullDownView(gesture: UIPanGestureRecognizer) {
    if(gesture.state == .changed) {
        if(iconImage.center.y - (iconImage.frame.size.height / 2) < view.frame.size.height / 3) {
            let translationY = gesture.translation(in: view).y
            iconImage.center.y += translationY

            gesture.setTranslation(CGPoint(x: 0, y: 0), in: view)
            iconImage.updateConstraints()
        }else{
            iconImage.isUserInteractionEnabled = false
            iconImage.center.y = (view.frame.size.height / 3) + (iconImage.frame.size.height / 2)
            iconImage.updateConstraints()
        }
    }

    if(gesture.state == .cancelled || gesture.state == .ended || gesture.state == .failed) {
        if(iconImage.center.y - (iconImage.frame.size.height / 2) >= view.frame.size.height / 6) {
            if(iconImage.center.y != (view.frame.size.height / 3) + (iconImage.frame.size.height / 2)) {
                iconImage.isUserInteractionEnabled = false
                iconImage.center.y = (view.frame.size.height / 3) + (iconImage.frame.size.height / 2)
                iconImage.updateConstraints()

                getAccounts()
            }
        }else{
            iconImage.center.y = view.frame.size.height / 2
            iconImage.updateConstraints()
        }
    }
}

func getAccounts() {
    noAccountsLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 50))
    noAccountsLabel.text = "No accounts found!"
    noAccountsLabel.font = UIFont(name: "Verdana", size: 20)
    noAccountsLabel.textAlignment = .center
    noAccountsLabel.center = CGPoint(x: view.frame.size.width / 2, y: noAccountsLabel.frame.size.height * 2)

    view.addSubview(noAccountsLabel)
}

Thanks for helping! Steff.

1

There are 1 answers

8
Fluidity On
func getAccounts() {
  let noAccountsLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 50))
  noAccountsLabel.text = "No accounts found!"
  noAccountsLabel.font = UIFont(name: "Verdana", size: 20)
  noAccountsLabel.textAlignment = .center
  noAccountsLabel.center = CGPoint(x: view.frame.size.width / 2, y: noAccountsLabel.frame.size.height * 2)

  view.addSubview(noAccountsLabel)
}

Worked for me.

PS: I may have done the logic wrong here, but you can use guard regardless to clear up some if statements:

guard gesture.state == .cancelled || gesture.state == .ended || gesture.state == .failed else  { return }

guard (iconImage.center.y - (iconImage.frame.size.height / 2) >= view.frame.size.height / 6) else { return }

if(iconImage.center.y != (view.frame.size.height / 3) + (iconImage.frame.size.height / 2)) {
  iconImage.isUserInteractionEnabled = false
  iconImage.center.y = (view.frame.size.height / 3) + (iconImage.frame.size.height / 2)
  iconImage.updateConstraints()

  getAccounts()
}