Setting Origin of Animation to center of View - ios

I have an animation that I'm trying to position in the center of the view. Here's my current code set at static values:
imageViewAnimatedFrame.origin.x = 10
imageViewAnimatedFrame.origin.y = 200
imageViewAnimatedFrame.size.height = 298
imageViewAnimatedFrame.size.width = 391
I'm pretty new to Swift. Is there a way to set the origin x and y to the center subtracted by half the width and height so it lays in the center?
Thanks!

You can do something like this,
let imageViewAnimatedFrame: UIImageView = UIImageView()
imageViewAnimatedFrame.frame = CGRectMake((self.view.frame.size.width / 2) - 159.5, (self.view.frame.size.height / 2) - 149, 391, 298)
Divide view's height and width by 2 and divide your imageview's height and width by 2. then subtract half width of imageview from half width of view as position x and same for position y consider height here.
this way your imageview always be in center.
Hope this will help :)

If you have an ImageView of these dimensions:
imageViewAnimatedFrame.size.height = 298
imageViewAnimatedFrame.size.width = 391
imageViewAnimatedFrame.frame = CGRectMake((self.view.frame.size.width -
imageViewAnimatedFrame.frame.size.width) / 2,
(self.view.frame.size.height -
imageViewAnimatedFrame.frame.size.height) / 2,
imageViewAnimatedFrame.frame.size.width, imageViewAnimatedFrame.frame.size.height);

Related

how to set width and height of a UIVIew in pixels?

I am working on an ObjectiveC app in which I need to set the width and height of a uiview in pixels and than scale that uiview equal to the devices width. The width and height values in pixels are fixed ( let say 900 px x 500 px).
Currently I am doing this,
UIScreen* mainScreen = [UIScreen mainScreen];
CGRect frame = CGRectMake(0, 0, 900.0, 500.0);
[view setFrame:frame];
CGFloat valScale = mainScreen.bounds.size.width/900.0;
[view setContentScaleFactor:valScale];
But this is not giving me the desired values.
What should I do?
(P.S) Does view.frame.size.width return the width in pixels or points?
Hi for scaling a view you can use the transform property. Say you wish to scale your UIView by a factor of 2 then you can use.
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);
CGRect width and height returns points not pixels. One point has two pixels for 2x devices like iPhone 6,7 and three pixels for 3x devices like iPhone 6 Plus,7 Plus.
For more detail you can refer this Apple's documentation link.

How to Center UI View when in landscape & portrait?

I having trouble centering 2 UIImages and a Layer when Orientation changes.
here is what i did to center it
_colorLayer = [CALayer layer];
CGRect frame = self.hueImageView.frame;
CGSize superviewSize = self.hueImageView.superview.frame.size;
self.hueImageView.center = CGPointMake((superviewSize.width / 2), (superviewSize.height / 2));
self.checkeredView.center = CGPointMake((superviewSize.width / 2), (superviewSize.height / 2));
self.labelPreview.center = CGPointMake((superviewSize.width / 2), (superviewSize.height / 2));
this works fine in portrait. but when i rotate the images do not go to the middle .
the project is on my github
https://github.com/RK905/NewColorPic/blob/master/NewColorPic/NEOColorPickerHSLViewController.m
just set the center from your superView to all other views. superView.center should do it. No fights with height/2 and width/2 .
If you would use constraints, you will not need to set it again. Just align the views to the center.y and center.x of the superView. No fight with orientations because the constrain center will automaticly move also

Xcode 6 UIView set width to zero causes height equals zero as well

I have next code below:
- (IBAction)onTappedAddValue:(id)sender
{
CGRect rect = self.progressView.frame;
rect.size.width = rect.size.width + 1;
self.progressView.frame = rect;
NSLog(#"%#", [NSValue valueWithCGRect:self.progressView.frame]);
}
by pressing on the button I resize my view, so everything works good if view has for example rect (0, 0, 100, 10);
but if I set width to the 0 in storyboard and heigh to 10 points, by some reason SDK thinks that height also should equal 0 after pressing button and invocation code above.
Please check this video and sources if you want to see how it works. When I set UIView's width to zero in storyboard and then try to increment it by pressing button, the width is incrementing but by some reason height is equal to zero but in stroryboard it has 10 pt.

How to set a UIView's origin reference?

I am creating a UIImageView and adding it in a loop to my view, I set the initial frame to 0,0,1,47 and each passage of the loop I change the center of the image view to space them out.
I am always using 0 as the origin.y
The problem is the origin reference is in the centre of the image view, assuming we was in interface builder, this is equivalent to the image below.
How can I change the reference point in code ?
After reading these answers and your comments I'm not really sure what is your point.
With UIView you can set position by 2 ways:
center – It definitely says it is the center.
frame.origin – Top left corner, can't be set directly.
If you want the bottom left corner to be at x=300, y=300 you can just do this:
UIView *view = ...
CGRect frame = view.frame;
frame.origin.x = 300 - frame.size.width;
frame.origin.y = 300 - frame.size.height;
view.frame = frame;
But if you go one level deeper to magical world of CALayers (don' forget to import QuartzCore), you are more powerful.
CALayer has these:
position – You see, it don't explicitely says 'center', so it may not be center!
anchorPoint – CGPoint with values in range 0..1 (including) that specifies point inside the view. Default is x=0.5, y=0.5 which means 'center' (and -[UIView center] assumes this value). You may set it to any other value and the position property will be applied to that point.
Example time:
You have a view with size 100x100
view.layer.anchorPoint = CGPointMake(1, 1);
view.layer.position = CGPointMake(300, 300);
Top left corner of the view is at x=200, y=200 and its bottom right corner is at x=300, y=300.
Note: When you rotate the layer/view it will be rotated around the anchorPoint, that is the center by default.
Bu since you just ask HOW to do specific thing and not WHAT you want to achieve, I can't help you any further now.
The object's frame includes its position in its superview. You can change it with something like:
CGRect frame = self.imageView.frame;
frame.origin.y = 0.0f;
self.imageView.frame = frame;
If I am understanding you correctly, you need to set the frame of the image view you are interested in moving. This can be done in the simple case like this:
_theImageView.frame = CGRectMake(x, y, width, height);
Obviously you need to set x, y, width, and height yourself. Please also be aware that a view's frame is in reference to its parent view. So, if you have a view that is in the top left corner (x = 0, y = 0), and is 320 points wide and 400 points tall, and you set the frame of the image view to be (10, 50, 100, 50) and then add it as a subview of the previous view, it will sit at x = 10, y = 50 of the parent view's coordinate space, even though the bounds of the image view are x = 0, y = 0. Bounds are in reference to the view itself, frame is in reference to the parent.
So, in your scenario, your code might look something like the following:
CGRect currentFrame = _theImageView.frame;
currentFrame.origin.x = 0;
currentFrame.origin.y = 0;
_theImageView.frame = currentFrame;
[_parentView addSubview:_theImageView];
Alternatively, you can say:
CGRect currentFrame = _theImageView.frame;
_theImageView.frame = CGRectMake(0, 0, currentFrame.size.width, currentFrame.size.height);
[_parentView addSubview:_theImageView];
Either approach will set the image view to the top left of the parent you add it to.
I thought I would take a cut at this in Swift.
If one would like to set a views position on the screen by specifying the coordinates to an origin point in X and Y for that view, with a little math, we can figure out where the center of the view needs to be in order for the origin of the frame to be located as desired.
This extension uses the frame of the view to get the width and height.
The equation to calculate the new center is almost trivial. See the below extension :
extension CGRect {
// Created 12/16/2020 by Michael Kucinski for anyone to reuse as desired
func getCenterWhichPlacesFrameOriginAtSpecified_X_and_Y_Coordinates(x_Position: CGFloat, y_Position: CGFloat) -> CGPoint
{
// self is the CGRect
let widthDividedBy2 = self.width / 2
let heightDividedBy2 = self.height / 2
// Calculate where the center needs to be to place the origin at the specified x and y position
let desiredCenter_X = x_Position + widthDividedBy2
let desiredCenter_Y = y_Position + heightDividedBy2
let calculatedCenter : CGPoint = CGPoint(x: desiredCenter_X, y: desiredCenter_Y)
return calculatedCenter // Using this point as the center will place the origin at the specified X and Y coordinates
}
}
Usage as shown below to place the origin in the upper left corner area, 25 pixels in :
// Set the origin for this object at the values specified
maskChoosingSlider.center = maskChoosingSlider.frame.getCenterWhichPlacesFrameOriginAtSpecified_X_and_Y_Coordinates(x_Position: 25, y_Position: 25)
If you want to pass a CGPoint into the extension instead of X and Y coordinates, that's an easy change you can make on your own.

iOS layout images dynamically

I'm trying to find a rational solution for a layout problem in iOS.
I need to load n images in a view with this layout:
-----------
| x x |
| x x |
| x x |
| x x |
| x x |
| x x |
-----------
Each image may have a different width, but the height has a maximum and each image is to be centered in it's column.
What is the suggested approach?
I'm thinking of putting views with 50% width side by side and then center the images in them.
Thanks!
This should get you started. It does not, however, take into consideration the ratio of the images.
CGRect bounds = [[UIScreen mainScreen] bounds];
CGFloat maxCol = [imageViewArray count] + ([imageViewArray count] % 2);
CGFloat width = (bounds.size.width/2) - 15;
CGFloat height = (bounds.size.height/maxCol) - 10;
// loop through imageViews
for(pos = 0; pos < [imageViewArray count]; pos++)
{
// calculate position within grid
CGFloat row = (pos/2);
CGFloat col = (pos%2);
// retrieves UIImageView and UIImage
imageView = [imageViewArray objectAtIndex:pos];
image = imageView.image;
// calculates image size
CGFloat scaledWidth = (height * image.size.width) / image.size.height;
// adjust size of image view
imageView.frame = CGRectMake(0, // temp value for x
0, // temp value for y
height, // width of image
scaledWidth); // height of image
// adjust position of image view
imageView.center = CGPointMake((bounds.size.width/3) + ((bounds.size.width/3) * col),
((height+10) * row) + (height+10)/2));
// add image view to super view
[rootView addSubView:imageView];
};
The above scaling assumes that the height of the grid location is always smaller than the width of the grid location.
If you will load a lot of images it will cause performance problems. Better way to do this - use UITableView with custom cells.
Looks like the same problem a text engine has when rendering glyphs. What you want to do is to first calculate the width for a row before you do anything else. This is done by looping from left to right, adding the sizes of the views until you get to the edge and can't add more. Then you know the width and the maximum height for that row, and simply center the views by adding an offset to the left equal to the margin that was left to the right divided by two. And then add an offset to the y coordinate by the maximum height for that row.
Then simply keep doing that for all views until they are all have a position.

Resources