Skip to content

Commit ec6fd3a

Browse files
committed
修复布局视图套布局视图并且尺寸为wrap时可能导致死循环的BUG。
1 parent 5dbc94c commit ec6fd3a

File tree

7 files changed

+75
-15
lines changed

7 files changed

+75
-15
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
---
55

6+
## [V1.4.1](https://github.com/youngsoft/MyLinearLayout/releases/tag/1.4.1)(2017/6/22)
7+
#### Fixed
8+
1. 修复了布局视图套布局视图,然后都具有wrapContentWidth或者wrapContentHeight属性时界面有可能进入死循环的问题,尤其是iPhonePlus设备。
9+
10+
611
## [V1.4.0](https://github.com/youngsoft/MyLinearLayout/releases/tag/1.4.0)(2017/6/16)
712

813
#### Added

MyLayout.podspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
1616
#
1717

1818
s.name = "MyLayout"
19-
s.version = "1.4.0"
19+
s.version = "1.4.1"
2020
s.summary = "MyLayout is an iOS UI framework integrates the functions with Android,AutoLayout,SizeClass,HTML CSS float and flexbox,UIView UITableView."
2121

2222
s.description = <<-DESC
@@ -75,7 +75,7 @@ Pod::Spec.new do |s|
7575
# Supports git, hg, bzr, svn and HTTP.
7676
#
7777

78-
s.source = { :git => "https://github.com/youngsoft/MyLinearLayout.git", :tag => "1.4.0" }
78+
s.source = { :git => "https://github.com/youngsoft/MyLinearLayout.git", :tag => "1.4.1" }
7979

8080

8181
# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

MyLayout/Lib/MyBaseLayout.m

+61-10
Original file line numberDiff line numberDiff line change
@@ -1808,7 +1808,8 @@ -(void)layoutSubviews
18081808
//设置子视图的frame并还原
18091809
for (UIView *sbv in self.subviews)
18101810
{
1811-
CGPoint ptorigin = sbv.bounds.origin;
1811+
CGRect sbvOldBounds = sbv.bounds;
1812+
CGPoint sbvOldCenter = sbv.center;
18121813

18131814
MyFrame *sbvmyFrame = sbv.myFrame;
18141815
UIView *sbvsc = [self myCurrentSizeClassFrom:sbvmyFrame];
@@ -1833,22 +1834,54 @@ -(void)layoutSubviews
18331834
if ([sbv isKindOfClass:[MyBaseLayout class]])
18341835
{
18351836
rc = _myRoundRectForLayout(sbvmyFrame.frame);
1837+
1838+
1839+
CGRect sbvTempBounds = CGRectMake(sbvOldBounds.origin.x, sbvOldBounds.origin.y, rc.size.width, rc.size.height);
1840+
1841+
if (_myCGFloatErrorEqual(sbvTempBounds.size.width, sbvOldBounds.size.width, _myMLayoutSizeError))
1842+
sbvTempBounds.size.width = sbvOldBounds.size.width;
1843+
1844+
if (_myCGFloatErrorEqual(sbvTempBounds.size.height, sbvOldBounds.size.height, _myMLayoutSizeError))
1845+
sbvTempBounds.size.height = sbvOldBounds.size.height;
1846+
1847+
1848+
if (_myCGFloatErrorNotEqual(sbvTempBounds.size.width, sbvOldBounds.size.width, _myMLayoutSizeError)||
1849+
_myCGFloatErrorNotEqual(sbvTempBounds.size.height, sbvOldBounds.size.height, _myMLayoutSizeError))
1850+
{
1851+
sbv.bounds = sbvTempBounds;
1852+
}
1853+
1854+
CGPoint sbvTempCenter = CGPointMake(rc.origin.x + sbv.layer.anchorPoint.x * sbvTempBounds.size.width, rc.origin.y + sbv.layer.anchorPoint.y * sbvTempBounds.size.height);
1855+
1856+
if (_myCGFloatErrorEqual(sbvTempCenter.x, sbvOldCenter.x, _myMLayoutSizeError))
1857+
sbvTempCenter.x = sbvOldCenter.x;
1858+
1859+
if (_myCGFloatErrorEqual(sbvTempCenter.y, sbvOldCenter.y, _myMLayoutSizeError))
1860+
sbvTempCenter.y = sbvOldCenter.y;
1861+
1862+
1863+
if (_myCGFloatErrorNotEqual(sbvTempCenter.x, sbvOldCenter.x, _myMLayoutSizeError)||
1864+
_myCGFloatErrorNotEqual(sbvTempCenter.y, sbvOldCenter.y, _myMLayoutSizeError))
1865+
{
1866+
sbv.center = sbvTempCenter;
1867+
}
1868+
1869+
18361870
}
18371871
else
18381872
{
18391873
rc = _myRoundRect(sbvmyFrame.frame);
1840-
}
1841-
18421874

1843-
sbv.center = CGPointMake(rc.origin.x + sbv.layer.anchorPoint.x * rc.size.width, rc.origin.y + sbv.layer.anchorPoint.y * rc.size.height);
1844-
sbv.bounds = CGRectMake(ptorigin.x, ptorigin.y, rc.size.width, rc.size.height);
1875+
sbv.center = CGPointMake(rc.origin.x + sbv.layer.anchorPoint.x * rc.size.width, rc.origin.y + sbv.layer.anchorPoint.y * rc.size.height);
1876+
sbv.bounds = CGRectMake(sbvOldBounds.origin.x, sbvOldBounds.origin.y, rc.size.width, rc.size.height);
1877+
1878+
}
18451879

1846-
18471880
}
18481881

18491882
if (sbvsc.myVisibility == MyVisibility_Gone && !sbv.isHidden)
18501883
{
1851-
sbv.bounds = CGRectMake(ptorigin.x, ptorigin.y, 0, 0);
1884+
sbv.bounds = CGRectMake(sbvOldBounds.origin.x, sbvOldBounds.origin.y, 0, 0);
18521885
}
18531886

18541887
if (sbvmyFrame.sizeClass.viewLayoutCompleteBlock != nil)
@@ -1869,8 +1902,8 @@ -(void)layoutSubviews
18691902

18701903
//因为布局子视图的新老尺寸计算在上面有两种不同的方法,因此这里需要考虑两种计算的误差值,而这两种计算的误差值是不超过1/屏幕精度的。
18711904
//因此我们认为当二者的值超过误差时我们才认为有尺寸变化。
1872-
BOOL isWidthAlter = fabs(newSelfSize.width - oldSelfSize.width) > _myMLayoutSizeError;
1873-
BOOL isHeightAlter = fabs(newSelfSize.height - oldSelfSize.height) > _myMLayoutSizeError;
1905+
BOOL isWidthAlter = _myCGFloatErrorNotEqual(newSelfSize.width, oldSelfSize.width, _myMLayoutSizeError);
1906+
BOOL isHeightAlter = _myCGFloatErrorNotEqual(newSelfSize.height, oldSelfSize.height, _myMLayoutSizeError);
18741907

18751908
//如果父视图也是布局视图,并且自己隐藏则不调整自身的尺寸和位置。
18761909
BOOL isAdjustSelf = YES;
@@ -3513,6 +3546,24 @@ -(NSString*)description
35133546
@end
35143547

35153548

3549+
BOOL _myCGFloatErrorEqual(CGFloat f1, CGFloat f2, CGFloat error)
3550+
{
3551+
#if CGFLOAT_IS_DOUBLE == 1
3552+
return fabs(f1 - f2) < error;
3553+
#else
3554+
return fabsf(f1 - f2) < error;
3555+
#endif
3556+
}
3557+
3558+
BOOL _myCGFloatErrorNotEqual(CGFloat f1, CGFloat f2, CGFloat error)
3559+
{
3560+
#if CGFLOAT_IS_DOUBLE == 1
3561+
return fabs(f1 - f2) > error;
3562+
#else
3563+
return fabsf(f1 - f2) > error;
3564+
#endif
3565+
}
3566+
35163567
BOOL _myCGFloatEqual(CGFloat f1, CGFloat f2)
35173568
{
35183569
#if CGFLOAT_IS_DOUBLE == 1
@@ -3590,7 +3641,7 @@ CGFloat _myRoundNumber(CGFloat f)
35903641
if (f == 0 || f == CGFLOAT_MAX || f == -CGFLOAT_MAX)
35913642
return f;
35923643

3593-
int fi = round(f);
3644+
int fi = rint(f);
35943645
if (_myCGFloatEqual(f, fi))
35953646
return fi;
35963647

MyLayout/Lib/MyLayout.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838

3939

40-
//Current version is 1.4.0, please open: https://github.com/youngsoft/MyLinearLayout/blob/master/CHANGELOG.md to show the changes.
40+
//Current version is 1.4.1, please open: https://github.com/youngsoft/MyLinearLayout/blob/master/CHANGELOG.md to show the changes.
4141

4242

4343
#ifndef MyLayout_MyLayout_h

MyLayout/Lib/MyLayoutInner.h

+4
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,16 @@
205205

206206
@end
207207

208+
extern BOOL _myCGFloatErrorEqual(CGFloat f1, CGFloat f2, CGFloat error);
209+
extern BOOL _myCGFloatErrorNotEqual(CGFloat f1, CGFloat f2, CGFloat error);
210+
208211
extern BOOL _myCGFloatLess(CGFloat f1, CGFloat f2);
209212
extern BOOL _myCGFloatGreat(CGFloat f1, CGFloat f2);
210213
extern BOOL _myCGFloatEqual(CGFloat f1, CGFloat f2);
211214
extern BOOL _myCGFloatNotEqual(CGFloat f1, CGFloat f2);
212215
extern BOOL _myCGFloatLessOrEqual(CGFloat f1, CGFloat f2);
213216
extern BOOL _myCGFloatGreatOrEqual(CGFloat f1, CGFloat f2);
217+
214218
extern BOOL _myCGSizeEqual(CGSize sz1, CGSize sz2);
215219
extern BOOL _myCGPointEqual(CGPoint pt1, CGPoint pt2);
216220
extern BOOL _myCGRectEqual(CGRect rect1, CGRect rect2);

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ To integrate MyLayout into your Xcode project using CocoaPods, specify it in you
558558
source 'https://github.com/CocoaPods/Specs.git'
559559
platform :ios, '7.0'
560560

561-
pod 'MyLayout', '~> 1.4.0'
561+
pod 'MyLayout', '~> 1.4.1'
562562
```
563563
564564
Then, run the following command:

README.zh.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ $ gem install cocoapods
566566
source 'https://github.com/CocoaPods/Specs.git'
567567
platform :ios, '7.0'
568568
569-
pod 'MyLayout', '~> 1.4.0'
569+
pod 'MyLayout', '~> 1.4.1'
570570
```
571571

572572
然后运行如下命令:

0 commit comments

Comments
 (0)