当UIView调整大小时,绘制线条的粗细会发生变化

时间:2014-06-05 10:51:42

标签: ios objective-c uiview

我有一个UIView根据捏手势调整自己的大小。我也在上面的视图中绘制了两条线。我注意到视图调整大小时线条的粗细会发生变化。知道为什么吗?

我的控制器类中的代码

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {
        recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
        recognizer.scale = 1;
}
UIView子类中的

代码

- (void)drawRect:(CGRect)rect
{
    NSLog(@"Drawing code");
    [super drawRect:rect];

    CGContextRef c = UIGraphicsGetCurrentContext();

    CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
    CGContextSetStrokeColor(c, red);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 20.0f, 5.0f);
    CGContextAddLineToPoint(c, 20.0f, rect.size.height);
    CGContextStrokePath(c);

    CGContextSetStrokeColor(c, red);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 0.0f, rect.size.height - 20);
    CGContextAddLineToPoint(c, rect.size.width - 20, rect.size.height - 20);
    CGContextStrokePath(c);
}

在阅读了答案和建议后,我尝试了以下内容,

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {
        recognizer.view.frame = CGRectApplyAffineTransform(recognizer.view.frame, CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale));
}

但它给了我以下错误,

enter image description here

1 个答案:

答案 0 :(得分:0)

变换将转换整个视图,包括其中绘制的内容。而是,更改视图的大小并重绘它。类似的东西:

myView.frame = CGRectApplyAffineTransform(myView.frame, inverseAffine);

也可能需要考虑规模。