I'm using a custom image (which is 360 x 276 so not proportional) and I'm rotating it with an animation.
The anchor point is not just (0,.5) or (1,0), it's something like (.23423, .912314). Is there any way to show, where the anchor point currently is? Or setting it in InterfaceBuilder? Currently I'm just trying to reach the correct CGPoint by testing different values, but I didn't get the perfect one.
You cannot simple set it in IB, unfortunately. In your case, I would use some image processing application to find out at what coordinate you need the image to rotate and then do the math:
84,3228 / 360 = 0.24423
Not necessarily brilliant, but workable I would say.
This might help.
CGPoint anchorPosition = CGPointMake(
imageView.frame.origin.x + imageView.frame.size.width*anchorPoint.x,
imageView.frame.origin.y + imageView.frame.size.height*anchorPoint.y);
You can refer to http://disanji.net/iOS_Doc/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/Layers.html
Related
So here is task i need to get done on iOS:
Show User's Current Location on an Image like this:
Yes, like this Image!
So few things one will notice as soon as one look at it. Its Tilted. It's headings are Off. You can see horizon..!
Lets say,
I have location coordinates for corners of this image.
I have angle at which this image is tilted. (e.g 50°)
I have heading angle of the image. (e.g -170° North, which means its about 10° South-West, correct me if im wrong.)
And i have Zoom level on which this image would have been zoomed if it was on Google Maps/Google Earth. (e.g 14 zoom level - out of 20)
But thats the task, i need to show users location on such images. Just like in "Google Earth".
Can anyone give me heads up to accomplish this task?
Thank you everyone. :)
Given the information you have, sounds like you can use line-plane intersection to solve your problem.
I achieved the desired result by plotting the coordinate on a UIView and transform it using CATrasnform3D. These explanations also help, Explanation of Transformation Matrix in terms of Core Animation and Core Animation Programming Guide itself.
_graphic.transform = CGAffineTransformMakeRotation(-2.89724656);
CATransform3D rotatedTransform = _graphic.layer.transform;
rotatedTransform.m34 = 1.0 / 500;
rotatedTransform.m22 = -2;
rotatedTransform.m41 = 170;
rotatedTransform.m42 = 170;
rotatedTransform =
CATransform3DRotate(rotatedTransform,
51 * M_PI / 180.0f,
1.0f,
0.0f,
0.0f);
_graphic.layer.transform = rotatedTransform;
[_graphic setNeedsDisplay];
Thank you all.
I am trying to make an app that uses a radial dial. As an example the radial dial features as availabel in this app
http://itunes.apple.com/us/app/kitchen-dial/id448558628?mt=8&ign-mpt=uo%3D4
What I wanted to ask is what method should I use to accomplish this? Guys, I am not asking you to write the code for me what I am asking is how is this done conceptually? I mean when a user swipses left/right on the screen how does it know which radial dial to move and in which direction. I am sure there are multiple buttons and there is some image transition going on here. I need to understand the concept of making such a thing. If anyone know or has done something similar please do share your thoughts. Does apple has a sample code for this? Thank you.
The central concepts are:
Ability to rotate an image around a center point.
Ability to calculate an angle relative to the center point given an x,y coordinate.
So, to move the wheel:
On touch began:
Calculate starting angle based on x,y
On touch moved:
Calculate new angle based on x,y and subtract starting angle. This is the angle delta (or how much more to rotate the image by).
To register button taps on the wheel:
On tap:
Calculate angle based on x,y, add existing rotation, divide by the angle size of each segment to get an index.
You asked for a high-level explanation of the concepts - and that's pretty much it.
This is a complicated question. The simple answer is: use a pan gesture recognizer, with:
- (void)panBegan:(UIPanGestureRecognizer *)pan {
CGRect frame = [self frame];
_central = CGPointMake(frame.origin.x+frame.size.width/2, frame.origin.y+frame.size.height/2);
CGPoint p = [pan locationInView:[self superview]];
p.x -= _central.x;
p.y -= _central.y;
_angle = atan2(p.x, p.y);
}
- (void)panMoved:(UIPanGestureRecognizer *)pan {
CGPoint p = [pan locationInView:[self superview]]];
p.x -= _central.x;
p.y -= _central.y;
CGFloat deltaAngle = _angle - atan2(p.x, p.y);
self.transform = CGAffineTransformMakeRotation(deltaAngle + _finalAngle);
}
where _angle is angle of the initial touch of the pan, and _finalAngle is saved angle at the end of the previous pan.
But: the UIPanGestureRecognizer has a velocity property, and you should be using it at the end of the pan to continue the rotation, as if the dial had inertia. And, if the dial has a fixed integer set of legal stopping positions, then you must adjust the slowdown curve so it stops on a legal stop. And, if you animate the deceleration, and the user starts a new pan before the previous one is finished, then you have to adjust _finalAngle based on the perceived position, not the stored position.
Here is a list of rotary knobs that are open source, so you can look at the code and see whats involved:
http://maniacdev.com/2011/12/open-source-libraries-for-easily-adding-rotary-knob-controls-in-your-ios-apps/
I'm trying to add a box to the centre of the screen in a zoom view. E.g. if I move into an area of the content view and try using the offset coordinates, it becomes erratic if I zoom in or out. I can't seem to figure out the right mathematical formula for this.
If you are working with a UIView or one of it's subclasses. You'll always have a center property available for you. That property is a CGPoint and you can do something like this to test if it is the required result you seek:
CGPoint center = [YourUIViewOrSubclass center];
NSLog(#"Center x is '%f' and y is '%f', center.c, center.y);
I hope this helps you. Otherwise try and rephrase your question and include a little context.
hi all upto now i know making rectangle with the CGrectmake and this rect(frame) i am using as imageview frame like UIImageView *someImage=[[uiimageview alloc]initwithframe:someRect]; now i can add an image with the frame of someRect. my problem here is when the coordinates like
(rectangleFirstx-coordinate,tectangleFirstY-cordinate)=(10,10)
(rectangleLastx-cordinate,rectangleLasty-cordinate)=(17,7) this, how can i give frame to the uiimageview....This is like a inclined rectangle..can any one suggest me how to apply frame through the ios library for these type of coordinates..Thanks in advance..
Your example isn't very clear because a rectangle with opposite corners at (10,10) and (10,7) can be in any one of a myriad of different orientations, including one perfectly aligned along the x and y axis.
What you can certainly do is create a UIImageView of the desired size and location and then rotate it by using one of many techniques, including animation methods.
[UIImageView animateWithDuration:0.1 animations:^
{
your_UIImageView_here.transform = CGAffineTransformMakeRotation((M_PI/180.0) * degrees);
}];
You can hide the UIImageView until the rotation is done and then show it.
If your question is about how to use the coordinates you provided to arrive at an angle I'd suggest that more data is needed because it is impossible to pick one of the billions of possible rectangles with corners at those two points without more information. Once you have more data then it is pretty basic trigonometry to figure out the angle to feed into the rotation.
I am using
float theAngle = atan2( location.y-self.center.y, location.x-self.center.x );
to rotate a wheel at a certain angle.
and
self.transform = CGAffineTransformMakeRotation(angleRadians);
for the transformation to take place.
But everytime the user represses the wheel to turn it, it goes back to the original location and starts from there. How may I stop this from happening please?
Thanks!
You may setting the rotation angle value of the wheel rather than adding the value to the current angle.
I would hazzard a guess that the reason is that you are calculating based on the original position of the wheel rather than the previous position of the wheel.
i.e. Presuming that self.center represents the original position of the wheel as first displayed, location represents the users desired new rotation and looking up atan2 on wikipedia (trig is not my strong point :-) I would guess you need self.location to be updated each time to the be location so that the next rotation starts from the last rotation.
Just a guess :-)