这是objective-c代码:
UIBezierPath *maskPath; maskPath = [UIBezierPath bezierPathWithRoundedRect:_backgroundImageView.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(3.0, 3.0)];
我想做的是,我想解析4个布尔值,然后修改byRoundingCorners
.但问题是,例如,我isRectCornerBottomLeft
是YES
和他们的其余部分NO
,我会做这样的事情:
maskPath = [UIBezierPath bezierPathWithRoundedRect:_backgroundImageView.bounds byRoundingCorners:(UIRectCornerBottomLeft) cornerRadii:CGSizeMake(3.0, 3.0)];
但我怎么能控制UIRectCorner
?当然,我可以做很多其他事情来检查是否isRectCornerBottomLeft
是YES
,以及哪一个是NO
写出每个条件.但除此之外,我该如何简化这种逻辑呢?谢谢.
没有真正的方法来"简化"逻辑.如果您有4个BOOL
值,则需要检查每个值:
UIRectCorner corners = 0; if (isRectCornerBottomLeft) { corners |= UIRectCornerBottomLeft; } if (isRectCornerBottomRight) { corners |= UIRectCornerBottomRight; } if (isRectCornerTopLeft) { corners |= UIRectCornerTopLeft; } if (isRectCornerTopRight) { corners |= UIRectCornerTopRight; }
你也可以这样做:
UIRectCorner corners = (isRectCornerBottomLeft ? UIRectCornerBottomLeft : 0) | (isRectCornerBottomRight ? UIRectCornerBottomRight : 0) | (isRectCornerTopLeft ? UIRectCornerTopLeft : 0) | (isRectCornerTopRight ? UIRectCornerTopRight : 0);