我有background
也是UIView
,其上有3 UIViews
个子视图。
我为每个人添加了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。
提前致谢!
答案 0 :(得分:-1)
您可能忘记添加&#39;覆盖&#39; layoutSubviews方法前面的关键字