Swift2 为什么要调用两次LayoutSubview?
我有一个子类UIView并重写layoutSubviews方法。 当我在viewController中调用这个子类并将其添加到控制器的视图中时,我发现“layoutSubviews”函数已经被调用了两次Swift2 为什么要调用两次LayoutSubview?,swift2,layoutsubviews,Swift2,Layoutsubviews,我有一个子类UIView并重写layoutSubviews方法。 当我在viewController中调用这个子类并将其添加到控制器的视图中时,我发现“layoutSubviews”函数已经被调用了两次 coverFlowView = CoverFlowView(frame: CGRectMake(0, 40, UIScreen.mainScreen().bounds.size.width, 480)) coverFlowView.delegate = self coverFlowView.
coverFlowView = CoverFlowView(frame: CGRectMake(0, 40, UIScreen.mainScreen().bounds.size.width, 480))
coverFlowView.delegate = self
coverFlowView.dataSource = self
self.view.addSubview(coverFlowView)
CoverFlowView是一个视图子类UIView,我遇到了相同的问题。我尝试了不同的操作系统,对相同的代码标志得到了不同的结果: 一个设备只调用layoutSubViews一次,但另一个设备调用了两次,导致我的代码出现一个bug 我的代码:
CGFloat step = 0.0;
if (self.array_rateOfEachColor) {
CGFloat sumRate = 0.0;
for (NSString *rate in self.array_rateOfEachColor) {
sumRate +=[rate floatValue];
}
NSAssert(sumRate != 1.0 , @" sum of all elemetns for array array_rateOfEachColor must be 1.0 ");
for (int i = 0 ; i < [self.mutableArray_layers count]; i ++) {
CGFloat step = [[_array_rateOfEachColor objectAtIndex:i] floatValue];
CAShapeLayer *tmp = [self.mutableArray_layers objectAtIndex:i];
[tmp setStrokeStart:self.strokeEndValue];
if (i == (self.mutableArray_layers.count -1)) { //the last layer
self.strokeEndValue = 1.0 - self.space ;
[tmp setStrokeEnd:self.strokeEndValue];
}else {
[tmp setStrokeEnd:self.strokeEndValue + step ];
}
self.strokeEndValue += (step + self.space); // record last strokeEndValue
}
}else{
step = 1.0 / self.mutableArray_layers.count; //average step
for (int i = 0 ; i < [self.mutableArray_layers count]; i ++) {
CAShapeLayer *tmp = [self.mutableArray_layers objectAtIndex:i];
[tmp setStrokeStart:self.strokeEndValue];
if (i == (self.mutableArray_layers.count -1)) { //the last layer
self.strokeEndValue = 1.0 - self.space ;
[tmp setStrokeEnd:self.strokeEndValue];
}else {
[tmp setStrokeEnd:self.strokeEndValue + step ];
}
self.strokeEndValue += (step + self.space); // record last strokeEndValue
}
}
self.strokeEndValue = 0.0; // different os , the layoutSubViews was called different times,so,this line is resolve this problem so that figure can be shown correctly
}
CGFloat步长=0.0;
if(self.array_rateofechColor){
cGRate=0.0;
for(NSString*self.array中的速率\u RateOfechColor){
sumRate+=[费率浮动值];
}
NSAssert(sumRate!=1.0,@“数组的所有元素之和必须为1.0”);
对于(int i=0;i<[self.mutableArray_layers count];i++){
CGFloat step=[[u数组\每种颜色对象的比率索引:i]floatValue];
CAShapeLayer*tmp=[self.mutableArray_layers objectAtIndex:i];
[tmp setStrokeStart:self.strokeEndValue];
如果(i==(self.mutableArray_layers.count-1)){//最后一层
self.strokeEndValue=1.0-self.space;
[tmp setStrokeEnd:self.strokeEndValue];
}否则{
[tmp setStrokeEnd:self.strokeEndValue+step];
}
self.strokeEndValue+=(step+self.space);//记录最后一次strokeEndValue
}
}否则{
步长=1.0/self.mutableArray_layers.count;//平均步长
对于(int i=0;i<[self.mutableArray_layers count];i++){
CAShapeLayer*tmp=[self.mutableArray_layers objectAtIndex:i];
[tmp setStrokeStart:self.strokeEndValue];
如果(i==(self.mutableArray_layers.count-1)){//最后一层
self.strokeEndValue=1.0-self.space;
[tmp setStrokeEnd:self.strokeEndValue];
}否则{
[tmp setStrokeEnd:self.strokeEndValue+step];
}
self.strokeEndValue+=(step+self.space);//记录最后一次strokeEndValue
}
}
self.strokeEndValue=0.0;//不同的操作系统,LayoutSubview的调用时间不同,所以,这一行是为了解决这个问题,以便正确显示图形
}
我遇到了相同的问题。我尝试了不同的操作系统,对于相同的代码标志,得到了不同的结果:
一个设备只调用layoutSubViews一次,但另一个设备调用了两次,导致我的代码出现一个bug
我的代码:
CGFloat step = 0.0;
if (self.array_rateOfEachColor) {
CGFloat sumRate = 0.0;
for (NSString *rate in self.array_rateOfEachColor) {
sumRate +=[rate floatValue];
}
NSAssert(sumRate != 1.0 , @" sum of all elemetns for array array_rateOfEachColor must be 1.0 ");
for (int i = 0 ; i < [self.mutableArray_layers count]; i ++) {
CGFloat step = [[_array_rateOfEachColor objectAtIndex:i] floatValue];
CAShapeLayer *tmp = [self.mutableArray_layers objectAtIndex:i];
[tmp setStrokeStart:self.strokeEndValue];
if (i == (self.mutableArray_layers.count -1)) { //the last layer
self.strokeEndValue = 1.0 - self.space ;
[tmp setStrokeEnd:self.strokeEndValue];
}else {
[tmp setStrokeEnd:self.strokeEndValue + step ];
}
self.strokeEndValue += (step + self.space); // record last strokeEndValue
}
}else{
step = 1.0 / self.mutableArray_layers.count; //average step
for (int i = 0 ; i < [self.mutableArray_layers count]; i ++) {
CAShapeLayer *tmp = [self.mutableArray_layers objectAtIndex:i];
[tmp setStrokeStart:self.strokeEndValue];
if (i == (self.mutableArray_layers.count -1)) { //the last layer
self.strokeEndValue = 1.0 - self.space ;
[tmp setStrokeEnd:self.strokeEndValue];
}else {
[tmp setStrokeEnd:self.strokeEndValue + step ];
}
self.strokeEndValue += (step + self.space); // record last strokeEndValue
}
}
self.strokeEndValue = 0.0; // different os , the layoutSubViews was called different times,so,this line is resolve this problem so that figure can be shown correctly
}
CGFloat步长=0.0;
if(self.array_rateofechColor){
cGRate=0.0;
for(NSString*self.array中的速率\u RateOfechColor){
sumRate+=[费率浮动值];
}
NSAssert(sumRate!=1.0,@“数组的所有元素之和必须为1.0”);
对于(int i=0;i<[self.mutableArray_layers count];i++){
CGFloat step=[[u数组\每种颜色对象的比率索引:i]floatValue];
CAShapeLayer*tmp=[self.mutableArray_layers objectAtIndex:i];
[tmp setStrokeStart:self.strokeEndValue];
如果(i==(self.mutableArray_layers.count-1)){//最后一层
self.strokeEndValue=1.0-self.space;
[tmp setStrokeEnd:self.strokeEndValue];
}否则{
[tmp setStrokeEnd:self.strokeEndValue+step];
}
self.strokeEndValue+=(step+self.space);//记录最后一次strokeEndValue
}
}否则{
步长=1.0/self.mutableArray_layers.count;//平均步长
对于(int i=0;i<[self.mutableArray_layers count];i++){
CAShapeLayer*tmp=[self.mutableArray_layers objectAtIndex:i];
[tmp setStrokeStart:self.strokeEndValue];
如果(i==(self.mutableArray_layers.count-1)){//最后一层
self.strokeEndValue=1.0-self.space;
[tmp setStrokeEnd:self.strokeEndValue];
}否则{
[tmp setStrokeEnd:self.strokeEndValue+step];
}
self.strokeEndValue+=(step+self.space);//记录最后一次strokeEndValue
}
}
self.strokeEndValue=0.0;//不同的操作系统,LayoutSubview的调用时间不同,所以,这一行是为了解决这个问题,以便正确显示图形
}
这就是它的工作方式。我有一个控制器,它被称为五次。取决于控制器及其设置方式。编写可以处理这种情况的代码。如果没有任何其他代码(如更改帧或旋转),只需添加子视图即可。如何处理这种情况?在layoutSubviews
中编写代码,使其可以多次调用。通常,尽量避免覆盖layoutSubviews
。这就是它的工作方式。我有一个控制器,它被称为五次。取决于控制器及其设置方式。编写可以处理这种情况的代码。如果没有任何其他代码(如更改帧或旋转),只需添加子视图即可。如何处理这种情况?在layoutSubviews
中编写代码,使其可以多次调用。通常,尽量避免覆盖布局子视图
。