Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS rotate view around an anchor point and follow touch

Tags:

ios

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?

like image 871
jmcopeland Avatar asked Dec 31 '25 11:12

jmcopeland


1 Answers

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.

like image 193
Stuart Avatar answered Jan 03 '26 03:01

Stuart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!