Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Rotating a UIView or UIImage without Animation

ebarmesebarmes Posts: 13Registered Users
So far I have...

myClass.h
@interface myClass : UIView {
UIImage* image;
}

-(id)initWithFrame:(CGRect)frame;

@end



myClass.m
- (id)initWithFrame:(CGRect)frame {


UIImage* loadedImage = [UIImage imageNamed:@aPic.png];
CGRect rect = CGRectMake(frame.origin.x, frame.origin.y,
loadedImage.size.width,
loadedImage.size.height);


self = [super initWithFrame:rect];
image = [loadedImage retain];

self.opaque = NO;
self.backgroundColor = [UIColor clearColor];


return self;
}


- (void)drawRect:(CGRect)rect{


[image drawInRect:rect];
}


Is there a way to 1) create an instance of myClass then rotate it 45 degrees before adding it to a superview, 2) rotate just the image property 45 before adding to superview, 3) rotate the image property before drawing it in the rect, or 4) rotate the loadedImage before passing it to the image property?
Post edited by ebarmes on
·

Replies

  • missing_nomissing_no Posts: 41Registered Users
    edited February 2011
    CGAffineTransforms are what you are looking for and this is one of the rare cases where the Cocoa documetation is actually useful:
    CGAFFINETRANSFORMS doc

    and there is also this great tutorial using Affine transforms:
    http://iphonedevelopment.blogspot.com/2008/10/demystifying-cgaffinetransform.html


    From the documentation:
    CGAffineTransform CGAffineTransformRotate (
    CGAffineTransform t,
    CGFloat angle
    );

    so say your UIimageView is called "thingy", Make it rotate 90deg

    thingy.transform = CGAffineTransformRotate (
    thingy.transform,
    M_PI/2); // Pi/2 is 90 degrees remember this?

    or for scaling:
    CGAffineTransform CGAffineTransformScale (
    CGAffineTransform t,
    CGFloat sx,
    CGFloat sy
    );

    so say your UIimageView is called "thingy", to make it face the oppisite direction (right to left) you would
    thingy.transform = CGAffineTransformScale (
    thingy.transform,
    -1,
    1);

    they use matrix transformations (hey school really was important) if this doesn't help hit this thread again I'll see what I can do.
    ·
  • ebarmesebarmes Posts: 13Registered Users
    edited February 2011
    Thanks bro,
    I think my problem was I was trying to use a UIImage which apparently doesnt have a .transform property. So i switched over to a UIImageView instead, now it works like a charm using your "thingy" example, just swapping in my ImageView's name.
    ·
  • missing_nomissing_no Posts: 41Registered Users
    edited February 2011
    ebarmes wrote: »
    Thanks bro,
    I think my problem was I was trying to use a UIImage which apparently doesnt have a .transform property. So i switched over to a UIImageView instead, now it works like a charm using your "thingy" example, just swapping in my ImageView's name.

    No problem brotato. It's what we are all here for; it's like a neverending "lessons learned" forum.
    ·
  • wiegerthefarmerwiegerthefarmer Posts: 3New Users
    edited March 2011
    missing_no wrote: »
    No problem brotato. It's what we are all here for; it's like a neverending "lessons learned" forum.

    It's been to long since I've been in school to remember the details of matrix transformation! :) I've found a whole bunch of examples...I'll post them in a sec about making the square image into a parallelogram... but I can't seem to get it working.

    Basically I need to make the vertical and horizontal parallel "lines" of the photo further apart or closer together...

    Thanks for any help! Getting the links now...

    Aaron (wiegerthefarmer)
    ·
  • wiegerthefarmerwiegerthefarmer Posts: 3New Users
    edited March 2011
    here's the links...
    iPhone image stretching (skew) - Stack Overflow
    iphone - How do I create and apply the image skew transform that I have calculated? - Stack Overflow
    iphone - Z direction UIview rotating? - Stack Overflow
    tumbljack
    I'm heading up to get a coffee...5am...can't sleep... and I'll see if I can piece this together...

    Any help or links would be wonderful. The answer is always out there...but not enough hours to google And code magic!!

    <edit> Looks like I might have found my solution...
    iphone - UIView perspective transform - Stack Overflow

    This
    UIView *myView = self.view;
        CALayer *layer = myView.layer;
        CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
        rotationAndPerspectiveTransform.m34 = 1.0 / -500;
        rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 45.0f * M_PI / 180.0f, 0.0f, 1.0f, 0.0f);
        layer.transform = rotationAndPerspectiveTransform;
    
    ·
  • OstricideOstricide Posts: 2New Users
    edited March 2011
    here's the links...
    iPhone image stretching (skew) - Stack Overflow
    iphone - How do I create and apply the image skew transform that I have calculated? - Stack Overflow
    iphone - Z direction UIview rotating? - Stack Overflow
    tumbljack
    I'm heading up to get a coffee...5am...can't sleep... and I'll see if I can piece this together...

    Any help or links would be wonderful. The answer is always out there...but not enough hours to google And code magic!!

    <edit> Looks like I might have found my solution...
    iphone - UIView perspective transform - Stack Overflow

    This
    UIView *myView = self.view;
        CALayer *layer = myView.layer;
        CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
        rotationAndPerspectiveTransform.m34 = 1.0 / -500;
        rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 45.0f * M_PI / 180.0f, 0.0f, 1.0f, 0.0f);
        layer.transform = rotationAndPerspectiveTransform;
    

    I have been working on something similar and managed to get an image to rotate in response to the accelerometer. It was not simple or I would just reproduce my steps but I can tell you that you are on the right track.

    the end result of all the setup was this little chunk of code.

    CATransform3D rotationTransform = CATransform3DIdentity;
    rotationTransform = CATransform3DRotate(rotationTransform, bubbleRotation, 0, 0, 1);
    bubbleView.layer.transform = rotationTransform;

    I think when your trying to change perspective its best to do it seperately. this makes it easier to keep track of the coords.

    Hope this is helpful!
    :cool:
    ·
Sign In or Register to comment.