2013-04-20 50 views
2

所以我已经子类化UIView并添加了一些绘图代码。我正在缩放产生的视图。 我想这个观点是独立的分辨率,使其在任何规模的清晰,我不会需要管理多个图像等等,等等UIView中矢量图形的平滑缩放

作为一个测试,我做了一下绘图代码看起来的喜欢这个。 它创建适合UIView的任何帧大小的同心椭圆。 实际上,外圈比框架稍小,因此不会被剪切。对此很好。实际的图形将更加复杂,并且将包含必须以小尺寸和具有这种性质的东西可读的文本。

- (void)drawRect:(CGRect)rect 
{ 
    UIColor* color = [UIColor colorWithRed: 0.833 green: 0.833 blue: 0.833 alpha: 1]; 

    float width = self.bounds.size.width; 
    float height = self.bounds.size.height; 
    float scalePercent = 0.8; 

    for(int i = 0; i < 10; i++){ 

     width = width * scalePercent; 
     height = height * scalePercent; 

     float x = (self.bounds.size.width - width)/2; 
     float y = (self.bounds.size.height - height)/2; 

     UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(x, y, width, height)]; 
     [color setStroke]; 
     ovalPath.lineWidth = 2; 
     [ovalPath stroke]; 
    } 
} 

现在,这里的换算:

- (void) makeBig{ 

    [UIView animateWithDuration:0.5 
          delay:0 
         options:UIViewAnimationOptionBeginFromCurrentState 
        animations:(void (^)(void)) ^{ 
         self.transform = CGAffineTransformMakeScale(2, 2); 
        } 
        completion:^(BOOL finished){ 
        }]; 
} 

当你运行这个视图缩放,但它是像素化。这是像素化的,因为视图尺寸增加了一倍,但分辨率没有改变。

所以,这里是如何不这样做。

- (void) makeBig{ 

    [UIView animateWithDuration:0.5 
          delay:0 
         options:UIViewAnimationOptionBeginFromCurrentState 
        animations:(void (^)(void)) ^{ 
         self.transform = CGAffineTransformMakeScale(2, 2); 
        } 
        completion:^(BOOL finished){ 
         CGRect targetFrame = self.frame; 
         self.transform = CGAffineTransformIdentity; 
         self.frame = targetFrame; 
         [self setNeedsDisplay]; 
        }]; 
} 

这可以工作,但修复程序在动画结束时可以在分辨率捕捉回屏幕分辨率时看到。 我可以尝试预先缩放视图并预先绘制最终大小,然后将其缩小,然后运行动画以再次缩放它,但出于各种原因,我可以认为这听起来完全愚蠢。我想我可能是错的,这是自火灾以来最明智的想法。但我有点怀疑。

那么矢量内容的平滑度如何做得最好呢?

+0

你好,我也试图画一个可缩放的向量,请帮助http://stackoverflow.com/questions/38191771/draw-zoomable-vector-in-ios-using-coregraphics – anoop4real 2016-08-01 12:53:45

回答

1

基于视图的动画真的很方便,但如果我没有弄错它使用CABasicAnimation,它只使用一个关键帧。它会多一点代码,但如果使用CAKeyframeAnimation代替,Core Animation将为每个关键帧重新绘制动画层的内容(您可以指定何时出现这些内容),这样可以避免出现像素化。

+0

我已经猜到我失踪了一些东西。我需要看看这个。感谢那。 – NFG 2013-04-20 12:39:38

+0

此外,我发现这个问题后,其他问题,但我希望报告一些实际的代码。 http://stackoverflow.com/questions/2168419/scaling-up-iphone-vector-based-graphics-on-affine-transforms – NFG 2013-04-20 12:40:56