0

我有一个backgroundwhich 也是一个UIView,它有 3UIViews作为它的子视图。我UIPanGestureRecognizer给它们每个都添加了一个,希望当用户向左拖动视图时,相应的UIView水平扩展。这是我的代码:

func labelPanned(sender:UIPanGestureRecognizer!){
    self.labelBeingMoved = sender.view.tag
    if(sender.state == UIGestureRecognizerState.Began || sender.state == UIGestureRecognizerState.Changed){
        var translation:CGPoint = sender.translationInView(self.background)
        self.labelCurrWidth += translation.x
        NSLog("self.labelCurrWidth is now %f", self.labelCurrWidth)
        if(self.labelCurrWidth <= self.labelOriginalWidth || self.labelCurrWidth >= self.labelMaxWidth){self.labelCurrWidth -= translation.x}
        sender.setTranslation(CGPointZero, inView: self.background)
        dispatch_async(dispatch_get_main_queue(), {
            self.background.setNeedsLayout()
            })
    }
}

layoutSubviews()通话中我有:

func layoutSubviews(){
    NSLog("layoutSubviews called")
    switch self.labelBeingMoved{
    case 1:
        NSLog("CASE 1")
        self.labelbg.frame = CGRectMake(self.labelbg.frame.origin.x, self.labelbg.frame.origin.y, self.labelCurrWidth, self.labelbg.frame.height)
    case 2:
        NSLog("CASE 2")
        self.label1bg.frame = CGRectMake(self.label1bg.frame.origin.x, self.label1bg.frame.origin.y, self.labelCurrWidth, self.label1bg.frame.height)
    case 3:
        NSLog("CASE 3")
        self.label2bg.frame = CGRectMake(self.label2bg.frame.origin.x, self.label2bg.frame.origin.y, self.labelCurrWidth, self.label2bg.frame.height)
    default:
        NSLog("An unknown error occurred")
    }
}

很简单,但它不知何故不起作用,我不知道出了什么问题。我也尝试过self.view.setNeedsLayout(),但也没有运气。*labelbgUIViews,不是UILabels。

提前致谢!

4

1 回答 1

-1

您可能忘记在 layoutSubviews 方法的前面添加“覆盖”关键字

于 2015-12-16T10:10:40.153 回答