Question

i have created one Diagonal UIView with eight subview at all corner and center of the all four edges..

All red dots are draggable to resize diamond shape.

All Subview add once in Diagonal UIView see image Screen1.. enter image description here

Now i dont want pass frame of Diamond View so it could manage with Center and frame of the red dots. i need to manage with Autoresizemask so i dont need to pass every time frame while increasing or decreasing.

here is the code for creating red dot for diamond shape..

 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];

Here i have post some images while increasing diamond shape some red dots has strange behavior..

And here is the list of all Autoresize mask for all red dots which looks in 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

Four corner red dots working fine.. but four center edges red dots are not working perfectly.. lets me show some screens..

enter image description here enter image description here

let me know your idea to solve this issue..

Remember i dont want to pass any frame or center just manage with Autoresize Mask . .

Helpful link to understand AutoresizeMask Link.

Regards,

Was it helpful?

Solution

Auto resizing does not work correctly for you because the topMargin/bottomMargin ratio is not exactly 3 for the dots at the center of edges due to the height of the red dots. It would be positioned correctly if you would have dots with zero width and height.

I think implementing layoutSubviews method would be a better solution for your task.

If you really want to solve this using only autoresizingMask. You need to put each red dot inside a view with zero size and add the zero-sized view inside the diamond view. Use the code below instead of the one you have provided. You may need to make some changes on other parts because the dots are no longer direct subview of the diamond.

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];

To fix the problem with touch events you should override pointInside:withEvent: method of UIView in CustomView class. The following code will make sure that touch events reach subviews even if the touch point is outside of the view's bounds.

- (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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top