我有一个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 :(得分:1)
考虑使用0.0到1.0范围内的点创建贝塞尔曲线路径,然后将变换直接应用于每个贝塞尔曲线路径。变换的比例显然会发生变化,因为您将路径缩放到完整视图大小,而不是相对大小。
因为缩放路径而不是已经渲染的视图,所以路径渲染仍然是干净和准确的。