iphone - 增加框架时自动调整 mask 大小问题 - iOS

标签 iphone objective-c ios uiview autoresizingmask

我创建了一个对角线 UIView,在所有角和所有四个边缘的中心有八个 subview 。

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

所有 subview 在 Diagonal UIView 中添加一次,请参见图像 Screen1.. enter image description here

现在我不需要钻石 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 中所有红点的所有自动调整大小蒙版的列表。

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

enter image description here

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

enter image description here enter image description here

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

请记住,我不想通过任何框架或中心,只需使用自动调整大小蒙版进行管理。 .

了解 AutoresizeMask 的有用链接 Link .

问候,

最佳答案

自动调整大小无法正常工作,因为由于红点的高度,边缘中心的点的 topMargin/bottomMargin 比率不正好为 3。如果您的点的宽度和高度为零,它将正确定位。

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

如果您确实想仅使用 autoresizingMask 来解决此问题。您需要将每个红点放入零大小的 View 内,并将零大小的 View 添加到菱形 View 内。使用下面的代码而不是您提供的代码。您可能需要对其他部分进行一些更改,因为这些点不再是菱形的直接 subview 。

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: 方法。以下代码将确保即使触摸点位于 View 边界之外,触摸事件也会到达 subview 。

- (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;
}

关于iphone - 增加框架时自动调整 mask 大小问题 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12216631/

相关文章:

ios - 从 chartboost 删除应用程序?

iphone - 另一个 UIViewController 中的 UItableViewController

iphone - 调整 iPhone 5 中的表格 View 大小

ios - 动画状态栏 iOS 7

ios - 为什么无法访问 view.center.x/y

iOS:使用 NSNotificationCenter 在 userInfo 中发布代码块?

iOS:UIAlertController 上的 textField 成为出现警报时的第一响应者

iphone - iOS 从 CoreData 中获取按主键排序的对象

ios - 滚动 UITableView 时随机崩溃

iOS 8 GM 不更新对 Collection View 的约束