Saurabh Mishra
Saurabh Mishra

Reputation: 881

Rotating a ImageView having one end fixed in iOS

I am developing a Ui which has a look & feel like a speedometer of vehicle which has a needle which is fixed from one end & rotate with some angle with that fixed end. I have done some sort of code for that

self.SIV_Needle.transform =CGAffineTransformMakeRotation((CGFloat(M_PI_2)/180.0) * 80)

Its working but the imageView loss its original x & y coordinates from one end.I have tried lot of things like changing anchor point , center but doesn't seems to work well.I want to move that needle

Upvotes: 2

Views: 1131

Answers (4)

Max von Hippel
Max von Hippel

Reputation: 2970

Responding to my comment, OP said the issue was that the control was not rotating around the desired axis. OP was not (I think) clear about what the desired axis is, nor about what the current axis is (I am using axis interchangeably to mean pivot point). There are a few potential fixes.

1) This needle thing is an ImageView, so you could do a simple hack and just make your image bigger or smaller to fix the rotation. To explain: if your needle is rotating around the center of the needle and not the ball part, then simply double the width of the image, such that the needle only takes up the right side and thus the "ball" of the needle is the pivot point.

2) Set the anchor property of the transform to wherever you want it to be rotating around. See this similar SO question & answer.

Upvotes: 2

Shoaib
Shoaib

Reputation: 2294

You can archive this by putting your pin image in a UIView at desired position and rotating your main UIView.

like;

let myView:UIIView = UIIView(); //set frame of UIView
let pinImgView:UIVImageView(); // set pin image and frame of required position
//--
myView.addSubview(pinImgView);
//--
myView.transform =CGAffineTransformMakeRotation((CGFloat(M_PI_2)/180.0) * 80);

I have made a sample project for this. you can take idea from it; http://www.filedropper.com/stackover or http://wikisend.com/download/263926/StackOver.zip

Upvotes: 0

user2435304
user2435304

Reputation:

Try to change the anchor point of the needle imageView and then add the Rotation. I have not tried it, but i think it should work.

Upvotes: 0

Tony Thomas
Tony Thomas

Reputation: 1005

You need to translate the UI , to compensate the position change comes due to roatation

Upvotes: 0

Related Questions