【发布时间】: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