我有一个容器视图的子视图数组,以编程方式手动设置框架
当用户选择过滤器时,我想缩放所选视图(匹配过滤器),将其大小增加1.1并保持中心但防止重叠。
到目前为止,这是我的代码:
- (void) positionSubviewsWithoutOverlapping: (NSArray *) views {
for (UIView *v in views){
CGPoint center = v.center;
v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y, v.frame.size.width*1.1, v.frame.size.height*1.1);
v.center = center;
}
while ([self repositionSubviews:views]) {
}
}
- (BOOL) repositionSubviews: (NSArray *) views{
BOOL somethingChanged = FALSE;
for (UIView *v1 in views){
for (UIView *v2 in views){
if (v1 != v2){
if (CGRectIntersectsRect(v1.frame, v2.frame)){
somethingChanged = TRUE;
[self preventOverlapOfView1:v1 View2:v2];
}
}
}
}
return somethingChanged;
}
- (void) preventOverlapOfView1: (UIView *) v1 View2: (UIView *) v2 {
if (CGRectGetMaxX(v1.frame) > CGRectGetMinX(v2.frame)){
CGFloat diff = CGRectGetMaxX(v1.frame)-CGRectGetMinX(v2.frame);
v1.frame = CGRectMake(v1.frame.origin.x-diff, v1.frame.origin.y, v1.frame.size.width, v1.frame.size.height);
}
else if (CGRectGetMinX(v1.frame) < CGRectGetMaxX(v2.frame)){
CGFloat diff = CGRectGetMaxX(v2.frame)-CGRectGetMinX(v1.frame);
v2.frame = CGRectMake(v2.frame.origin.x-diff, v2.frame.origin.y, v2.frame.size.width, v2.frame.size.height);
}
if (CGRectGetMaxY(v1.frame) > CGRectGetMinY(v2.frame)){
CGFloat diff = CGRectGetMaxY(v1.frame)-CGRectGetMinY(v2.frame);
v2.frame = CGRectMake(v2.frame.origin.x, v2.frame.origin.y-diff, v2.frame.size.width, v2.frame.size.height);
}
else if (CGRectGetMinY(v1.frame) < CGRectGetMaxY(v2.frame)){
CGFloat diff = CGRectGetMaxY(v2.frame)-CGRectGetMinY(v1.frame);
v1.frame = CGRectMake(v1.frame.origin.x, v1.frame.origin.y-diff, v1.frame.size.width, v1.frame.size.height);
}
}
问题出在- (void) preventOverlapOfView1: (UIView *) v1 View2: (UIView *) v2
,我应该使用CGRectIntersection
,但我无法弄清楚如何使用它。
有没有人有建议?
答案 0 :(得分:0)
作为一个稍微左侧的建议,为什么不使用UICollectionView?它的设计精确适合您所建议的应用类型。您可以放大某个单元格,它会自动移动其他单元格以适应大小的变化。
如果没有,那么我建议您使用autolayout。这将再次更好地帮助其他观点不重叠。但是集合视图将是最佳选择。