0

I have a background which is also a UIView, on which has 3 UIViews as its subviews. I added a UIPanGestureRecognizer to each of them, hoping when the user drags a view to the left, the corresponding UIView expands horizontally. Heres my code:

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

And in the layoutSubviews() call I have:

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

Pretty simple, but it somehow doesn't work and I couldn't figure out whats wrong. I've also tried self.view.setNeedsLayout() but no luck either. *The labelbg's are UIViews, not UILabels.

Thanks in advance!

Shruti Thombre
  • 989
  • 4
  • 11
  • 27
ddolce
  • 739
  • 2
  • 10
  • 30

1 Answers1

-1

You might forgot to add 'override' keyword at the front of layoutSubviews method

Lei Zhang
  • 634
  • 4
  • 6