【问题标题】:Showing label after UIPanGestureRecognizer在 UIPanGestureRecognizer 之后显示标签
【发布时间】:2016-12-19 14:26:20
【问题描述】:

我试图在用户向下拖动图像后显示标签。问题是标签不会在之后显示。其他一切正常,但只有标签不起作用。我试图找到解决方案几个小时,但我不知道我做错了什么。

在 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)
}

感谢您的帮助! 斯特夫。

【问题讨论】:

  • 好吧,只是想确保明显..文本颜色与背景不一样是吗?
  • 并且您确定该块正在被调用,具有getAccounts的块?
  • 是的,我尝试打印一个字符串,它工作正常。而且文字颜色不一样,试过改了还是不行。
  • noAccountsLabel 是在哪里创建的?那是 IBOutlet 吗?
  • 只是检查显而易见的...... (1) 你使用的是自动布局吗?我只看到标签的框架坐标。 (2) 说到这个,试着设置你声明的 UILabel() 的框架,而不是初始化另一个 UILabel。这种语法对我来说是错误的。

标签: swift uipangesturerecognizer


【解决方案1】:
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)
}

为我工作。

PS:我可能在这里做错了逻辑,但是您可以使用guard来清除一些if语句:

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()
}

【讨论】:

  • 我找到了一个显示标签的解决方案,但现在 iconImage 跳回到原来的位置,你知道我该如何解决这个问题吗?
  • 相当愚蠢,但我忘记了我已经在用户默认值中有“帐户”,这就是标签没有显示的原因。但是当标签显示时,iconImage 会跳回原来的位置。
  • 尝试将getAccounts 放在块中的图标图像代码上方
  • 还是不行,我觉得是有iconImage的约束,可以吗?
  • 我没有在约束方面做太多工作,但如果在屏幕上添加新内容时它会跳来跳去,这似乎就是问题所在。您可能需要在这里发布一个新问题 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
相关资源
最近更新 更多