根据参考点旋转图像

Rotate an image based on reference point

我有代码可以根据图像的几何中心像 "spinner" 一样旋转我的图像...但是因为图像本身是不对称的,我希望围绕特定参考点旋转它UIImageView 的 (x,y) 坐标。 这是我当前旋转图像的代码:

self.spinnerView.center=CGPointMake(self.spinnerView.center.x, self.spinnerView.center.y);
self.spinnerView.transform=CGAffineTransformMakeRotation (angle);
// 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);

并阅读文档:

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.

所以 :

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

写了关于锚点的文章:

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.