Skip to content

Commit ed0905a

Browse files
committed
把全局的 flatf 重命名为 flat,并将参数类型从 float 改为 CGFloat,从而修复 UIImage 绘制图片时由于精度不足导致可能尺寸不正确的 bug
1 parent b5bf9aa commit ed0905a

19 files changed

Lines changed: 82 additions & 82 deletions

QMUIKit/UICommon/QMUICommonDefines.h

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -215,39 +215,39 @@ EndIgnoreAvailabilityWarning
215215
*
216216
* 例如传进来 “2.1”,在 2x 倍数下会返回 2.5(0.5pt 对应 1px),在 3x 倍数下会返回 2.333(0.333pt 对应 1px)。
217217
*/
218-
CG_INLINE float
219-
flatfSpecificScale(float floatValue, float scale) {
218+
CG_INLINE CGFloat
219+
flatSpecificScale(CGFloat floatValue, CGFloat scale) {
220220
scale = scale == 0 ? ScreenScale : scale;
221-
CGFloat flattedValue = ceilf(floatValue * scale) / scale;
221+
CGFloat flattedValue = ceil(floatValue * scale) / scale;
222222
return flattedValue;
223223
}
224224

225225
/**
226226
* 基于当前设备的屏幕倍数,对传进来的 floatValue 进行像素取整。
227227
*
228-
* 注意如果在 Core Graphic 绘图里使用时,要注意当前画布的倍数是否和设备屏幕倍数一致,若不一致,不可使用 flatf() 函数
228+
* 注意如果在 Core Graphic 绘图里使用时,要注意当前画布的倍数是否和设备屏幕倍数一致,若不一致,不可使用 flat() 函数,而应该用 flatSpecificScale
229229
*/
230-
CG_INLINE float
231-
flatf(float floatValue) {
232-
return flatfSpecificScale(floatValue, 0);
230+
CG_INLINE CGFloat
231+
flat(CGFloat floatValue) {
232+
return flatSpecificScale(floatValue, 0);
233233
}
234234

235235
/**
236-
* 类似flatf(),只不过 flatf 是向上取整,而 floorfInPixel 是向下取整
236+
* 类似flat(),只不过 flat 是向上取整,而 floorInPixel 是向下取整
237237
*/
238-
CG_INLINE float
239-
floorfInPixel(float floatValue) {
240-
CGFloat resultValue = floorf(floatValue * ScreenScale) / ScreenScale;
238+
CG_INLINE CGFloat
239+
floorInPixel(CGFloat floatValue) {
240+
CGFloat resultValue = floor(floatValue * ScreenScale) / ScreenScale;
241241
return resultValue;
242242
}
243243

244244
CG_INLINE BOOL
245-
betweenf(float minimumValue, float value, float maximumValue) {
245+
between(CGFloat minimumValue, CGFloat value, CGFloat maximumValue) {
246246
return minimumValue < value && value < maximumValue;
247247
}
248248

249249
CG_INLINE BOOL
250-
betweenOrEqualf(float minimumValue, float value, float maximumValue) {
250+
betweenOrEqual(CGFloat minimumValue, CGFloat value, CGFloat maximumValue) {
251251
return minimumValue <= value && value <= maximumValue;
252252
}
253253

@@ -268,26 +268,26 @@ ReplaceMethod(Class _class, SEL _originSelector, SEL _newSelector) {
268268
/// 用于居中运算
269269
CG_INLINE CGFloat
270270
CGFloatGetCenter(CGFloat parent, CGFloat child) {
271-
return flatf((parent - child) / 2.0);
271+
return flat((parent - child) / 2.0);
272272
}
273273

274274
#pragma mark - CGPoint
275275

276276
/// 两个point相加
277277
CG_INLINE CGPoint
278278
CGPointUnion(CGPoint point1, CGPoint point2) {
279-
return CGPointMake(flatf(point1.x + point2.x), flatf(point1.y + point2.y));
279+
return CGPointMake(flat(point1.x + point2.x), flat(point1.y + point2.y));
280280
}
281281

282282
/// 获取rect的center,包括rect本身的x/y偏移
283283
CG_INLINE CGPoint
284284
CGPointGetCenterWithRect(CGRect rect) {
285-
return CGPointMake(flatf(CGRectGetMidX(rect)), flatf(CGRectGetMidY(rect)));
285+
return CGPointMake(flat(CGRectGetMidX(rect)), flat(CGRectGetMidY(rect)));
286286
}
287287

288288
CG_INLINE CGPoint
289289
CGPointGetCenterWithSize(CGSize size) {
290-
return CGPointMake(flatf(size.width / 2.0), flatf(size.height / 2.0));
290+
return CGPointMake(flat(size.width / 2.0), flat(size.height / 2.0));
291291
}
292292

293293
#pragma mark - UIEdgeInsets
@@ -316,24 +316,24 @@ UIEdgeInsetsConcat(UIEdgeInsets insets1, UIEdgeInsets insets2) {
316316

317317
CG_INLINE UIEdgeInsets
318318
UIEdgeInsetsSetTop(UIEdgeInsets insets, CGFloat top) {
319-
insets.top = flatf(top);
319+
insets.top = flat(top);
320320
return insets;
321321
}
322322

323323
CG_INLINE UIEdgeInsets
324324
UIEdgeInsetsSetLeft(UIEdgeInsets insets, CGFloat left) {
325-
insets.left = flatf(left);
325+
insets.left = flat(left);
326326
return insets;
327327
}
328328
CG_INLINE UIEdgeInsets
329329
UIEdgeInsetsSetBottom(UIEdgeInsets insets, CGFloat bottom) {
330-
insets.bottom = flatf(bottom);
330+
insets.bottom = flat(bottom);
331331
return insets;
332332
}
333333

334334
CG_INLINE UIEdgeInsets
335335
UIEdgeInsetsSetRight(UIEdgeInsets insets, CGFloat right) {
336-
insets.right = flatf(right);
336+
insets.right = flat(right);
337337
return insets;
338338
}
339339

@@ -348,19 +348,19 @@ CGSizeIsEmpty(CGSize size) {
348348
/// 将一个CGSize像素对齐
349349
CG_INLINE CGSize
350350
CGSizeFlatted(CGSize size) {
351-
return CGSizeMake(flatf(size.width), flatf(size.height));
351+
return CGSizeMake(flat(size.width), flat(size.height));
352352
}
353353

354354
/// 将一个 CGSize 以 pt 为单位向上取整
355355
CG_INLINE CGSize
356356
CGSizeCeil(CGSize size) {
357-
return CGSizeMake(ceilf(size.width), ceilf(size.height));
357+
return CGSizeMake(ceil(size.width), ceil(size.height));
358358
}
359359

360360
/// 将一个 CGSize 以 pt 为单位向下取整
361361
CG_INLINE CGSize
362362
CGSizeFloor(CGSize size) {
363-
return CGSizeMake(floorf(size.width), floorf(size.height));
363+
return CGSizeMake(floor(size.width), floor(size.height));
364364
}
365365

366366
#pragma mark - CGRect
@@ -374,13 +374,13 @@ CGRectIsNaN(CGRect rect) {
374374
/// 创建一个像素对齐的CGRect
375375
CG_INLINE CGRect
376376
CGRectFlatMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height) {
377-
return CGRectMake(flatf(x), flatf(y), flatf(width), flatf(height));
377+
return CGRectMake(flat(x), flat(y), flat(width), flat(height));
378378
}
379379

380-
/// 对CGRect的x/y、width/height都调用一次flatf,以保证像素对齐
380+
/// 对CGRect的x/y、width/height都调用一次flat,以保证像素对齐
381381
CG_INLINE CGRect
382382
CGRectFlatted(CGRect rect) {
383-
return CGRectMake(flatf(rect.origin.x), flatf(rect.origin.y), flatf(rect.size.width), flatf(rect.size.height));
383+
return CGRectMake(flat(rect.origin.x), flat(rect.origin.y), flat(rect.size.width), flat(rect.size.height));
384384
}
385385

386386
/// 为一个CGRect叠加scale计算
@@ -392,13 +392,13 @@ CGRectApplyScale(CGRect rect, CGFloat scale) {
392392
/// 计算view的水平居中,传入父view和子view的frame,返回子view在水平居中时的x值
393393
CG_INLINE CGFloat
394394
CGRectGetMinXHorizontallyCenterInParentRect(CGRect parentRect, CGRect childRect) {
395-
return flatf((CGRectGetWidth(parentRect) - CGRectGetWidth(childRect)) / 2.0);
395+
return flat((CGRectGetWidth(parentRect) - CGRectGetWidth(childRect)) / 2.0);
396396
}
397397

398398
/// 计算view的垂直居中,传入父view和子view的frame,返回子view在垂直居中时的y值
399399
CG_INLINE CGFloat
400400
CGRectGetMinYVerticallyCenterInParentRect(CGRect parentRect, CGRect childRect) {
401-
return flatf((CGRectGetHeight(parentRect) - CGRectGetHeight(childRect)) / 2.0);
401+
return flat((CGRectGetHeight(parentRect) - CGRectGetHeight(childRect)) / 2.0);
402402
}
403403

404404
/// 返回值:同一个坐标系内,想要layoutingRect和已布局完成的referenceRect保持垂直居中时,layoutingRect的originY
@@ -481,32 +481,32 @@ CGRectLimitMaxWidth(CGRect rect, CGFloat maxWidth) {
481481

482482
CG_INLINE CGRect
483483
CGRectSetX(CGRect rect, CGFloat x) {
484-
rect.origin.x = flatf(x);
484+
rect.origin.x = flat(x);
485485
return rect;
486486
}
487487

488488
CG_INLINE CGRect
489489
CGRectSetY(CGRect rect, CGFloat y) {
490-
rect.origin.y = flatf(y);
490+
rect.origin.y = flat(y);
491491
return rect;
492492
}
493493

494494
CG_INLINE CGRect
495495
CGRectSetXY(CGRect rect, CGFloat x, CGFloat y) {
496-
rect.origin.x = flatf(x);
497-
rect.origin.y = flatf(y);
496+
rect.origin.x = flat(x);
497+
rect.origin.y = flat(y);
498498
return rect;
499499
}
500500

501501
CG_INLINE CGRect
502502
CGRectSetWidth(CGRect rect, CGFloat width) {
503-
rect.size.width = flatf(width);
503+
rect.size.width = flat(width);
504504
return rect;
505505
}
506506

507507
CG_INLINE CGRect
508508
CGRectSetHeight(CGRect rect, CGFloat height) {
509-
rect.size.height = flatf(height);
509+
rect.size.height = flat(height);
510510
return rect;
511511
}
512512

QMUIKit/UIComponents/ImagePickerLibrary/QMUIAlbumViewController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ - (void)layoutSubviews {
7676

7777
CGFloat contentViewPaddingRight = 10;
7878
self.imageView.frame = CGRectMake(0, 0, CGRectGetHeight(self.contentView.bounds), CGRectGetHeight(self.contentView.bounds));
79-
self.textLabel.frame = CGRectSetXY(self.textLabel.frame, CGRectGetMaxX(self.imageView.frame) + self.albumNameInsets.left, flatf([self.textLabel qmui_minYWhenCenterInSuperview]));
79+
self.textLabel.frame = CGRectSetXY(self.textLabel.frame, CGRectGetMaxX(self.imageView.frame) + self.albumNameInsets.left, flat([self.textLabel qmui_minYWhenCenterInSuperview]));
8080
CGFloat textLabelMaxWidth = CGRectGetWidth(self.contentView.bounds) - contentViewPaddingRight - CGRectGetWidth(self.detailTextLabel.frame) - self.albumNameInsets.right - CGRectGetMinX(self.textLabel.frame);
8181
if (CGRectGetWidth(self.textLabel.frame) > textLabelMaxWidth) {
8282
self.textLabel.frame = CGRectSetWidth(self.textLabel.frame, textLabelMaxWidth);
8383
}
8484

85-
self.detailTextLabel.frame = CGRectSetXY(self.detailTextLabel.frame, CGRectGetMaxX(self.textLabel.frame) + self.albumNameInsets.right, flatf([self.detailTextLabel qmui_minYWhenCenterInSuperview]));
85+
self.detailTextLabel.frame = CGRectSetXY(self.detailTextLabel.frame, CGRectGetMaxX(self.textLabel.frame) + self.albumNameInsets.right, flat([self.detailTextLabel qmui_minYWhenCenterInSuperview]));
8686
_bottomLineLayer.frame = CGRectMake(0, CGRectGetHeight(self.contentView.bounds) - PixelOne, CGRectGetWidth(self.bounds), PixelOne);
8787
}
8888

QMUIKit/UIComponents/ImagePickerLibrary/QMUIImagePickerViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ - (void)initPreviewViewControllerIfNeeded {
257257
- (CGSize)referenceImageSize {
258258
CGFloat collectionViewWidth = CGRectGetWidth(self.collectionView.bounds);
259259
CGFloat collectionViewContentSpacing = collectionViewWidth - UIEdgeInsetsGetHorizontalValue(self.collectionView.contentInset);
260-
NSInteger columnCount = floorf(collectionViewContentSpacing / self.minimumImageWidth);
260+
NSInteger columnCount = floor(collectionViewContentSpacing / self.minimumImageWidth);
261261
CGFloat referenceImageWidth = self.minimumImageWidth;
262262
BOOL isSpacingEnoughWhenDisplayInMinImageSize = UIEdgeInsetsGetHorizontalValue(self.collectionViewLayout.sectionInset) + (self.minimumImageWidth + self.collectionViewLayout.minimumInteritemSpacing) * columnCount - self.collectionViewLayout.minimumInteritemSpacing <= collectionViewContentSpacing;
263263
if (!isSpacingEnoughWhenDisplayInMinImageSize) {

QMUIKit/UIComponents/QMUIDialogViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ - (void)viewDidLayoutSubviews {
255255
button.frame = self.footerView.bounds;
256256
self.buttonSeparatorLayer.hidden = YES;
257257
} else {
258-
CGFloat buttonWidth = flatf(CGRectGetWidth(self.footerView.bounds) / buttonCount);
258+
CGFloat buttonWidth = flat(CGRectGetWidth(self.footerView.bounds) / buttonCount);
259259
self.cancelButton.frame = CGRectMake(0, 0, buttonWidth, CGRectGetHeight(self.footerView.bounds));
260260
self.submitButton.frame = CGRectMake(CGRectGetMaxX(self.cancelButton.frame), 0, CGRectGetWidth(self.footerView.bounds) - CGRectGetMaxX(self.cancelButton.frame), CGRectGetHeight(self.footerView.bounds));
261261
self.buttonSeparatorLayer.hidden = NO;

QMUIKit/UIComponents/QMUIEmotionView.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,16 @@ - (void)layoutSubviews {
120120
[super layoutSubviews];
121121
// 删除按钮必定布局到最后一个表情的位置,且与表情上下左右居中
122122
[self.deleteButton sizeToFit];
123-
self.deleteButton.frame = CGRectSetXY(self.deleteButton.frame, flatf(CGRectGetWidth(self.bounds) - self.padding.right - CGRectGetWidth(self.deleteButton.frame) - (self.emotionSize.width - CGRectGetWidth(self.deleteButton.frame)) / 2.0), flatf(CGRectGetHeight(self.bounds) - self.padding.bottom - CGRectGetHeight(self.deleteButton.frame) - (self.emotionSize.height - CGRectGetHeight(self.deleteButton.frame)) / 2.0));
123+
self.deleteButton.frame = CGRectSetXY(self.deleteButton.frame, flat(CGRectGetWidth(self.bounds) - self.padding.right - CGRectGetWidth(self.deleteButton.frame) - (self.emotionSize.width - CGRectGetWidth(self.deleteButton.frame)) / 2.0), flat(CGRectGetHeight(self.bounds) - self.padding.bottom - CGRectGetHeight(self.deleteButton.frame) - (self.emotionSize.height - CGRectGetHeight(self.deleteButton.frame)) / 2.0));
124124
}
125125

126126
- (void)drawRect:(CGRect)rect {
127127
[self.emotionHittingRects removeAllObjects];
128128

129129
CGSize contentSize = CGRectInsetEdges(self.bounds, self.padding).size;
130130
NSInteger emotionCountPerRow = (contentSize.width + self.minimumEmotionHorizontalSpacing) / (self.emotionSize.width + self.minimumEmotionHorizontalSpacing);
131-
CGFloat emotionHorizontalSpacing = flatf((contentSize.width - emotionCountPerRow * self.emotionSize.width) / (emotionCountPerRow - 1));
132-
CGFloat emotionVerticalSpacing = flatf((contentSize.height - self.numberOfRows * self.emotionSize.height) / (self.numberOfRows - 1));
131+
CGFloat emotionHorizontalSpacing = flat((contentSize.width - emotionCountPerRow * self.emotionSize.width) / (emotionCountPerRow - 1));
132+
CGFloat emotionVerticalSpacing = flat((contentSize.height - self.numberOfRows * self.emotionSize.height) / (self.numberOfRows - 1));
133133

134134
CGPoint emotionOrigin = CGPointZero;
135135
for (NSInteger i = 0, l = self.emotions.count; i < l; i++) {
@@ -280,7 +280,7 @@ - (void)pageEmotions {
280280
CGFloat contentWidthInPage = CGRectGetWidth(self.collectionView.bounds) - UIEdgeInsetsGetHorizontalValue(self.paddingInPage);
281281
NSInteger maximumEmotionCountPerRowInPage = (contentWidthInPage + self.minimumEmotionHorizontalSpacing) / (self.emotionSize.width + self.minimumEmotionHorizontalSpacing);
282282
NSInteger maximumEmotionCountPerPage = maximumEmotionCountPerRowInPage * self.numberOfRowsPerPage - 1;// 删除按钮占一个表情位置
283-
NSInteger pageCount = ceilf((CGFloat)self.emotions.count / (CGFloat)maximumEmotionCountPerPage);
283+
NSInteger pageCount = ceil((CGFloat)self.emotions.count / (CGFloat)maximumEmotionCountPerPage);
284284
for (NSInteger i = 0; i < pageCount; i ++) {
285285
NSRange emotionRangeForPage = NSMakeRange(maximumEmotionCountPerPage * i, maximumEmotionCountPerPage);
286286
if (NSMaxRange(emotionRangeForPage) > self.emotions.count) {
@@ -342,7 +342,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
342342

343343
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
344344
if (scrollView == self.collectionView) {
345-
NSInteger currentPage = roundf(scrollView.contentOffset.x / CGRectGetWidth(scrollView.bounds));
345+
NSInteger currentPage = round(scrollView.contentOffset.x / CGRectGetWidth(scrollView.bounds));
346346
self.pageControl.currentPage = currentPage;
347347
}
348348
}

QMUIKit/UIComponents/QMUIGridView.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ - (void)setSeparatorColor:(UIColor *)separatorColor {
6464

6565
// 返回最接近平均列宽的值,保证其为整数,因此所有columnWidth加起来可能比总宽度要小
6666
- (CGFloat)stretchColumnWidth {
67-
return floorf((CGRectGetWidth(self.bounds) - self.separatorWidth * (self.columnCount - 1)) / self.columnCount);
67+
return floor((CGRectGetWidth(self.bounds) - self.separatorWidth * (self.columnCount - 1)) / self.columnCount);
6868
}
6969

7070
- (NSInteger)rowCount {

QMUIKit/UIComponents/QMUIImagePreviewView.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
178178

179179
// 在滑动过临界点的那一次才去调用 delegate,避免过于频繁的调用
180180
BOOL isFirstDidScroll = self.previousIndexWhenScrolling == 0;
181-
BOOL turnPageToRight = betweenOrEqualf(self.previousIndexWhenScrolling, floorf(index) + 0.5, index);
182-
BOOL turnPageToLeft = betweenOrEqualf(index, floorf(index) + 0.5, self.previousIndexWhenScrolling);
181+
BOOL turnPageToRight = betweenOrEqual(self.previousIndexWhenScrolling, floor(index) + 0.5, index);
182+
BOOL turnPageToLeft = betweenOrEqual(index, floor(index) + 0.5, self.previousIndexWhenScrolling);
183183
if (!isFirstDidScroll && (turnPageToRight || turnPageToLeft)) {
184-
index = roundf(index);
184+
index = round(index);
185185
if (0 <= index && index < [self.collectionView numberOfItemsInSection:0]) {
186186

187187
// 不调用 setter,避免又走一次 scrollToItem

QMUIKit/UIComponents/QMUIMoreOperationController.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ - (void)viewDidLayoutSubviews {
356356
NSInteger maxItemCountInScrollView = MAX(self.importantShowingItems.count, self.normalShowingItems.count);
357357
NSInteger itemCountForTotallyVisibleItem = isLargeSreen ? 4 : 3;
358358

359-
CGFloat itemWidth = flatf((contentWidth - fmaxf(UIEdgeInsetsGetHorizontalValue(importantScrollViewInsets), UIEdgeInsetsGetHorizontalValue(normaltScrollViewInsets))) / itemCountForTotallyVisibleItem) - (maxItemCountInScrollView > itemCountForTotallyVisibleItem ? (isLargeSreen ? 8 : 12) : 0);
359+
CGFloat itemWidth = flat((contentWidth - fmaxf(UIEdgeInsetsGetHorizontalValue(importantScrollViewInsets), UIEdgeInsetsGetHorizontalValue(normaltScrollViewInsets))) / itemCountForTotallyVisibleItem) - (maxItemCountInScrollView > itemCountForTotallyVisibleItem ? (isLargeSreen ? 8 : 12) : 0);
360360

361361
CGFloat itemMaxHeight = 0;
362362
CGFloat itemMaxX = 0;
@@ -371,7 +371,7 @@ - (void)viewDidLayoutSubviews {
371371
itemMaxHeight = CGRectGetHeight(itemView.bounds);
372372
}
373373
}
374-
self.importantItemsScrollView.contentSize = CGSizeMake(flatf(itemMaxX), flatf(itemMaxHeight));
374+
self.importantItemsScrollView.contentSize = CGSizeMake(flat(itemMaxX), flat(itemMaxHeight));
375375
self.importantItemsScrollView.contentInset = importantScrollViewInsets;
376376
self.importantItemsScrollView.contentOffset = CGPointMake(-self.importantItemsScrollView.contentInset.left, -self.importantItemsScrollView.contentInset.top);
377377
self.importantItemsScrollView.frame = CGRectFlatted(CGRectMake(0, 0, contentWidth, UIEdgeInsetsGetVerticalValue(self.importantItemsScrollView.contentInset) + self.importantItemsScrollView.contentSize.height));
@@ -396,7 +396,7 @@ - (void)viewDidLayoutSubviews {
396396
itemMaxHeight = CGRectGetHeight(itemView.bounds);
397397
}
398398
}
399-
self.normalItemsScrollView.contentSize = CGSizeMake(flatf(itemMaxX), flatf(itemMaxHeight));
399+
self.normalItemsScrollView.contentSize = CGSizeMake(flat(itemMaxX), flat(itemMaxHeight));
400400
self.normalItemsScrollView.contentInset = normaltScrollViewInsets;
401401
self.normalItemsScrollView.frame = CGRectFlatted(CGRectMake(0, layoutOriginY, contentWidth, UIEdgeInsetsGetVerticalValue(self.normalItemsScrollView.contentInset) + self.normalItemsScrollView.contentSize.height));
402402
self.normalItemsScrollView.contentOffset = CGPointMake(-self.normalItemsScrollView.contentInset.left, -self.normalItemsScrollView.contentInset.top);

0 commit comments

Comments
 (0)