Associate CGAffineTransformRotation to CGAffineTransformTranslation - iphone

I am trying to rotate a custom view (VIEWA) added on a parent view based on touch gesture using CGAffineTransformRotate. Everything is working fine. Now, I have another view(VIEWB) added on the parent view which should follow the path traced by a corner of the VIEWA while being rotated.
What i did was to calculate the new coordinates of the VIEWB from the VIEWA transformation matrix and translated the subview. i.e
VIEWA.transform = CGAffineTransformRotate(startTransform, -angleDifference+M_PI_2);
CGFloat cosa = VIEWA.transform.a;
CGFloat msinb = VIEWA.transform.b;
CGFloat sinc = VIEWA.transform.c;
CGFloat cosd = VIEWA.transform.d;
CGFloat newX = VIEWB.center.x * cosa + VIEWB.center.y * msinb;
CGFloat newY = VIEWB.center.x * sinc + VIEWB.center.y * cosd;
CGFloat xdiff = newX - VIEWB.center.x;
CGFloat ydiff = newY - VIEWB.center.y;
VIEWB.transform = CGAffineTransformTranslate(VIEWB.transform, xdiff, ydiff);
But i could not get what i wanted. Can somebody help me ?
Update:
This is what I'm trying to do :(red dot is A and black popup is B):

I assume you rotate view A but the red circle stays at a fixed position in view A's coordinate system. Thus the center of the red circle, in view A's coordinate system, should be something like easy to compute, like this:
CGPoint redCircleCenterInViewA = CGPointMake(CGRectGetMidX(viewA.bounds), 12);
You can simply convert that point to the parent view's coordinate system, like this:
CGPoint redCircleCenterInParentView = [viewA convertPoint:redCircleCenterInViewA
toView:viewA.superview];
Then you can set view B's center to that point, minus half of view B's height:
viewB.center = CGPointMake(redCircleCenterInParentView.x,
redCircleCenterInParentView.y - viewB.frame.size.height / 2);
UPDATE
I see that you actually have layer A, not view A. That only requires a slight modification. The message you send to a layer to convert coordinates is slightly different than the message you send to a view.
CALayer *layerA = ...;
UIView *parentView = ...;
UIView *viewB = ...;
CGPoint redCircleCenterInLayerA = CGPointMake(CGRectGetMidX(layerA.bounds), 12);
CGPoint redCircleCenterInParentView = [layerA convertPoint:redCircleCenterInLayerA
toLayer:parentView.layer];
viewB.center = CGPointMake(redCircleCenterInParentView.x,
redCircleCenterInParentView.y - viewB.frame.size.height / 2);
Note that there are also convertPoint:fromLayer: and convertPoint:fromView: messages. Don't let Xcode autocomplete the wrong one or you will be scratching your head!

Related

Setting TextLabel Position Objective-c

I have a Scroll View with many objects in it,since i cant edit the position on scrollview in InterfaceBuilder,how i set the position of the object programmactily?
Something like:
myTextField.x = 50;
myTextField.y = 540;
Use the frame property to set it relative to the superview's coordinates like this:
myTextLabel.frame = CGRectMake(x, y, width, height);
There is no textlabel but I assume you mean either UITextField or UILabel, either way both inherit from UIView and have the frame property.
You could also move it according to the object's center.
float x = 220; //Change This Number
float y = 180; //Change This Number
myTextField.center = CGPointMake(x,y);

how to rotate a UIView to face a point

I have a UIView that I want to rotate when I move my finger across the screen (like in a circle). I want the UIView to rotate so that it faces my touchpoint. I then want to shoot something from my UIView (like a bullet) in the direction that the UIView is facing. Any Ideas???
Glad I remember triginometry
-(void)degreesToRotateObjectWithPosition:(CGPoint)objPos andTouchPoint:(CGPoint)touchPoint{
float dX = touchPoint.x-objPos.x; // distance along X
float dY = touchPoint.y-objPos.y; // distance along Y
float radians = atan2(dY, dX); // tan = opp / adj
//Now we have to convert radians to degrees:
float degrees = radians*M_PI/360;
return degrees;
}
Once you have your nice method, just do this:
CGAffineTransform current = view.transform;
[view setTransform:CGAffineTransformRotate(current, [view degreesTorotateObjectWithPosition:view.frame.origin andTouchPoint:[touch locationInView:parentView]]
//Note: parentView = The view that your object to rotate is sitting in.
This is pretty much all the code that you'll need.The math is right, but I'm not sure about the setTransform stuff. I'm at school writing this in a browser. You should be able to figure it out from here.
Good luck,
Aurum Aquila

How can I change the underlying points coordinate space of the iPad in such a way, that 1 point equals 2 pixels?

How can I change the underlying points coordinate space of the iPad in such a way, that 1 point equals 2 pixels?
Apply a CGAffineTransformScale to whatever view you are using:
myView.transform = CGAffineTransformScale(myView.transform, 2.0, 2.0);
Here is a great blog post on the use of transforms:
http://iphonedevelopment.blogspot.com/2008/10/demystifying-cgaffinetransform.html
hum!
static __inline__ CGPoint CGPointMake2(CGFloat x, CGFloat y)
{
CGPoint p;
p.x = x*2; // twice the pixels.
p.y = y*2;
return p;
}

Scaling custom draw code for different iOS resolutions

I am struggling to get my custom drawing code to render at the proper scale for all iOS devices, i.e., older iPhones, those with retina displays and the iPad.
I have a subclass of UIView that has a custom class that displays a vector graphic. It has a scale property that I can set. I do the scaling in initWithCoder when the UIView loads and I first instantiate the vector graphic. This UIView is shown when the user taps a button on the home screen.
At first I tried this:
screenScaleFactor = 1.0;
if ([UIScreen instancesRespondToSelector:#selector(scale)]) {
screenScaleFactor = [[UIScreen mainScreen] scale];
}
// and then I multiply stuff by screenScale
... which worked for going between normal iPhones and retina iPhones, but chokes on the iPad. As I said, you can get to the UIView at issue by tapping a button on the home screen. When run on the iPad, if you display the UIView when at 1X, it works, but at 2X I get a vector graphic that twice as big as it should be.
So I tried this instead:
UPDATE: This block is the one that's right. (with the corrected spelling, of course!)
screenScaleFactor = 1.0;
if ([self respondsToSelector:#selector(contentScaleFactor)]) { //EDIT: corrected misspellng.
screenScaleFactor = (float)self.contentScaleFactor;
}
// again multiplying stuff by screenScale
Which works at both 1X and 2X on the iPad and on the older iPhones, but on a retina display, the vector graphic is half the size it should be.
In the first case, I query the UIScreen for its scale property and in the second case, I'm asking the parent view of the vector graphic for its contentsScaleFactor. Neither of these seem to get me where I want for all cases.
Any suggestions?
UPDATE:
Here's the method in my subclassed UIView (it's called a GaugeView):
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGAffineTransform t0 = CGContextGetCTM(context);
t0 = CGAffineTransformInvert(t0);
CGContextConcatCTM(context, t0);
[needle updateBox];
[needle draw: context];
}
needle is of class VectorSprite which is a subclass of Sprite which is subclassed from NSObject. These are from a programming book I'm working through. needle has the scale property that I set.
updateBox comes from Sprite and looks like this:
- (void) updateBox {
CGFloat w = width*scale;
CGFloat h = height*scale;
CGFloat w2 = w*0.5;
CGFloat h2 = h*0.5;
CGPoint origin = box.origin;
CGSize bsize = box.size;
CGFloat left = -kScreenHeight*0.5;
CGFloat right = -left;
CGFloat top = kScreenWidth*0.5;
CGFloat bottom = -top;
offScreen = NO;
if (wrap) {
if ((x+w2) < left) x = right + w2;
else if ((x-w2) > right) x = left - w2;
else if ((y+h2) < bottom) y = top + h2;
else if ((y-h2) > top) y = bottom - h2;
}
else {
offScreen =
((x+w2) < left) ||
((x-w2) > right) ||
((y+h2) < bottom) ||
((y-h2) > top);
}
origin.x = x-w2*scale;
origin.y = y-h2*scale;
bsize.width = w;
bsize.height = h;
box.origin = origin;
box.size = bsize;
}
Sprite also has the draw and drawBody methods which are:
- (void) draw: (CGContextRef) context {
CGContextSaveGState(context);
// Position the sprite
CGAffineTransform t = CGAffineTransformIdentity;
t = CGAffineTransformTranslate(t,x,y);
t = CGAffineTransformRotate(t,rotation);
t = CGAffineTransformScale(t,scale,scale);
CGContextConcatCTM(context, t);
// draw sprite body
[self drawBody: context];
CGContextRestoreGState(context);
}
- (void) drawBody: (CGContextRef) context {
// Draw your sprite here, centered
// on (x,y)
// As an example, we draw a filled white circle
if (alpha < 0.05) return;
CGContextBeginPath(context);
CGContextSetRGBFillColor(context, r,g,b,alpha);
CGContextAddEllipseInRect(context, CGRectMake(-width/2,-height/2,width,height));
CGContextClosePath(context);
CGContextDrawPath(context,kCGPathFill);
}
How, exactly, are you rendering the graphic?
This should be handled automatically in drawRect: (the context you get should be already 2x). This should also be handled automatically with UIGraphicsBeginImageContextWithOptions(size,NO,0); if available (if you need to fall back to UIGraphicsBeginImageContext(), assume a scale of 1). You shouldn't need to worry about it unless you're drawing the bitmap yourself somehow.
You could try something like self.contentScaleFactor = [[UIScreen mainScreen] scale], with appropriate checks first (this might mean if you display it in an iPad at 2x, you'll get high-res graphics).
Fundamentally, there's not much difference between an iPad in 2x mode and a "retina display", except that the iPad can switch between 1x and 2x.
Finally, there's a typo: #selector(contentsScaleFactor) has an extra s.

iPhone position of a subview?

I add a view dynamically, but when it appears on the form, it's in the upper left hand corner.
Where do I set the X and Y of the new subview?
You should set the frame property:
myView.frame = CGRectMake(10,10,200,100);
This will position the view at location (10,10), with a size of 200x100
Agreed.
Note that you cannot change coordinates individually, as you might expect. That is, this doesn't work:
self.mysubview.frame.origin.x += 17; // FAILS! Not assignable!!
If it was a pain to calculate all the other coordinates you need, you can (a) suck it up or (b) do something like the following:
CGRect theFrame = self.mysubview.frame;
theFrame.origin.x += 17;
self.mysubview.frame = theFrame; // this is legal