UIView
上書きで何かを描画する習慣があります
- (void)drawRect:(CGRect)rect
これは問題なく動作し、Retina スクリーンで鮮明な結果が得られます。
ただし、今は、図面の基になっているプロパティをアニメーション化できるようにしたいと考えています。アニメート可能なプロパティの作成は でのみ可能のようですCALayer
。そのため、 で描画を行う代わりにUIView
、カスタムCALayer
サブクラスを作成し、内部で描画を行います。
- (void) drawInContext:(CGContextRef)ctx
drawRect
この関数では、 customの関数で使用したのとほぼ同じ描画コードを使用しますUIView
。
結果は同じように見えますが、網膜の解像度ではなく、ピクセル化されています (大きな正方形のピクセル)。
私が入れたら
self.contentsScale = [UIScreen mainScreen].scale;
私のdrawInContext
実装の最初に、ピクセル化された結果の代わりに、ぼやけた結果が得られます(レンダリングがまだ非網膜解像度で実行され、その後網膜解像度にアップスケールされているかのように)。
で鋭い網膜パスをレンダリングする正しい方法はCALayers
drawInContext
何ですか?
ここにいくつかのスクリーンショットがあります(青い線は問題のカスタム図面の一部です。黄色の部分は単なるイメージです)
カスタム UIView の内部に描画drawRect
:
カスタム CALayer 内に描画drawInContext
:
最初drawInContext
に設定して、カスタム CALayer 内に描画します。self.contentScale
完全を期すために、(の簡略化されたバージョンの) 描画コードを次に示します。
//if drawing inside custom UIView sublcass:
- (void)drawRect:(CGRect)rect
{
CGContextRef currenctContext = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextSetLineWidth(currenctContext, _lineWidth);
CGContextSetLineJoin(currenctContext,kCGLineJoinRound);
CGContextMoveToPoint(currenctContext,x1, y1);
CGContextAddLineToPoint(currenctContext,x2, y2);
CGContextStrokePath(currenctContext);
}
//if drawing inside custom CALayer subclass:
- (void) drawInContext:(CGContextRef)ctx {
{
//self.contentsScale = [UIScreen mainScreen].scale;
CGContextRef currenctContext = ctx;
CGContextSetStrokeColorWithColor(currenctContext, [UIColor blackColor].CGColor);
CGContextSetLineWidth(currenctContext, _lineWidth);
CGContextSetLineJoin(currenctContext,kCGLineJoinRound);
CGContextMoveToPoint(currenctContext,x1, y1);
CGContextAddLineToPoint(currenctContext,x2, y2);
CGContextStrokePath(currenctContext);
}
私が達成したいことを言い換えると、UIView
アプローチと同じ鮮明な網膜レンダリングを達成したいのですが、CALayer