Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在drawRect中调整UIView大小和动态调整图形大小_Ios_Objective C_Uiview - Fatal编程技术网

Ios 在drawRect中调整UIView大小和动态调整图形大小

Ios 在drawRect中调整UIView大小和动态调整图形大小,ios,objective-c,uiview,Ios,Objective C,Uiview,我有一个UIView,我想通过UISlider调整大小。我的UISlider范围设置为1.0-2.0。目前,我正在使用CGAffineTransform调整视图的大小 截图: 我在自定义UIView中完成的操作: - (void)drawRect:(CGRect)rect { NSLog(@"Draw rect called"); //// Square Drawing UIBezierPath* squarePath = [UIBezierPath bezier

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

截图:

我在自定义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];

但是,图像看起来不清晰或“重绘”

请考虑使用0.0到1.0范围内的点创建贝塞尔路径,然后将变换直接应用于每个贝塞尔路径。变换的比例显然会改变,因为要将路径缩放到全视图大小,而不是相对大小


由于缩放路径而不是已渲染视图,因此路径渲染仍将保持干净和准确。

如何缩放路径?在我使用CGAffineTransform之前。我可以将此对象应用于UIBezierPath吗?是的,您可以对所有路径应用类似的变换,然后绘制它们。不会对视图应用任何转换。为什么要将范围从0.0更改为1.0?这样您就可以可靠地缩放到任何视图大小(很容易)-技术上不需要,但将来更易于维护。因此,我正在使用UISlider中的数字创建CGAffineTransform,然后我调用setNeedsDisplay,这将强制重画,并将转换应用于每个单独的UIBezierPath。它正在调整大小,但看起来仍然很模糊,好像还没有重新绘制。(编辑我的问题)。我做错什么了吗?