Scale UIView and all its children - ios

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

Related

Rotate UILabel On Clockface

Is it possible to rotate UILabels or similar about a clockface?
For example, if I had an array of numbers from one to twelve, it would render as such in a circular manner. However, with a touch event I could move the labels about the face of the clock i.e. I could move 12 in a rightward motion until it reached 6, with all other numbers remaining in their positions.
I have considered UIPicker, but that follows a 'top to bottom' approach. I need a solution to move the label about a point while maintaining the order of the other labels.
It's simply a matter of opposing rotation transforms. Rotate the clock face while at the same time rotating all the subview numbers the same amount in the opposite direction.
Recommended:
1 Clock without small & Big handle (As Image) set it in ImageView.
2 Another two image view one for Small hand another one for Big hand.
3 Rotate the hands(UIImageView) by using the below code as you needed.
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
UIImageView *smallHand = set your smallHand image view here;
UIImageView *bigHand = set your bigHand image view here;
// Small hand
CGAffineTransform smallHandTransform = CGAffineTransformRotate(self.transform, (DEGREES_TO_RADIANS(1)));
// Big hand
CGAffineTransform bigHandTransform = CGAffineTransformRotate(self.transform, (DEGREES_TO_RADIANS(1*0.60)));
// Apply rotation
smallHand.transform = smallHandTransform;
bigHand.transform = bigHandTransform;
I knew that question was rotating the label, if you need it apply this transform on UILabel.
I hope somebody will make use of this idea. Good luck :)
Here is the example project

Using a UIButton to mask a moving UIView

I want to bind two views to a button's border like a mask. The effect I want is seen in the image below
So I have a custom controller that uses a PageViewController to swipe between two UIViewControllers. I also have two views (the red one and the blue one). I can track the direction and degree of the page turning, so I can link that to the movement of the two views. All I need to do is apply a mask to the moving boxes so that all the movement stays within the button.
If anyone knows how to do this, I'd be eternally grateful for the information!
My understanding is that you have a view, then you want to transition between its two subviews with a sliding animation. This is fairly straight forward.
First set "Clip Subviews" to true (either in storyboard editor or setting view.clipToBounds=true). If you want a sphere like you show then set layer.cornerRadius = view.frame.size.width/2
The you can just set the frames of your subviews- either animating them with UIView animateWithDuration or setting the frames yourself in your animation loop.
The former in code (for sliding the first element to the left and right element in):
[UIView animateWithDuration:duration animations:^{
[subview1 setFrame:CGRectMake(-subview1.frame.size.width, 0, subview1.frame.size.width, subview1.frame.size.height)];
[subview2 setFrame:CGRectMake(0, 0, subview2.frame.size.width, subview2.frame.size.height)];
}];
The reverse animation is similar. If you're doing it in your own animation loop (which sounds like what you want) then you can do the interpolation yourself pretty easily.
If you want to be more advanced with your mask (i.e. based on an image) then look at something like this: How to mask UIViews in iOS
This solution may not be the optimized one, but do the trick you want. First i wanna make sure that i understand your requirement. I understand that you wanna go away to left the red image & bring in the right blue image in the button with the sliding effect.
If my understanding meet your requirement,Here is the trick
First decide which portion of red & blue image you needed. That must be easy as you know the direction & degree of the turning the UIPageViewController.
Then Crop that decided portions from the red & blue image using the code here.
Now draw both resulted cropped image on the coreGraphics side by side (red on the left & blue on the right) & retrieve the final resulted image.
set the image to the UIButton.
Finally to acquire the round shape you can set the radius property of layer property of the button.
For the third step, how you can you draw image on coreGraphics & retrieve apple give a detail explanation here
You have to do all these steps each time you wanna update the image of the UIButton. There may be a other good way to do this :)
You can use corner radious on the button layer
CGPoint saveCenter = roundedView.center;
CGRect newFrame = CGRectMake(roundedView.frame.origin.x, roundedView.frame.origin.y, newSize, newSize);
button.frame = newFrame;
button.layer.cornerRadius = newSize / 2.0;
button.center = saveCenter;

How to make 3d rotation of uiview like ios 7 uipickerview type

How to make 3d rotation of uiview like ios 7 uipickerview structure. It will look like 3d wheel of some width
Thanks in advance.
I think this is one that can help you (if it still needed or for those who come here later)
CATransform3DMakeRotation
Example:
lable.layer.transfortm = CATransform3DMakeRotation(angle * M_PI/360, 1, 0, 0);
This will rotate label on x axis by specified angle, so you can change this value depending on label position in scroll view of your custom picker.
Also just read docs about CATransform3D

UIView: how to "reset" transformations. CGAffineTransformScale not working as I would expect

I think my question can be summed up as how to store and reset the transform of a view. But then perhaps explaining my situation might help.
If I apply the transforms below to a view, one after another (like if I add this code to a switch or a button). I get exactly the result I would expect: the scale switches between: a view that is .55 times the size of the original view, and the view at it's original scale. Works to scale sub-views of someView too, just as I want. Ad infinitum. Perfect.
//tranformScale 1
someView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.55, 0.55 );
//tranformScale 2
someView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0 );
The trouble is I want to use this code (or similar) to scale a sub-view of self.view when an iOS device goes into landscape, to fit the sub-view to the landscape screen (and back up when in portrait). It almost works, but for some reason instead of outputting round values values for the frame of the view being scaled (as happens with a test using a button to call the transforms), progressively strange values are produced. Eventually, after about 4 rotations the sub-view flies off screen. I suspect it has to do with self.view changing shape, but then again, when I log the frame of self.view it's shape is very predictable.
By the way I am centering the view using autoresizingMask flexible margins, not using auto-layout. perhaps I should be centering the view with another type of calculation?
Thanks for reading!
I did this and it worked perfect.
self.imageview.transform = CGAffineTransformIdentity
(1) be sure not to set or get any frame data after applying transforms, it is unsupported and will yield unpredictable results;
(2) turn off your autoresizing mask flex margins and center like this:
view.center = CGPointMake(view.superview.bounds.size.width/2,
view.superview.bounds.size.height/2);
(take care to apply centering while view is at full size. i.e. BEFORE the transform if it is the resize transform, AFTER the transform if it is the identity transform)

UIView's content blurry when I center it

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

Resources