Objective-C を使用して Xcode で OSX アプリを作成しています。内部に NSView を含むウィンドウがあり、その NSView は NSNumbers を含む NSMutableArray からのデータを使用して、画像が 0,0 に描画されるようにグリッド上に対応する画像を描画することになっています。32,0; 64,0 . . . 0,32; 32,32; したがって、配列のカウントはグリッドの W*H であり、この場合は 21*21 または 441 です。
左クリックして画像を「配置」します。これは、クリックした場所に基づいて配列を更新し、setNeedsDisplay:YES を呼び出して、更新された配列を反映するように再描画することを意味します。これまでのところ、配列に基づいて適切に画像を描画することができます。
ただし、右クリックすると、特定のグリッド スロット内の画像が一定量回転するはずです。ここで私が抱えている唯一の問題は、回転した画像を適切な場所に実際に描画する方法を見つけることです。それらは中心点を中心に回転する必要があります。これは 16,16 の相対座標になります (すべての画像のサイズは 32x32 ピクセルです)。そのまま、私のコードは次のとおりです。
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
//Black background
[[NSColor blackColor] setFill];
NSRectFill(dirtyRect);
// Drawing code here.
NSRect rectToDraw = CGRectMake(0,0,32,32);
NSInteger imageIndex = 0;
NSImage *imageToDraw = nil;
for (int i = 0; i < [objDataArray count]; i++) {
//Loop through data array and draw each image where it should be
if ([objDataArray objectAtIndex:i]==[NSNull null]) continue; //Don't draw anything in this slot
//Math to determine where to draw based on the current for loop index
//0 = 0,0; 1 = 32,0 . . . 20 = 640,0; 21 = 0,32; etc. (grid is 21x21)
rectToDraw.origin.x = (i % 21)*32;
rectToDraw.origin.y = floor(i/21)*32;
//Get the data at this index in the data array
imageIndex = [((NSNumber*)[objDataArray objectAtIndex:i]) integerValue];
//Use the retrieved number to get a corresponding image
imageToDraw = (NSImage*)[objImagesArray objectAtIndex:imageIndex];
//Draw that image at the proper location
[imageToDraw drawInRect:rectToDraw];
}
}
つまり、角度単位の回転量が変数rotationAmountによって指定されているとします。drawInRect 行 (右中かっこの前の最後の行) を変更して、rectToDraw で指定された適切な位置に画像が描画されるようにするにはどうすればよいですか?
ありがとう。