1
我有一個UIView,我想通過UISlider可以調整大小。我的UISlider範圍設置爲1.0-2.0。目前我正在使用CGAffineTransform調整視圖的大小。調整大小的UIView和動態調整繪圖中的drawRect
截圖:
我在自定義的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];
然而,圖像看起來不清晰或「重新繪製」
我將如何縮放路徑?在我使用CGAffineTransform之前。我可以將這個對象應用於UIBezierPaths嗎? – Kex
編輯我的問題,告訴你我正在嘗試的東西.. – Kex
是的,你會對所有的路徑應用一個類似的變換,然後繪製它們。沒有變換將應用於視圖。 – Wain