我有一个background
which 也是一个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()
,但也没有运气。*labelbg
是UIView
s,不是UILabel
s。
提前致谢!