2015-10-18 126 views
1

我有一个UIView,我想通过UISlider可以调整大小。我的UISlider范围设置为1.0-2.0。目前我正在使用CGAffineTransform调整视图的大小。调整大小的UIView和动态调整绘图中的drawRect

截图:

enter image description here

我在自定义的UIView完成:

- (void)drawRect:(CGRect)rect { 

    NSLog(@"Draw rect called"); 


    //// Square Drawing 
    UIBezierPath* squarePath = [UIBezierPath bezierPathWithRect: CGRectMake(8, 8, 50, 50)]; 
    [UIColor.whiteColor setFill]; 
    [squarePath fill]; 


    //// Right top Drawing 
    UIBezierPath* rightTopPath = [UIBezierPath bezierPathWithRect: CGRectMake(57, 13, 9, 10)]; 
    [UIColor.whiteColor setStroke]; 
    rightTopPath.lineWidth = 1; 
    [rightTopPath stroke]; 


    //// Top left Drawing 
    UIBezierPath* topLeftPath = [UIBezierPath bezierPathWithRect: CGRectMake(13, 0, 17.5, 9)]; 
    [UIColor.whiteColor setStroke]; 
    topLeftPath.lineWidth = 1; 
    [topLeftPath stroke]; 


    //// Top right Drawing 
    UIBezierPath* topRightPath = [UIBezierPath bezierPathWithRect: CGRectMake(35, 0, 17.5, 9)]; 
    [UIColor.whiteColor setStroke]; 
    topRightPath.lineWidth = 1; 
    [topRightPath stroke]; 


    //// Right middle Drawing 
    UIBezierPath* rightMiddlePath = [UIBezierPath bezierPathWithRect: CGRectMake(57, 28, 9, 10)]; 
    [UIColor.whiteColor setStroke]; 
    rightMiddlePath.lineWidth = 1; 
    [rightMiddlePath stroke]; 


    //// Right bottom Drawing 
    UIBezierPath* rightBottomPath = [UIBezierPath bezierPathWithRect: CGRectMake(57, 43, 9, 10)]; 
    [UIColor.whiteColor setStroke]; 
    rightBottomPath.lineWidth = 1; 
    [rightBottomPath stroke]; 


} 

代码为滑块:

- (IBAction)changeValue:(id)sender { 


    CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity, self.slider.value, self.slider.value); 
    self.square.transform = transform; 

} 

这工作好但出现质量损失我调整大小时绘图,因为我实际上不是动态的改变绘图的大小。我真正想要做的是动态改变图形的大小。我将如何实现这一目标?我是否需要先调整帧大小,然后调用[self.square setNeedsDisplay]传递乘法因子?

编辑:

所以我厌倦了这样的方法,现在当我移动滑块我有:

- (IBAction)changeValue:(id)sender { 


    CGRect newFrame = CGRectMake(20, 20, 72*self.slider2.value, 72*self.slider2.value); 
self.square.frame = newFrame; 
[self.square resizeAt:self.slider2.value]; 



} 

所以我只是改变帧大小现在,那么滑块值传递到视图并呼吁:

-(void)resizeAt: (float)multiply{ 

    self.size=multiply; 
    self.transform = CGAffineTransformScale(CGAffineTransformIdentity, self.size, self.size); 
    [self setNeedsDisplay]; 

} 

什么在发生的drawRect的例子:

UIBezierPath* squarePath = [UIBezierPath bezierPathWithRect: CGRectMake(11, 11, 50, 50)]; 
    [squarePath applyTransform:self.transform]; 
    [UIColor.whiteColor setFill]; 
    [squarePath fill]; 

然而,图像看起来不清晰或“重新绘制”

回答

1

考虑0.0〜1.0,然后应用你的直接变换到各贝塞尔路径的范围内创建与点贝塞尔路径。变换的比例显然会发生变化,因为您将路径缩放为完整视图大小,而不是相对大小。

由于缩放路径而不是已经渲染的视图,路径渲染仍然是干净和准确的。

+0

我将如何缩放路径?在我使用CGAffineTransform之前。我可以将这个对象应用于UIBezierPaths吗? – Kex

+0

编辑我的问题,告诉你我正在尝试的东西.. – Kex

+0

是的,你会对所有的路径应用一个类似的变换,然后绘制它们。没有变换将应用于视图。 – Wain