Skip to content

Instantly share code, notes, and snippets.

@ocrickard
Created June 6, 2018 22:36
Show Gist options
  • Save ocrickard/d07e30f9ba2c29fef7e491ab42c19d08 to your computer and use it in GitHub Desktop.
Save ocrickard/d07e30f9ba2c29fef7e491ab42c19d08 to your computer and use it in GitHub Desktop.
CGRect firstRect = _selectionRects.count > 0 ? CGRectInset([_selectionRects.firstObject CGRectValue], -1, -1) : CGRectNull;
CGRect bigRect = CGRectNull;
CGRect lastRect = _selectionRects.count > 1 ? CGRectInset([_selectionRects.lastObject CGRectValue], -1, -1) : CGRectNull;
if (_selectionRects.count > 2) {
for (int i = 1; i < _selectionRects.count - 1; i++) {
NSValue *value = _selectionRects[i];
bigRect = CGRectUnion(bigRect, CGRectInset(value.CGRectValue, -1, -1));
}
}
if (!CGRectIsNull(bigRect)) {
CGFloat newSize = bigRect.size.height + CGRectGetMinY(bigRect) - CGRectGetMaxY(firstRect);
if (!CGRectIsNull(lastRect)) {
newSize += CGRectGetMinY(lastRect) - CGRectGetMaxY(bigRect);
}
bigRect.origin.y = CGRectGetMaxY(firstRect);
bigRect.size.height = newSize;
} else if (!CGRectIsNull(lastRect)) {
// Split the difference between firstRect and lastRect
CGFloat difference = CGRectGetMinY(lastRect) - CGRectGetMaxY(firstRect);
CGFloat midPointY = CGRectGetMaxY(firstRect) + difference / 2.0;
firstRect.size.height += midPointY - CGRectGetMaxY(firstRect);
CGFloat newLastRectSize = lastRect.size.height + CGRectGetMinY(lastRect) - midPointY;
lastRect.size.height = newLastRectSize;
lastRect.origin.y -= difference / 2.0;
}
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
BOOL displaySelectionLayer = NO;
if (!CGRectIsNull(firstRect)) {
[bezierPath appendPath:[UIBezierPath bezierPathWithRect:firstRect]];
displaySelectionLayer = YES;
}
if (!CGRectIsNull(bigRect)) {
[bezierPath appendPath:[UIBezierPath bezierPathWithRect:bigRect]];
}
if (!CGRectIsNull(lastRect)) {
[bezierPath appendPath:[UIBezierPath bezierPathWithRect:lastRect]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment