3

Here is the objective-c code:

UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:_backgroundImageView.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(3.0, 3.0)];

What I would like to do is, I would like to parse 4 booleans, and modify the byRoundingCorners. But the problem is that, for example, I have isRectCornerBottomLeft is YES, and rest of them is NO, I will do something like this:

maskPath = [UIBezierPath bezierPathWithRoundedRect:_backgroundImageView.bounds byRoundingCorners:(UIRectCornerBottomLeft) cornerRadii:CGSizeMake(3.0, 3.0)];

But how can I control the UIRectCorner? of course, I can do many if else to check whether the isRectCornerBottomLeft is YES, and which one is NO to write every conditions out. But apart from that, how can I simplify that logic? Thanks.

DNB5brims
  • 29,344
  • 50
  • 131
  • 195

1 Answers1

10

There is no real way to "simplify" the logic. If you have 4 BOOL values, you need to check each one:

UIRectCorner corners = 0;
if (isRectCornerBottomLeft) {
    corners |= UIRectCornerBottomLeft;
}
if (isRectCornerBottomRight) {
    corners |= UIRectCornerBottomRight;
}
if (isRectCornerTopLeft) {
    corners |= UIRectCornerTopLeft;
}
if (isRectCornerTopRight) {
    corners |= UIRectCornerTopRight;
}

You could also do something like:

UIRectCorner corners = (isRectCornerBottomLeft ? UIRectCornerBottomLeft : 0) |
                       (isRectCornerBottomRight ? UIRectCornerBottomRight : 0) |
                       (isRectCornerTopLeft ? UIRectCornerTopLeft : 0) |
                       (isRectCornerTopRight ? UIRectCornerTopRight : 0);
rmaddy
  • 314,917
  • 42
  • 532
  • 579