【问题标题】:Autoresizing mask issue with Increasing frame - iOS增加帧的自动调整蒙版问题 - iOS
【发布时间】:2012-08-31 13:45:00
【问题描述】:

我创建了一个 Diagonal UIView,在所有四个边缘的所有角落和中心都有八个子视图..

所有红点都可以拖动来调整菱形的大小。

所有子视图在对角线 UIView 中添加一次,请参见图像 Screen1..

现在我不想通过 Diamond View 的框架,这样它就可以管理中心和红点的框架。 我需要使用 Autoresizemask 进行管理,因此我不需要在增加或减少时通过每个时间帧。

这里是为菱形创建红点的代码..

 view4 = [[CustomDotRD alloc] initWithFrame:CGRectMake(self.frame.size.width - self.frame.size.width/4 - RED_W_H_RD/2, self.frame.size.height - self.frame.size.height/4 - RED_W_H_RD/2, RED_W_H_RD, RED_W_H_RD)];
        view4.backgroundColor = [UIColor clearColor];
        UIPanGestureRecognizer *panGestureDiamond = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(singleFingerTapsDiamond:)];
        [view4 addGestureRecognizer:panGestureDiamond];
        [panGestureDiamond release];
        [view4 setContextPropertyColor:RGB_RED contextFrame:CGRectMake(RED_W_H/2, RED_W_H/2, RED_W_H, RED_W_H) index:4 tag:kTAG_D4];
        view4.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        view4.center = CGPointMake(self.frame.size.width - self.frame.size.width/4, self.frame.size.height - self.frame.size.height/4);
        [self addSubview:view4];
        [self.boundDiamondArray addObject:view4];

在这里我发布了一些图片,同时增加了菱形,一些红点有奇怪的行为..

这是在 Screen2 中显示的所有红点的所有 Autoresize 蒙版列表。

1 - BottomMargin , LeftMargin , RightMargin
2 - TopMargin , BottomMargin , LeftMargin , RightMargin
3 - TopMargin , BottomMargin , LeftMargin 
4 - TopMargin , BottomMargin , LeftMargin , RightMargin
5 - TopMargin , LeftMargin , RightMargin
6 - TopMargin , BottomMargin , LeftMargin , RightMargin
7 - TopMargin , BottomMargin , RightMargin
8 - TopMargin , BottomMargin , LeftMargin , RightMargin

四个角落的红点工作正常.. 但是四个中心边缘的红点不能正常工作.. 让我展示一些屏幕..

让我知道你解决这个问题的想法..

请记住,我不想传递任何框架或中心,只需使用 Autoresize Mask 进行管理。 .

了解 AutoresizeMask Link 的有用链接。

问候,

【问题讨论】:

  • 我会尝试将 #2 更改为顶部/右边距,将 #4 更改为底部/右侧。这将使它们更接近对角线。但我不认为只能通过自动调整大小来完成。

标签: iphone objective-c ios uiview autoresizingmask


【解决方案1】:

自动调整大小不适用于您,因为由于红点的高度,边缘中心的点的 topMargin/bottomMargin 比率不完全为 3。如果您的点的宽度和高度为零,它将被正确定位。

我认为实现layoutSubviews 方法将是您任务的更好解决方案。

如果你真的想只使用autoresizingMask 来解决这个问题。您需要将每个红点放在大小为零的视图中,并将零大小的视图添加到菱形视图中。使用下面的代码而不是您提供的代码。您可能需要对其他部分进行一些更改,因为这些点不再是钻石的直接子视图。

view4 = [[CustomDotRD alloc] initWithFrame:CGRectMake(-RED_W_H_RD/2, -RED_W_H_RD/2, RED_W_H_RD, RED_W_H_RD)];
view4.backgroundColor = [UIColor clearColor];
UIPanGestureRecognizer *panGestureDiamond = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(singleFingerTapsDiamond:)];
[view4 addGestureRecognizer:panGestureDiamond];
[panGestureDiamond release];
[view4 setContextPropertyColor:RGB_RED contextFrame:CGRectMake(RED_W_H/2, RED_W_H/2, RED_W_H, RED_W_H) index:4 tag:kTAG_D4];
UIView* dotView4 = [[CustomView alloc] initWithFrame:CGRectZero];
dotView4.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
dotView4.center = CGPointMake(self.frame.size.width - self.frame.size.width/4, self.frame.size.height - self.frame.size.height/4);
[dotView4 addSubview:view4];
[self addSubview:dotView4];
[dotView4 release];
[self.boundDiamondArray addObject:view4];

要解决触摸事件的问题,您应该在 CustomView 类中覆盖 UIViewpointInside:withEvent: 方法。以下代码将确保触摸事件到达子视图,即使触摸点位于视图边界之外。

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
  for(UIView* aView in self.subviews){
    CGPoint localPoint = [self convertPoint:point toView:aView];
    if([aView pointInside:localPoint withEvent:event]){
      return YES;
    }
  }
  return NO;
}

【讨论】:

  • 卓越 这很完美。但是适用于 view4 的 Pan Gesture 不起作用。 .让我知道您的任何有用意见..
  • 我已经编辑了答案并添加了如何通过手势解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多