Search code examples
iosuiimageviewxcode6image-rotation

Rotate an image based on reference point


I have the code that will rotate my image like a "spinner" based on its geometric center... however because the image itself is asymmetrical, i wish to rotate it about a particular reference point in terms of (x,y) coordinates of the UIImageView. Here is my current code that rotates the image:

self.spinnerView.center=CGPointMake(self.spinnerView.center.x, self.spinnerView.center.y);
self.spinnerView.transform=CGAffineTransformMakeRotation (angle);

Solution

  • // why u change center of View on itself??? o_O What you want with it?
    self.spinnerView.center=CGPointMake(self.spinnerView.center.x, self.spinnerView.center.y);
    

    And read a documentation:

    The origin of the transform is the value of the center property, or the layer’s anchorPoint property if it was changed. (Use the layer property to get the underlying Core Animation layer object.) The default value is CGAffineTransformIdentity.

    so :

    self.spinnerView.layer.anchorPoint=CGPointMake(0, 0);
    self.spinnerView.transform=CGAffineTransformMakeRotation(angle);
    

    was written about anchorPoint:

    You specify the value for this property using the unit coordinate space. The default value of this property is (0.5, 0.5), which represents the center of the layer’s bounds rectangle. All geometric manipulations to the view occur about the specified point.