0

カスタムの UIView サブクラスがあります

- (void)drawRect:(CGRect)rect

50,50 の位置に UIImage を描画し、20 度回転させたいと考えています。
次のコードを試しました:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextRotateCTM (context, degreesToRad(20));
[image drawAtPoint:CGPointMake(50, 50)];
CGContextRestoreGState(context);

画像を正しく回転させますが、50,50 ではありません...
正しい座標を維持しながら UIImage を回転するにはどうすればよいですか?

4

1 に答える 1

2

drawRect を使用して画像を描画するのはなぜですか?

UIImageView は可能な限り高速になるように最適化されているため、次のようなものを使用できます。

UIImage *image = [UIImage imageNamed:@"icon.png"];

UIImageView *imageView = [ [ UIImageView alloc ] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];

imageView.image = image;

[self addSubview:imageView];

CGAffineTransform rotate = CGAffineTransformMakeRotation( 1.0 / 20.0 * M_PI );

[imageView setTransform:rotate];
于 2013-10-17T15:04:36.120 に答える