jmcopeland
jmcopeland

Reputation: 1226

iOS rotate view around an anchor point and follow touch

I need to create a circular rating meter in iOS, it's like a UISlider only curved 3/4 around a circle. I need to rotate a UIImageView (which is the rating needle) around an anchor point, by dragging an invisible touchable handle area the will follow the tip of the needle. The angle of the needle will determine a value.

I am struggling to find an elegant solution. Any thoughts?

Upvotes: 3

Views: 11616

Answers (1)

Stuart
Stuart

Reputation: 37053

Have you tried using the anchorPoint property of the UIImageView's underlying layer? If you have a CGPoint representing the point (in your image view's coords) around which it is to rotate, you can set the layer's anchor point:

CGRect imageBounds = self.needleImageView.bounds;
[self.needleImageView.layer setAnchorPoint:CGPointMake(rotationPoint.x / imageBounds.size.width, rotationPoint.y / imageBounds.size.height)];


Ensure that the image view's center property is set to the appropriate position also, and then apply the rotation transform to rotate about the layer's anchor point:

self.needleImageView.transform = CGAffineTransformRotate(self.needleImageView.transform, angleInRadians);


Remember that you need to import <QuartzCore/QuartzCore.h> in order to access the layer's properties.

Upvotes: 6

Related Questions