About transform and CGAffineTransformMakeRotation

Discussion in 'iPhone/iPad Programming' started by mikezang, Jan 14, 2011.

  1. mikezang, Jan 14, 2011
    Last edited: Jan 14, 2011

    macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I am using code as below to rotate a UIView for 90 degrees, when I want to rotate 90 degrees again from last position and use the same code, nothing happened.
    Code:
    view.transform = CGAffineTransformMakeRotation(M_PI / 2);
    
    But when I change to below, it rotate 90 degree from last position, I want to know why, I checked Programming Guide and Class Reference about UIView, CGContext and CGAffineTransform, but still not clear, hope you can answer my question.
    Code:
    view.transform = CGAffineTransformMakeRotation(M_PI);
    
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Setting the transform is not cumulative.

    Code:
    view.transform = CGAffineTransformMakeRotation(M_PI);
    
    sets the transform M_PI

    Code:
    view.transform = CGAffineTransformMakeRotation(M_PI);
    view.transform = CGAffineTransformMakeRotation(M_PI);
    
    sets the transform to M_PI and then sets it to M_PI again. Which is clearly redundant.

    It's just a property. It's never going to set it to 2*M_PI. Why would you expect it to work any differently?
     
  3. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    Sorry I made mistake, the first code os M_PI / 2, then is M_PI.

    I use that code to animate my view, first view rotate 90 degrees in animation, so I think if I animated two times, view should rotate 180 degrees from original position, and rotate 90 degrees from position after first rotation!

    But View only rotate 90 degrees even if I run same code more than two times, then I change code to M_PI after first rotation, this time view rotate more 90 degrees, now I got what I need.

    The problem is why I have to set 180 degrees in second time, can you answer me?
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I repeat: you are setting a property. When you set the rotation to 90º that's what happens. If you set it to 90º again it doesn't suddenly become 180º. You are not calling a command that rotates by an amount. You are setting it to a specific amount.

    It's just as if you were setting an integer. If you set a property to 10 and then set it to 10 again you'd be really surprised and upset if the property value was 20.
     
  5. mikezang, Jan 14, 2011
    Last edited: Jan 14, 2011

    thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #5
    Well, I understood. thank you very much.

    So can you tell me how I can let view rotate 90 in real, so that I can rotate 90 again let it in 180?

    Maybe use CGContextRotateCTM?

    I think CGAffineTransformRotate might be what I need, I will try after a few hours, as I have to sleep now, I am sit here since yesterday 1800.
     

Share This Page