NSAffineTransform Examples

  1. - (void) drawAnnotationElement:(unsigned) indx maxElement:(unsigned) max inRect:(NSRect) aRect atEdge:(DKOGridEdge) edge 
  2. {
  3. //<indx> is the ordinal item to draw, starting from 0. The actual drawn element factors in the offset and increment. <edge> specifies which edge(s) the annotation // should be drawn at. It will be left or right. For vertical annotation left == top, right == bottom. 
  4.         
  5.         if( edge == 0 )
  6.                 return;         // nothing to do
  7.         
  8.         NSString*       str = [self annotationStringForElement:indx 
  9. maxElement:max];
  10. NSAttributedString* std = [[NSAttributedString alloc] initWithString:str attributes:[self textAttributes]]; 
  11.         NSRect  bbox, ir;
  12.         
  13.         // get the bounding box for the string
  14.         bbox.size = [std size];
  15.         ir = [mGridRef borderRect];
  16.         
  17.         // swap the box width/height if rotated
  18.         
  19.         if([self isRotated])
  20.         {
  21.                 float temp = bbox.size.width;
  22.                 bbox.size.width = bbox.size.height;
  23.                 bbox.size.height = temp;
  24.         }
  25.         
  26.         // position the box
  27.         
  28.         float textOffset = [mGridRef borderAllowance] + mOffset;
  29.         
  30.         float sd = [mGridRef spanLength];
  31.         
  32.         if([self orientationIsVertical])
  33.         {
  34.                 if( edge == kDKOLeftEdge )
  35.                         bbox.origin.x = NSMinX( ir ) - bbox.size.width - 
  36. textOffset;
  37.                 else
  38.                         bbox.origin.x = NSMaxX( ir ) + textOffset;
  39.                 
  40. bbox.origin.y = ( NSMinY( ir ) + ( sd * indx )) - (bbox.size.height * 0.5); 
  41.                 
  42.                 if([self placement] == kDKOPlaceAnnotationBetweenGrid )
  43.                         bbox.origin.y += sd * 0.5;
  44.                 
  45.                 // don't draw if beyond end of interior
  46.                 
  47.                 if( bbox.origin.y > NSMaxY( ir ))
  48.                         return;
  49.         }
  50.         else
  51.         {
  52.                 if( edge == kDKOLeftEdge )
  53. bbox.origin.y = NSMinY( ir ) - bbox.size.height - ( textOffset * 0.7); 
  54.                 else
  55.                         bbox.origin.y = NSMaxY( ir ) + ( textOffset * 0.7 );
  56.                 
  57. bbox.origin.x = ( NSMinX( ir ) + ( sd * indx )) - (bbox.size.width * 0.5); 
  58.                 
  59.                 if([self placement] == kDKOPlaceAnnotationBetweenGrid )
  60.                         bbox.origin.x += sd * 0.5;
  61.                 
  62.                 if( bbox.origin.x > NSMaxX( ir ))
  63.                         return;
  64.         }
  65.         
  66.         // draw the text in the box
  67.         
  68.         if( NSIntersectsRect( bbox, aRect ))
  69.         {
  70.                 NSPoint         origin = bbox.origin;
  71.                 
  72.                 [NSGraphicsContext saveGraphicsState];
  73.                 
  74.                 if([self isRotated])
  75.                 {
  76.                         NSAffineTransform* tfm = [NSAffineTransform transform];
  77.                         
  78.                         if([self orientationIsVertical])
  79.                         {
  80.                                 origin.x = NSMaxX( bbox );
  81.                                 [tfm translateXBy:origin.x yBy:origin.y];
  82.                                 [tfm rotateByDegrees:90];
  83.                                 [tfm translateXBy:-origin.x yBy:-origin.y];
  84.                         }
  85.                         else
  86.                         {
  87.                                 origin.y = NSMaxY( bbox );
  88.                                 [tfm translateXBy:origin.x yBy:origin.y];
  89.                                 [tfm rotateByDegrees:-90];
  90.                                 [tfm translateXBy:-origin.x yBy:-origin.y];
  91.                         }
  92.                         [tfm concat];
  93.                 }
  94.                 
  95.                 [std drawAtPoint:origin];
  96.                 [NSGraphicsContext restoreGraphicsState];
  97.         }
  98. }
    我想要做的是基本的,旋转的形象在NSView 。找不到任何例子或者信息是很简单对我来说,把握好这一点。我认为这必须做的NSAffineTransform 。最接近我可以以我个人的是运用变换和获得的图像旋转,但我只得到右上角的图片显示/旋转。

NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:0.5f * [image width] yBy:0.5f * [image height]];
[transform rotateByDegrees:45.0f];
[transform translateXBy:-0.5f * [image width] yBy:-0.5f * [image height]];
[NSGraphicsContext saveGraphicsState];
[transform concat];
[image compositeToPoint:NSMakePoint(0.0f, 0.0f) operation:NSCompositeSourceOver];
[NSGraphicsContext restoreGraphicsState];

这个工程非常不错。我变化一下,下面的任何人都感兴趣。

NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:0.5f * [image width] yBy:0.5f * [image height]];
[transform rotateByDegrees:45.0f];
[transform translateXBy:-0.5f * [image width] yBy:-0.5f * [image height]];
[NSGraphicsContext saveGraphicsState];
[transform concat];


//[image compositeToPoint:NSMakePoint(0.0f, 0.0f) operation:NSCompositeSourceOver];
[image drawAtPoint:NSMakePoint(0.0f, 0.0f) fromRect:[self bounds] operation:NSCompositeSourceOver fraction:1];
[NSGraphicsContext restoreGraphicsState];

 

  1. #import <Cocoa/Cocoa.h>
  2. @interface UFOView : NSView {
  3.     float angle;
  4.     NSBezierPath *path; 
  5. }
  6. @end
  7. //helper - gives center of rectangle
  8. static NSPoint MidRect(NSRect rect) { return NSMakePoint(NSMidX(rect), NSMidY(rect)); }
  9. @implementation UFOView
  10. - (void)awakeFromNib {
  11.     path = [[NSBezierPath bezierPath] retain]; //cache to save recreating
  12.     [path moveToPoint:NSMakePoint(-22, -30)];
  13.     [path lineToPoint:NSMakePoint(-2, 30)];
  14.     [path lineToPoint:NSMakePoint(28, -30)];
  15.     [path lineToPoint:NSMakePoint(-35, 8)];
  16.     [path lineToPoint:NSMakePoint(35, 8)];
  17.     [path closePath];
  18.     [NSTimer scheduledTimerWithTimeInterval:1.0/24 target:self selector:@selector(singleStep:) userInfo:nil repeats:YES];
  19. }
  20. - (void)dealloc {
  21.     [path release];
  22.     [super dealloc];
  23. }
  24. - (void)singleStep:(NSTimer *)timer {
  25.     angle += 5.0;
  26.     [self setNeedsDisplay:YES];
  27. }
  28. - (void)drawRect:(NSRect)rect { // unless you specify opaque it clears the view for you
  29.     NSPoint pathCenter  = MidRect([path bounds]); // could cache this too...
  30.     NSPoint viewCenter = MidRect([self bounds]);
  31.     
  32.     NSAffineTransform *transform = [NSAffineTransform transform]; // it will autorelease
  33.     [transform translateXBy:viewCenter.x yBy:viewCenter.y];
  34.     [transform rotateByDegrees:angle];
  35.     [transform translateXBy:-pathCenter.x yBy:-pathCenter.y];
  36.     // alternatively to creating a new path is to [transform concat] and just draw...
  37.     NSBezierPath *drawPath = [transform transformBezierPath:path]; // it will autorelease
  38.     [[NSColor lightGrayColor] setFill];
  39.     [[NSColor blackColor] setStroke];
  40.     [drawPath fill];
  41.     [drawPath stroke];
  42. }
  43. @end

 

 

 

 

你可能感兴趣的:(timer,image,Path)