调整superview时如何调整所有子视图/子图层的大小?

时间:2014-12-04 16:05:04

标签: ios objective-c uiview uiimageview core-graphics

这个问题与项目有关:https://github.com/FindForm/FFAngularPointilism

目前,所有行为都是由原始UIImage的尺寸决定的。尽管调整大小,缩小和旋转,但是绘制了双三角形正方形"保持原样。无论应用它的UIImageView的边界如何,三角形模糊都保持不变。

在我的UIImageView子类中,我使用UIImage初始化并在指定的初始值设定项中调用以下内容:

- (void)loadMatrix
{
num = 12;

CGFloat width = self.bounds.size.width;
CGFloat height = self.bounds.size.height;
CGFloat theBiggestSideLength = MAX(width, height);

_array = [NSMutableArray array];
_array2 = [NSMutableArray array];

for(int i = 0; i < theBiggestSideLength; i += num)
{
    [self.array addObject:[NSMutableArray array]];
    [self.array2 addObject:[NSMutableArray array]];
}

for(int i = 0; i < self.array.count; i++)
{
    for(int j = 0; j < self.array.count; j++)
    {
        [self.array[i] addObject:[self getRGBAsFromImage:self.image
                                                     atX:j * 2 * num
                                                    andY:i * 2 * num]];

        int xIndex = ((j * 2 * num) - (num / 2.0));
        int yIndex = ((i * 2 * num) + (num / 2.0));
        xIndex %= (int)width * 2;
        yIndex %= (int)width * 2;
        NSLog(@"%d", xIndex);

        [self.array2[i] addObject:[self getRGBAsFromImage:self.image
                                                      atX:xIndex
                                                     andY:yIndex]];
    }
}

_imageGrayscaleView = [[UIImageView alloc] initWithImage:[self convertToGreyscale:self.image]];
_finalbwimage = self.imageGrayscaleView.image;
_imageGrayscaleView.frame = self.bounds;
[self insertSubview:_imageGrayscaleView atIndex:0];

_viewMask = [[UIView alloc] initWithFrame:self.frame];

_viewMask.center = CGPointMake(_viewMask.center.x,
                               _viewMask.center.y + _viewMask.frame.size.height / 2.0f);


_shapeLayer = [[CAShapeLayer alloc] init];
CGRect maskRect = CGRectZero;
CGPathRef path = CGPathCreateWithRect(maskRect, NULL);
_shapeLayer.path = path;
CGPathRelease(path);
self.imageGrayscaleView.layer.mask = _shapeLayer;
}

要绘制这些图块,我添加一个计时器并在每个间隔调用以下方法。 Num和row按预期更新。:

- (void)drawTile
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(pixel * num, row * num, num, num)];
view.backgroundColor = [self.array[row] objectAtIndex:pixel];
[self addSubview:view];
[self.ksubviews addObject:view];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(view.frame.origin.x , view.frame.origin.y + (view.frame.size.height))];
[path addLineToPoint:CGPointMake(view.frame.origin.x , view.frame.origin.y)];
[path addLineToPoint:CGPointMake(view.frame.origin.x + view.frame.size.width, view.frame.origin.y + view.frame.size.height)];
[path closePath];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = path.CGPath;
shapeLayer.strokeColor = [[UIColor blackColor] CGColor];
shapeLayer.fillColor = [((UIColor *)[self.array2[row] objectAtIndex:pixel]) CGColor];
shapeLayer.lineWidth = 0;
[self.layer addSublayer:shapeLayer];
[self.ksublayers addObject:shapeLayer];
}

以下是正确的行为:

enter image description here

当通过笔尖调整此图像的大小或以编程方式设置其边界时,可设置动画的三角形方块不会改变。

1 个答案:

答案 0 :(得分:0)

您可以在类中使用Draw Rect方法更改所有子视图,每次进行更改时,都可以调用方法setNeedDisplay。

我没有理解你的问题,但我希望这有帮助!