Resolution Change Positioning - objective-c

I am trying to position NSRects by using NSRect mainFrame = [[[NSScreen screens] objectAtIndex:0] frame]; and setting that as my x origin for the rects like so:
outlineBox.origin.x = (mainFrame.size.width - width2) / 2.0 * (some positioning number);
width2 is just a float value.
However when I change resolution that last multiplied positioning number is way off to the new resolution frame, what can I do to solve this? Thanks!

Related

Dynamically changing size and frame position of UITextView

What I would like to do is to make a custom UITextView to allow dynamically changing size and positions of it like PicLab's text edit function.
PicLab tools article
In sum for my simple questions following
1, How to move a position of TextView at your disposal
2, How to change a size of TextView by finger slide
3, How to dynamically change size of fonts corresponding to size of TextView
That would be great if sharing me with useful links as well as codes.
to move the position of a UITextView (actually of any UI element in iOS), you can modify its frame property. the frame property defines the rectangle that this elements takes on the screen, being defined with x and y positions ( (0, 0) is the top left corner of the screen).
you can also change the size of a UITextView by modifying the frame, this time instead of modifying x and y, you'll have to modify its height and width
an example of setting a UITextField with a width of 200 and height of 50 would look like in the top left corner of the screen looks like:
CGFloat x = 0.0;
CGFloat y = 0.0;
CGFloat width = 2000.0;
CGFloat height = 50.0;
textField.frame = CGRectMake(x, y, width, height)
the changing of the font size will have to be implemented dynamically by a custom function of yours, you can use the class method of UIFont fontWithName:size for this.
Frame cannot be set for coordinates and dimension directly, you have to assign a new object. A quick search found this, Change ios Frame Size(width e height) keeping position (x,y).

Check for overlapping views after transform

I have multiple UIImageView's within a UIView. These UIImageViews have been transformed.
When the gesture is applied to any one of the UIImageView's the overlapping imageViews should get displaced in such a way that the move outside the UIImageView's frame and we get a full view of the image (all of this would be done in an animating manner).
Also please note that the images will have been transformed so I wont be able to use the frame properties.
Right now I'm using CGRectIntersectsRect but its not giving me the desired effect.
After struggling a lot, finally i achieved the desired result with this algorithm.
I calculate the list of overlapping UIImageView's and store it in an array.
I am using CGRectIntersectsRect for this.
Note : This will also include the UIImageView on which the gesture was applied (say target ImageView).
For each of these overlapping UIImageView, i perform the following steps :
1) Calculate the angle between the overlapping ImageView and target
ImageView. This will give me the direction to shift the Overlapping ImageView.
2) Now shift the overlapping ImageView by some constant distance(say len), such that it maintains the same angle. Direction should be such that it moves away from the target ImageView.
3) You can calculate the new x,y co-ordinates using Sin/Cos functions.
x = start_x + len * cos(angle);
y = start_y + len * sin(angle);
NOTE: For ImageViews whose center is less than your target ImageView's center, you will need to subtract the value.
4) Now shift the overlapping ImageView to the new calculated center.
5) Continue shifting it until the views do not intersect any more.
I am attaching my code. I hope it helps.
-(void)moveImage:(UIImageView *)viewToMove fromView:(UIImageView
*)imageToSendBack
{
CGFloat angle = angleBetweenLines(viewToMove.center, imageToSendBack.center);
CGFloat extraSpace = 50;
CGRect oldBounds = viewToMove.bounds;
CGPoint oldCenter = viewToMove.center;
CGPoint shiftedCenter;
while(CGRectIntersectsRect(viewToMove.frame,imageToSendBack.frame))
{
CGPoint startPoint = viewToMove.center;
shiftedCenter.x = startPoint.x - (extraSpace * cos(angle));
if(imageToSendBack.center.y < viewToMove.center.y)
shiftedCenter.y = startPoint.y + extraSpace * sin(angle);
else
shiftedCenter.y = startPoint.y - extraSpace * sin(angle);
viewToMove.center = shiftedCenter;
}
viewToMove.bounds = oldBounds;
viewToMove.center = oldCenter;
[self moveImageAnimationBegin:viewToMove toNewCenter:shiftedCenter];
}
I didn't try this, but I would do it like this:
After getting the bool from CGRectIntersectsRect, multiply this overlapping UIImageView x and y by the width and height of it.
And since you can't use frame property after changing the transformation, you will need to multiply the width and height by 1.5 for the maximum amount of rotation.
Warning: If the transform property is not the identity transform, the
value of this property is undefined and therefore should be ignored.
This might move some view father than other, but you will be certain all views will be moved outside the UIImageView's frame.
If you try it and it doesn't work, post some code and I will help you with it.
EDIT: For views that their x,y less than your UIImageView's x,y you will need to subtract the offset.

Center a MacGLView in a Window

I have an cocos2D app that runs in 4/3 ratio and I want to enter fullscreen mode resizing the view to maintain the aspect ratio without showing black bars at screen sides. So, when the app enters fullscreen, I do this:
NSRect aFrame=[[NSScreen mainScreen] frame];
[glView_ setFrameSize:NSMakeSize(aFrame.size.width,aFrame.size.height*1.2)];
This way I got fullscreen wide but (as I wanted) the ratio is untouched. My problem now is that the glview is not centered on screen. It extends from left bottom point so I got my game unevenly clipped (all clipping is done at top). I've trying to use [glview setFrameOrigin] and similar to select the visible portion of the view without success.
I just want to displace my view a certain number of pixels down so it shows the center area of whole view. Is it possible?.
Thanks in advice.
You are changing the dimensions of glView_, but not its origin.
NSRect aFrame=[[NSScreen mainScreen] frame];
CGFloat x = 0.0f;
CGFloat y = aFrame.size.height;
CGFloat width = aFrame.size.width;
CGFloat height = aFrame.size.height * 1.2f;
glView_.frame = NSRectFromCGRect((CGRectMake(x, y/2 - height / 2, width, height));

Custom UITextField blurred text

When a UITextField with custom frame (200 × 54 px) loses focus, the text looks blurred.
(source: idzr.org)
Is this a common problem? Is there any workaround / solution known?
I found a number of references to this bug through Google, but everybody worked around it by playing with font sizes. After much hunting I found this thread that says anti-aliasing is applied when a view's frame contains fractional pixel values, e.g. if you calculate its size as a fraction of the super view.
Sure enough, casting the CGRect values to int for the view's frame worked perfectly. So as an example, if you wanted your text field to be centered vertically in the superview, you should use an int cast like this:
textFieldWidth = 300;
textFieldHeight = 31;
offsetX = 0;
offsetY = (superview.bounds.size.height - textFieldHeight) / 2;
textField.frame = CGRectMake((int) offsetX,
(int) offsetY,
(int) textFieldWidth,
(int) textFieldHeight);
There is also the CGRectIntegral function that you can use to convert a CGRect to integral values.

How do I convert Cocoa co-ords from top left == origin to bottom left == origin

I use CGWindowListCopyWindowInfo to get a list of all windows. It gives me the co-ordinates of each window based upon the origin being the top-left of the screen.
If I use NSWindow's setFrame method, the co-ordinates on based upon the origin being the bottom-left of the screen.
What's a clean, reliable way to convert from one to the other?
Added: By clean and reliable, I mean, something sure to work regardless whether the user has multiple screens or is using Spaces. I figure there must be a known idiom using library APIs.
Math is quite reliable :-)
yFromBottom = screenHeight - windowHeight - yFromTop
Main screen height is
[[[NSScreen screens] objectAtIndex:0] frame].size.height
I would suggest using an NSAffineTransform. If you draw with respect to the default origin and then apply a transform to the view, you can essentially flip things around in one fell swoop.
Try something like this (from here):
NSRect boundsInWindow = [myView convertRect:[myView bounds] toView:nil];
NSRect visibleRectInWindow = [myView convertRect:[myView visibleRect] toView:nil];
// Flip Y to convert NSWindow coordinates to top-left-based window coordinates.
float borderViewHeight = [[myView window] frame].size.height;
boundsInWindow.origin.y = borderViewHeight - NSMaxY(boundsInWindow);
visibleRectInWindow.origin.y = borderViewHeight - NSMaxY(visibleRectInWindow);