• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: rotating an object around the center of a view
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: rotating an object around the center of a view


  • Subject: Re: rotating an object around the center of a view
  • From: Erik Buck <email@hidden>
  • Date: Sun, 4 Mar 2007 11:05:58 -0500

To rotate about some point, translate to that point, rotate, and then translate back. The approach is not Cocoa specific and is covered in any introductory graphics text book.

Here is some Cocoa code as a sample

/* MyRotateView */

#import <Cocoa/Cocoa.h>

@interface MyRotateView : NSView
{
   float       rotationAngleDeg;
}

- (IBAction)takeRotationAngleDegFrom:(id)sender;

@end


#import "MyRotateView.h"

@implementation MyRotateView


- (void)drawRect:(NSRect)rect
{
NSPoint centerOfView = NSMakePoint(NSMidX([self bounds]), NSMidY([self bounds]));
float inscribedCircleRadius = 0.5f * MIN([self bounds].size.width,
[self bounds].size.height);
NSRect pivotPointIndicatorRect = NSMakeRect (centerOfView.x - 10.0f,
centerOfView.y - 5.0f, 20.0f, 10.0f);
NSAffineTransform *translateTransform = [NSAffineTransform transform];


   {  // draw something not rotated
      // erase past drawing
      [[NSColor whiteColor] set];
      NSRectFill([self bounds]);

      NSBezierPath      *path = [NSBezierPath bezierPath];

[[NSColor grayColor] set];
[path moveToPoint:NSMakePoint(centerOfView.x - inscribedCircleRadius, centerOfView.y)];
[path lineToPoint:NSMakePoint(centerOfView.x + inscribedCircleRadius, centerOfView.y)];
[path moveToPoint:NSMakePoint(centerOfView.x, centerOfView.y - inscribedCircleRadius)];
[path lineToPoint:NSMakePoint(centerOfView.x, centerOfView.y + inscribedCircleRadius)];
[path stroke];
}



[NSGraphicsContext saveGraphicsState]; // Save the state

[translateTransform translateXBy:centerOfView.x yBy:centerOfView.y]; // Trans to center of rotation
[translateTransform rotateByDegrees:rotationAngleDeg]; // Rotate
[translateTransform translateXBy:-centerOfView.x yBy:- centerOfView.y];// Translate back
[translateTransform concat];


{ // draw something rotated
NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect:pivotPointIndicatorRect];


[[NSColor greenColor] set];
[path moveToPoint:NSMakePoint(centerOfView.x - 20.0f, centerOfView.y)];
[path lineToPoint:NSMakePoint(centerOfView.x + inscribedCircleRadius, centerOfView.y)];
[path stroke];
}


   [NSGraphicsContext restoreGraphicsState];       // Restore the state

{ // draw something not rotated
NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(centerOfView.x -
inscribedCircleRadius, centerOfView.y - inscribedCircleRadius,
2.0f * inscribedCircleRadius, 2.0f * inscribedCircleRadius)];


      [[NSColor blackColor] set];
      [path stroke];
   }
}

- (IBAction)takeRotationAngleDegFrom:(id)sender
{
   rotationAngleDeg = [sender floatValue];
   [self setNeedsDisplay:YES];
}

@end

There is a sample called TransparentTetris that does this too at http://www.cocoaprogramming.net/Downloads.html

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: rotating an object around the center of a view
      • From: Ken Tozier <email@hidden>
  • Prev by Date: Re: Calculating the size of NSFont...
  • Next by Date: Re: rotating an object around the center of a view
  • Previous by thread: Re: rotating an object around the center of a view
  • Next by thread: Re: rotating an object around the center of a view
  • Index(es):
    • Date
    • Thread