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);
Related
I spend lot of time while setting the position of label, button,switch, image,cell, textfield, etc. Is there any Formula to set Co ordinates of these objects in user desired Location in xib file.
Thanks
Sample Code :
yourView.frame = CGRectMake(xval, yval, width, height);
//OR
[yourView setFrame:CGRectMake(xval, yval, width, height)];
if you want to set frame in .xib then you can find it in XCode as shown in figure below.
and if want to set it through coding then do following:
yourview.frame=CGRectMake(x, y, width, height);
hope this will help you.
Dont' forget all objects coordinate resize itself according to superview frame. If you want to change only 1 of the coordinate or size property you can use this:
CGRect frame = label.frame;
frame.size.height = 165.0f; //frame.size.width || frame.origin.x || frame.origin.y
cell.L_content.frame = frame;
otherwise if you want to do it in single line:
label = CGRectMake(xCoordinate, yCoordinate, width, height);
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!
This is quite the iPhone quandry. I am working on a library, but have narrowed down my problem to very simple code. What this code does is create a 50x50 view, applies a rotation transform of a few degrees, then shifts the frame down a few times. The result is the 50x50 view is now much larger looking.
Here's the code:
// a simple 50x50 view
UIView *redThing = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
redThing.backgroundColor = [UIColor redColor];
[self.view addSubview:redThing];
// rotate a small amount (as long as it's not 90 or 180, etc.)
redThing.transform = CGAffineTransformRotate(redThing.transform, 0.1234);
// move the view down 2 pixels
CGRect newFrame = CGRectMake(redThing.frame.origin.x, redThing.frame.origin.y + 2, redThing.frame.size.width, redThing.frame.size.height);
redThing.frame = newFrame;
// move the view down another 2 pixels
newFrame = CGRectMake(redThing.frame.origin.x, redThing.frame.origin.y + 2, redThing.frame.size.width, redThing.frame.size.height);
redThing.frame = newFrame;
// move the view down another 2 pixels
newFrame = CGRectMake(redThing.frame.origin.x, redThing.frame.origin.y + 2, redThing.frame.size.width, redThing.frame.size.height);
redThing.frame = newFrame;
// move the view down another 2 pixels
newFrame = CGRectMake(redThing.frame.origin.x, redThing.frame.origin.y + 2, redThing.frame.size.width, redThing.frame.size.height);
redThing.frame = newFrame;
So, what the heck is going on? Now, if I move the view by applying a translation transform, it works just fine. But that's not what I want to do and this should work anyway.
Any ideas?
From the UIView documentation:
If the transform property is also set, use the bounds and center properties instead; otherwise, animating changes to the frame property does not correctly reflect the actual location of the view.
Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.
In other words, I would be wary of the frame property when a transform is set.
That's the question xD
Given an instance of a CCSprite in cocos2d in iphone, what method can I use to obtain the image width and height?
The CCSprite class has a bounding box property that's a CGRect:
CCSprite *sprite = [CCSprite spriteWithFile: #"file.png"];
int width = [sprite boundingBox].size.width;
I added a width and height methods to my CCSprite subclass.
-(CGFloat) width
{
return [self boundingBox].size.width;
}
-(CGFloat) height
{
return [self boundingBox].size.height;
}
raw width:
sprite.contentSize.width
raw height:
sprite.contentSize.height
current width:
sprite.contentSize.width * sprite.scaleX
current height:
sprite.contentSize.height * sprite.scaleY
IN cocos2d-x
sprite->boundingBox().size.width;
sprite->boundingBox().size.height;
In cocos2d-x v3.x, boundingBox is deprecated in the Node class (i.e. the super class of Sprite). Use the following code instead:
auto spriteWidth = sprite->getTextureRect().size.width;
auto spriteHeight = sprite->getTextureRect().size.height;
or
auto spriteWidth = sprite->getContentSize().width;
auto spriteHeight = sprite->getContentSize().height;
Answer for 2018 (Cocos2d-x v3.x:)
The other answers are incomplete and out-of-date.
Note that I'm using JavaScript below along with destructuring assignment syntax. Be sure to view the Cocos API documentation for your language implementation.
getBoundingBox()
Gives you the:
Scaled size (the size after setScale() is applied to the sprite).
Coordinates of sprite on the screen. Note that the default anchorPoint for sprites is (0.5, 0.5), while this coordinate represents the (0, 0) position. In other words, if the anchorPoint is set at the default, then getBoundingBox().x + getBoundingBox().width / 2 = getPosition().x (the x value you set in setPosition()).
Example:
const boundingBox = sprite.getBoundingBox();
const { x, y, width, height } = boundingBox;
getContentSize()
Gives you the:
Unscaled size.
Example:
const contentSize = sprite.getContentSize();
const { x, y } = contentSize;
getTextureRect()
Gives you the:
Unscaled size.
Coordinates of sprite on the texture from which it is extracted (i.e. sprite sheet)
Example:
const textureRect = sprite.getTextureRect();
const { x, y, width, height } = textureRect;
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