I have UIView that isn't big as the whole screen and I want it to appear in the middle of the screen. I did that with the following code:
self.dialogView.center = CGPointMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0);
Unfortunately, when I center it like that, UIView's content appears blurry.
I've fixed it with this:
self.dialogView.center = CGPointMake(self.view.bounds.size.width / 2.0 + 0.5, self.view.bounds.size.height / 2.0);
Now UIView's content appears as it should :)
Can you tell me why that content becomes blurry and why I need to add 0.5 to the left position offset?
The new center is causing the origin of the control's view to fall on a half-pixel, like { 20.5, 20.5 }. Since the screen can't draw only half a pixel, the rendering engine attempts to get the same effect by diffusing the image as best it can, resulting in blurry images.
You need to make sure all your views' origins are whole numbers.
well i think thats due to how the UIView is being placed on the pixel grid of the screen. if you think about it on regular screens (72 px/inch), 0.5 points means 0.5 pixels which would probably cause what you see... thats my theory at least :P
Related
Could someone point me in the right direction for figuring out how to properly write out my code so that all buttons on any particular y-axis would try to fit the width of the current device screen as best as possible?
To be more exact, currently, I have 4 buttons (made from a SKSpriteNode) that need to be positioned within the screen in such a way that they look perfectly fitted. Device screen is portrait, although from a math point of view, that wouldn't matter, only the actual width of the screen. Each button width uses the same texture, thus resulting in identical widths, but the texture it self might change from time to time resulting in each buttons width either to shrink a little or expand.
Right now, my positioning code looks as follows:
btnLvl1.position = CGPointMake(_scrollingNode.size.width/20*3, scrollStart.position.y - 60);
btnLvl2.position = CGPointMake(_scrollingNode.size.width/20*8, scrollStart.position.y - 60);
btnLvl3.position = CGPointMake(_scrollingNode.size.width/20*13, scrollStart.position.y - 60);
btnLvl4.position = CGPointMake(_scrollingNode.size.width/20*18, scrollStart.position.y - 60);
I was hoping someone could recommend a math formula for making sure, regardless of each button width & how many buttons there are, that they would be placed within the screen width as best as possible. A super bonus would be if something could be done for the height of a device screen (but not necessary).
P.S. I did try figuring this one out my self, but I'm not very strong math-wise.
You can use your scene size to "auto-fit" any node. For example, if you want to center a button on the screen:
button.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
Although you can use this:
button.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
I make a game using XNA. I would like to align sprite in the center of the screen. This works well when fullscreen is set to false. But when I set IsFullScreen to true, it doesn't work.
I activated the console and print the screen size on it. The size is good (1366 * 768). I also print the current position of the mouse pointer, and when I'm at the bottom right corner, it shows 1279*719, that's why my sprites are not center-align. Why ?
width = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
height = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
_positionStart = new Vector2(width * 0.5F - _startButton.Width * 0.5F, height * 0.5F - 20);
PS : I'm using XNA 4.0 with VS2012
PS2 : sorry for my grammar, I don't speak English fluently :)
Probably because the top left corner of your sprite is drawn in the middle of the screen which means that the sprite will be drawn slightly on the right side and slightly lower too.
Try values lover than 0.5, maybe 0,45, or even less. This should help.
Well, if the other answer didn't work, a longer, but much more efficient way to do this would be too either:
Set a Rectangle() to the object, and make that act as a bounding box, and use the .Center feature to align the center of it.
Divide the height and the width by 2, as you did, but then minus half the height and width of the image itself (If you can't find that then just use a bounding box as above).
Hope this helps, I know this post is a bit old now, but someone else might stumble across this, and appreciate this answer!
I have an UIView with around 50 UIButtons. All button positions were given in pixels, relative to the left upper corner of my main UIView.
All (background) images used in the view are available in higher resolution. As I am porting my app from iPhone to iPad, I would like to increase the effective pixel size of the UIView.
Now I'm searching a way to upscale the whole UIView by a factor of 2*. Is that possible without destroying the position of the inner elements?
FYI, the UIView is designed in a NIB-file in XCode. But I don't mind if it can be done programmatically.
I ended up using
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);
It allowed me to keep the design created in the Interface Builder.
Unfortunately the sharpness of the image suffers in that case, but this is a small price to pay compared to scripting the whole design programmatically.
Update for Swift 3.0
self.view.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)
For Swift 4.0, zoom 2x:
myView.transform = CGAffineTransform.identity.scaledBy(x: 2, y: 2)
You can first programmatically create those buttons like example create those buttons using the CGRectMake method and stating the width and height to be X and Y and multiply by 2 if ipad is detected as for origin it should change respectively too, might cause overlapping if too close to each other
Edit: It all depends on your logic, im unsure too
I have a UIScrollView which displays a UIView which itself contains a small hierarchy of views. One of those sub-views is used to draw icons (their logical meaning are sticky note markers on a PDF page). Currently, the icons are zoomed along with all the other content. However, I want to draw them with the same size regardless of the current zoom (their correct position must still be maintained, of course). What's the best way to do this?
You basically have to set the transform scale of the subviews individually to match the zoom factor of the scrollView. So whenever you scroll, loop through the subviews and if the scrollview zoom factor is, say, 5.0, you'd set the transform like this:
CGFloat scaleFactor = 1.0/5.0; // 1.0 divided by the scrollview zoom factor
subview.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
On the plus side, it will be centered correctly, so all you have to do is set the transform and everything else should just work (you don't have to manipulate the frame).
I am having some issues understanding and using anchorPoint. As I understand it, the default anchor is (.5,.5) which applies transforms about the center of objects. However, when I placed a UIButton into a CCUIViewWrapper, I noticed that scaling it would scale about the right side of the object (i.e. if I scaled from 0 to 1, it would grow from right to left).
I wasn't sure what to make of this, but with tinkering found that I had to adjust the anchor point to (0,0) in order to make scaling occur about the center of the object. Why would this happen?
Further more, while I expect that to make the anchor be the bottom left of the object because Cocos2d is bottom left oriented for (0,0), it did not and in fact aligned the top left with my wrapper's position value.
In the end, what worked to scale about the center was to make the anchor point (0,0) and position the wrapper using the top left of the object, almost as if it were using the UIView coordinates to place the object. I'm not certain if this behavior is something occurring strictly with wrapped UIViews in CCUIViewWrappers nor do I know if this is simply correct behavior and I'm completely misunderstanding it.
Can someone clarify and explain?
I've had a similar problem with the wrapper. One of the issue here is that the wrapper attaches the view to the root window instead of the director's OpenGL view, which means it wont respect whatever autorotation the OpenGL view is using. The code looks like it is trying compensate for the difference between UIView and Cocos2d coordinates but doesn't seem to do it properly when it comes to anchor points. I ended up just using UIView coordinates in the end but I did experiment with an alternate method for setting the anchor point by using:
uiItem.center = ccp((self.anchorPoint.x - 0.5f) * self.contentSize.width, (self.anchorPoint.y - 0.5f) * self.contentSize.height);
At the start of the updateUIViewTransform method in CCUIViewWrappers before setting the view's transform. This makes anchor points behave as expected, at least for views attached directly to a layer.
Instead of using:
uiItem.center = ccp((self.anchorPoint.x - 0.5f) * self.contentSize.width, (self.anchorPoint.y - 0.5f) * self.contentSize.height);
which set the anchor point to the bottom right, I used:
uiItem.center = ccp(-uiItem.bounds.size.height/2, uiItem.bounds.size.width/2);
which set it to the center.
The reason the x and y are flipped, I'm guessing it's because my item is rotated by 90.