iOS, Graceful Orientation Change - ios

I call this function at viewDidLoad and didRotateFromInterfaceOrientation
-(void) makeBox{
[self.view1 removeFromSuperview];
float viewWidth = CGRectGetWidth(self.view.frame);
float viewHeight = CGRectGetHeight(self.view.frame);
float startx = (viewWidth / 100) * 10;
float starty = (viewHeight /100) * 20;
float width = (viewWidth / 100) * 80;
float height = (viewHeight /100) * 60;
CGRect frame1 = CGRectMake(startx, starty, width, height);
self.view1 = [[UIView alloc] initWithFrame:frame1];
[self.view1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:self.view1];
}
When the view does change, its very 'sketchy' & not very graceful at all. I'm using simulator but i assume that it would be the same if i were to run on a device. How would i go about making this transition smoother? I would post to user experiance page, but i wish todo this programmatically
The overall point of this, other than to learn, is to achieve orientation independent graphics from code (without autolayout).

There's no need to remove the view and create a new one with the desired frame if its the only thing you're changing. Just modify the view frame in-place:
- (void)makeBox {
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
CGFloat viewHeight = CGRectGetHeight(self.view.frame);
CGFloat startx = (viewWidth / 100) * 10;
CGFloat starty = (viewHeight /100) * 20;
CGFloat width = (viewWidth / 100) * 80;
CGFloat height = (viewHeight /100) * 60;
CGRect view1Frame = CGRectMake(startx, starty, width, height);
if (!self.view1) {
// The view doesn't exists yet, we create it
self.view1 = [[UIView alloc] initWithFrame:view1Frame];
[self.view1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:self.view1];
}
else {
// Just update the frame
self.view1.frame = view1Frame;
}
}
For extra-nice UX, wrap the frame update in an animation block:
// ... snip ...
else {
[UIView animateWithDuration:0.3 animations:^() {
self.view1.frame = view1Frame;
}];
}

Related

How to implement Auto Layout with Button Animations

I have an animation that moves three buttons upward on the screen programmatically. However, when I test it on the simulator for iPhone 6 or 4 the position of the buttons are all wrong (but are only right on the iPhone 5). How do I fix this. The buttons are built programmatically so I can't really use auto layout to position them on the view controller.
-(IBAction)Search:(id)sender {
self.button.frame = CGRectMake(124, 475, 78, 72);
self.buttonTwo.frame = CGRectMake(124, 475, 78, 76);
self.buttonThree.frame = CGRectMake(124, 475, 78, 76);
// animate
[UIView animateWithDuration:0.75 animations:^{
self.button.frame = CGRectMake(13, 403, 78, 72);
self.buttonTwo.frame = CGRectMake(124, 347, 78, 76);
self.buttonThree.frame = CGRectMake(232, 403, 78, 76);
You are using fixed values for frames, but the screens are different widths and/or heights.
If they are correct for iPhone 5-5s (Also known as iPhone 4") then one way to handle it is:
-(IBAction)Search:(id)sender {
CGFloat screenWidth = self.view.bounds.size.width;
CGFloat screenHeight = self.view.bounds.size.height;
CGFloat normalizedX = (124 / 320) // You calculate these 'normalized' numbers, possibly from a designer's spec.
// it's the percent the amount should be over, as number from 0-1.
// This number is based on screen width of 320 having x 124 pt.
CGFloat startingX = normalizedX * screenWidth;
CGFloat startingY = (475 / 588) * screenHeight;
CGFloat width = (78 / 320) * screenWidth;
CGFloat height = (72 / 588) * screenHeight;
CGRect startingRect = CGRectMake(startingX, startingY, width, height)
self.button.frame = startingRect;
self.buttonTwo.frame = startingRect;
self.buttonThree.frame = startingRect;
// animate
[UIView animateWithDuration:0.75 animations:^{
CGFloat firstX = (13 / 320) * screenWidth;
CGFloat lowerY = (403 / 588) * screenHeight;
self.button.frame = CGRectMake(firstX, lowerY, width, height);
CGFloat secondX = (124 / 320) * screenWidth;
CGFloat upperY = (347 / 588) * screenHeight;
self.buttonTwo.frame = CGRectMake(secondX, upperY, width, height);
CGFloat thirdX = (233 / 320) * ScreenWidth;
self.buttonThree.frame = CGRectMake(thirdX, lowerY, width, height);
}];
}
This will scale the size of everything up and keep the relative positions. Note UIButtons will have the same text size. You can play with these numbers until you get the effect you want. Hope this helps!

How to zoom from min to max coordinates positions in UIScrollView?

I have a UIScrollView, which hold an UIImageView. I have a requirements to reach and zoom to particular location in UIImageView.
So for e.g. I want to start from (x: 0, y: 0) to specified location.
What's the best practice to do that?
This is how I'm achieving this:
- (void) setInitialZoomLevelAndShowImageInCenter {
CGRect scrollViewFrame = scrollViewMap.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / scrollViewMap.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / scrollViewMap.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
scrollViewMap.minimumZoomScale = minScale/1.5;
scrollViewMap.maximumZoomScale = 10.0f;
scrollViewMap.zoomScale = minScale;
CGFloat newZoomScale = scrollViewMap.zoomScale * 2.80f;
CGFloat xPos = 0;
CGFloat yPos = 0;
BOOL isMatched = NO;
while (!isMatched) {
[self zoomToPoint:CGPointMake(xPos, yPos) withScale:newZoomScale animated:YES];
BOOL isAnyChange = NO;
if(xPos <= slotItem.xCord) {
xPos+=1.0f;
isAnyChange = YES;
}
if(yPos <= slotItem.yCord) {
yPos+=1.0f;
isAnyChange = YES;
}
if(!isAnyChange) {
isMatched = YES;
}
}
}
- (void)zoomToPoint:(CGPoint)zoomPoint withScale: (CGFloat)scale animated: (BOOL)animated
{
//Normalize current content size back to content scale of 1.0f
CGSize contentSize;
contentSize.width = (scrollViewMap.contentSize.width / scrollViewMap.zoomScale);
contentSize.height = (scrollViewMap.contentSize.height / scrollViewMap.zoomScale);
//derive the size of the region to zoom to
CGSize zoomSize;
zoomSize.width = scrollViewMap.bounds.size.width / scale;
zoomSize.height = scrollViewMap.bounds.size.height / scale;
//offset the zoom rect so the actual zoom point is in the middle of the rectangle
CGRect zoomRect;
zoomRect.origin.x = zoomPoint.x - zoomSize.width / 2.0f;
zoomRect.origin.y = zoomPoint.y - zoomSize.height / 2.0f;
zoomRect.size.width = zoomSize.width;
zoomRect.size.height = zoomSize.height;
//apply the resize
[scrollViewMap zoomToRect: zoomRect animated: animated];
}

iOS, create custom popover style UIView

I know there is alot of documentation on this topic, and from what i've read i've learned a lot. My issue here is that i began creating a custom UIView within a viewcontroller & now when i'm trying to make it the UIView its own class, so as to be called & used within a working app, i'm pretty confused as to what i need to do to achieve it.
The overall aim of the class is to be able to select a button one a view controller, which will instantiate the custom view, then display. The visual effect for the user is that they click the button, then a window will animate-appear from the bounds of the button (although the code below uses the positioning of top left currently), then display a combination of switches & labels, along with a back & save button.
You can see from my code that what i need to achieve this process, i've managed to implement into a single function (again, this was from creating it within a viewcontroller). What i'm after now, and can't quite fathom out is for the custom view to be imported, then on the action of the main view controller's button, add the view to self.view.
- (void)makeBox{
//view bounds
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
CGFloat viewHeight = CGRectGetHeight(self.view.frame);
//red box x, y & size
CGFloat startx = (viewWidth / 100) * 10;
CGFloat starty = (viewHeight /100) * 20;
CGFloat width = (viewWidth / 100) * 80;
CGFloat height = (viewHeight /100) * 70;
CGRect view1Frame = CGRectMake(startx, starty, width, height);
//label & switch frame
CGRect labelMR = CGRectMake(10, ((height / 100) * 12), ((width / 100) * 80) - 7.5, 25);
CGRect switchMR = CGRectMake(((width / 100) * 80) + 7.5, ((height / 100) * 11), ((width / 100) * 10), 15);
//this is repeated for 6 other labels & switches
CGRect backButtonR = CGRectMake(5, 5, 50, 35);
CGRect saveButtonR = CGRectMake((width - 50), 5, 50, 35);
if (!self.view1) {
self.switchM = [[UISwitch alloc] initWithFrame:switchMR];
self.switchM.tag = 10;
if(self.monday){ //self.monday refers to a BOOL property of the viewcontroller that this was originally made in
self.switchM.on = true;
} else{
self.switchM.on = false;
}
[self.switchM addTarget:self action:#selector(switched:) forControlEvents:UIControlEventTouchUpInside];
// this switch instantiation process is repeated 6 other times
self.backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.backButton addTarget:self
action:#selector(hideBox)
forControlEvents:UIControlEventTouchUpInside];
[self.backButton setTitle:#"Back" forState:UIControlStateNormal];
self.backButton.frame = backButtonR;
self.saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.saveButton addTarget:self
action:#selector(daysChanged)
forControlEvents:UIControlEventTouchUpInside];
[self.saveButton setTitle:#"Save" forState:UIControlStateNormal];
self.saveButton.frame = saveButtonR;
self.labelM = [[UILabel alloc] initWithFrame:labelMR];
self.labelM.layer.masksToBounds = YES;
self.labelM.layer.cornerRadius = 5;
self.labelM.layer.borderColor = [UIColor blackColor].CGColor;
self.labelM.layer.borderWidth = .5;
self.labelM.text = #" Monday";
self.labelM.backgroundColor = [UIColor whiteColor];
//again - repeated 6 times
//use this starting frame to make the 'popover' appear small at first
CGRect startFrame = CGRectMake(10, 10, 10, 10);
self.view1 = [[UIView alloc] initWithFrame:startFrame];
[self.view addSubview:self.view1];
[UIView animateWithDuration:0.5 animations:^(void){
self.view1.layer.cornerRadius = 10;
self.view1.layer.borderColor = [UIColor blackColor].CGColor;
self.view1.layer.borderWidth = .5;
[self.view1 setBackgroundColor:[UIColor lightGrayColor]];
self.view1.frame = view1Frame;
} completion:^(BOOL finished){
[self.view1 addSubview:self.labelM];
[self.view1 addSubview:self.switchM];
//repeat 6 times for other labels & switches
}];
}
else {
[UIView animateWithDuration:0.3 animations:^() {
self.view1.frame = view1Frame;
} completion:^(BOOL finished) {
self.labelM.frame = labelMR;
self.switchM.frame = switchMR;
//repeated for the other 6
self.backButton.frame = backButtonR;
self.saveButton.frame = saveButtonR;
}];
}
}
-(void) hideBox{
//back button was selected
[UIView animateWithDuration:0.5 animations:^(void){
[self.view1 removeFromSuperview];
self.view1 = nil;
}];
}
I understand that as i'm doing/calling this via code, i need to override either init or initWithFrame:. Do i pass the initWithFrame: the containing view controllers bounds, of which the custom view can be calculated on, and how do i handle the animation?
I've managed to set up the delegate protocol which will notify when the save button is pressed. that part i understand, its just the rest of the parts i'm unclear on.
First some background.
Typically for a popover controller, you have a single transparent root view which contains the popover view itself. This root view is the size of the containing window and usually higher in z-order than all other views. There are two reasons for this:
1) The popover needs to draw on top of all other views. If the root view wasn't on top of all other views, the popover could be clipped and not draw all of its contents.
2) The user usually needs some place to tap and dismiss the popover (effectively a cancel action).
UIPopoverController does this as do some 3rd party popovers. Some even go so far as to use their own UIWindows.
To answer your specific questions:
1) initWithFrame: / setFrame: should be passed a CGRect that is in the coordinate space of the view that will contain the view. In other words, the superview. If you decide to use a transparent root view, the popover (the part that draw on screen) will be passed a CGRect in the coordinate space of the transparent root view.
2) Usually a popover is animated like so:
// set initial frame
popover.frame = CGRectMake(x, y, width, 0);
popover.alpha = 0.0;
[rootView addSubview:popover];
[UIView animateWithDuration:0.5
animations:^{
CGRect frame = popover.frame;
frame.size.height = some-height;
popover.frame = frame;
popover.alpha = 1;
}
];
I actually got it to work! (although i can't promise this is the cleanest or most resourceful way - we'll work on that).
Here's what i did:
Split the makeBox function into 3 function, initWithWidth:andHeight:forView, openBox & updateBoxWithWidth:andHeight.
I used a saveButton delegate method that informs the parent that save button has been pressed.
.h
#protocol BoxDelegate
-(void) daysChanged;
#end
__weak id <BoxDelegate> delegate;
#interface aBox : UIView
#property (nonatomic) BOOL monday;
#property (nonatomic, weak) id <BoxDelegate> delegate;
//property values for the custom view
-(id) initWithWidth: (CGFloat) width andHeight: (CGFloat) height withView: (UIView*) theView;
-(void) updateViewWithWidth: (CGFloat) width andHeight: (CGFloat) height;
-(void) openBox;
#end
.m
#synthesize delegate;
-(id) initWithWidth:(CGFloat)inWidth andHeight:(CGFloat)inHeight withView: (UIView*) theView{
self = [super init];
self.mainView = theView;
//super view bounds
self.viewWidth = inWidth;
self.viewHeight = inHeight;
//red box x, y & size
CGFloat startx = (self.viewWidth / 100) * 10;
CGFloat starty = (self.viewHeight /100) * 20;
self.width = (self.viewWidth / 100) * 80;
self.height = (self.viewHeight /100) * 70;
self.view1Frame = CGRectMake(startx, starty, self.width, self.height);
//label & switch frame
self.labelMR = CGRectMake(10, ((self.height / 100) * 12), ((self.width / 100) * 80) - 7.5, 25);
self.switchMR = CGRectMake(((self.width / 100) * 80) + 7.5, ((self.height / 100) * 11), ((self.width / 100) * 10), 15);
//repeated for the other 6
self.backButtonR = CGRectMake(5, 5, 50, 35);
self.saveButtonR = CGRectMake((self.width - 50), 5, 50, 35);
self.switchM = [[UISwitch alloc] initWithFrame:self.switchMR];
self.switchM.tag = 10;
if(self.monday){
self.switchM.on = true;
} else{
self.switchM.on = false;
}
[self.switchM addTarget:self action:#selector(switched:) forControlEvents:UIControlEventTouchUpInside];
self.labelM = [[UILabel alloc] initWithFrame:self.labelMR];
self.labelM.layer.masksToBounds = YES;
self.labelM.layer.cornerRadius = 5;
self.labelM.layer.borderColor = [UIColor blackColor].CGColor;
self.labelM.layer.borderWidth = .5;
self.labelM.text = #" Monday";
self.labelM.backgroundColor = [UIColor whiteColor];
//repeated for the other 6
self.saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.saveButton addTarget:delegate
action:#selector(daysChanged)
forControlEvents:UIControlEventTouchUpInside];
[self.saveButton setTitle:#"Save" forState:UIControlStateNormal];
self.saveButton.frame = self.saveButtonR;
return self;
}
-(void) openBox{
CGRect startFrame = CGRectMake(10, 10, 10, 10);
self.view1 = [[UIView alloc] initWithFrame:startFrame];
[self.mainView addSubview:self.view1];
[UIView animateWithDuration:0.5 animations:^(void){
self.view1.layer.cornerRadius = 10;
self.view1.layer.borderColor = [UIColor blackColor].CGColor;
self.view1.layer.borderWidth = .5;
[self.view1 setBackgroundColor:[UIColor lightGrayColor]];
// repeated for the other 6
self.view1.frame = self.view1Frame;
} completion:^(BOOL finished){
[self.view1 addSubview:self.labelM];
[self.view1 addSubview:self.switchM];
[self.view1 addSubview:self.backButton];
[self.view1 addSubview:self.saveButton];
}];
}
-(void) updateViewWithWidth: (CGFloat) width andHeight:(CGFloat) height{
//re-calculate
self.viewWidth = width;
self.viewHeight = height;
CGFloat startx = (self.viewWidth / 100) * 10;
CGFloat starty = (self.viewHeight /100) * 20;
self.width = (self.viewWidth / 100) * 80;
self.height = (self.viewHeight /100) * 70;
self.view1Frame = CGRectMake(startx, starty, self.width, self.height);
//label & switch frame
self.labelMR = CGRectMake(10, ((self.height / 100) * 12), ((self.width / 100) * 80) - 7.5, 25);
self.switchMR = CGRectMake(((self.width / 100) * 80) + 7.5, ((self.height / 100) * 11), ((self.width / 100) * 10), 15);
self.backButtonR = CGRectMake(5, 5, 50, 35);
self.saveButtonR = CGRectMake((self.width - 50), 5, 50, 35);
[UIView animateWithDuration:0.3 animations:^() {
self.view1.frame = self.view1Frame;
} completion:^(BOOL finished) {
self.labelM.frame = self.labelMR;
self.switchM.frame = self.switchMR;
self.backButton.frame = self.backButtonR;
self.saveButton.frame = self.saveButtonR;
}];
}
And then in our parent viewcontroller
#property aBox *theBox;
...
- (void)viewDidLoad {
[super viewDidLoad];
self.theBox = [[aBox alloc] initWithWidth:self.view.frame.size.width andHeight:self.view.frame.size.height withView:self.view];
[self.theBox openBox];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[self.theBox updateViewWithWidth:self.view.frame.size.width andHeight:self.view.frame.size.height];
}
- (void) daysChanged{
NSLog(#"Days Changed!");
}

How to keep an image from going out side of a set area

I'm trying to make it so when you touch the image it moves to a random spot, but I now realize I need it to stay with in a set space so I can add things like a score counter. The problem I'm having is that I have no idea how to do this so any help is appreciated.
My Code:
-(IBAction)hideImage {
CGFloat xRange = self.view.bounds.size.width - Image.bounds.size.width;
CGFloat yRange = self.view.bounds.size.height - Image.bounds.size.height;
CGFloat minX = self.view.center.x - (xRange / 2);
CGFloat minY = self.view.center.y - (yRange / 2);
int randomX = (arc4random() % (int)floorf(xRange)) + minX;
int randomY = (arc4random() % (int)floorf(yRange)) + minY;
Image.center = CGPointMake(randomX, randomY);
}
Thank you.
You could use a UIView that has the frame that you want. Then add the UIImageView to the UIView and set:
[thisView setClipsToBounds: YES];
So:
CGRect myBounds = CGRectMake(0, 0, 320, 300);
UIView* thisView = [[UIView alloc] initWithFrame: myBounds];
[self.view addSubview: thisView];
UIImageView* image = [[UIImageView alloc] initWithFrame:...];
//Other image code
[thisView addSubview: image];
[thisView setClipsToBounds: YES];
If you have a UIImageView property called movingImageView as a subview, try this:
-(IBAction)hideImage {
UIView *containerView = self.view;
UIView *movingView = self.movingImageView;
CGFloat xRange = CGRectGetWidth(containerView.bounds) - CGRectGetWidth(movingImageView.bounds);
CGFloat yRange = CGRectGetHeight(containerView.bounds) - CGRectGetHeight(movingImageView.bounds);
CGFloat minX = CGRectGetMidX(movingImageView.bounds);
CGFloat minY = CGRectGetMidY(movingImageView.bounds);
NSInteger randomX = (arc4random() % (NSInteger)floorf(xRange)) + minX;
NSInteger randomY = (arc4random() % (NSInteger)floorf(yRange)) + minY;
self.movingImageView.center = CGPointMake(randomX, randomY);
}
When the imageView's bounds is greater or equal to the container's bounds things will break.
This is pretty much the same code as you already had, if the 'image' is actually contained in a UIButton, you could change the method declaration to be:
-(IBAction)hideImage:(id)sender {
UIView *containerView = self.view;
UIView *movingView = (UIView *)sender
... The rest is same as above ...

UIScrollview calculation of new CGRect after ZoomToRect

My problem is i am not able to get the exact CGRect value after zooming for zooming in UIScrollview i am using zoomToRect method of UIScrollview. while zooming i am setting the contentOffset property.
Code follows:
if(image != nil) {
CGSize zoomViewSize = image.frame.size;
CGSize scrollViewSize = self.bounds.size;
if(zoomViewSize.width < scrollViewSize.width) {
anOffset.x = -(scrollViewSize.width - zoomViewSize.width) / 2.0;
}
if(zoomViewSize.height < scrollViewSize.height) {
anOffset.y = -(scrollViewSize.height - zoomViewSize.height) / 2.0;
}
}
super.contentOffset = anOffset;
after zooming i am setting the content inset property so that image comes in the center. for content inset the code is given below.
-(void)zoomtorect:(CGRect)rect animated:(BOOL)animated {
[super zoomtorect:rect animated:YES];
CGFloat pageWidth = image.image.size.height;
CGSize imageSize = rect.size;
CGSize zoomedImageSize = CGSizeMake(imageSize.width * 0.2, imageSize.height * 0.2);
CGSize pageSize = self.bounds.size;
//scLayer.frame = rect;
UIEdgeInsets inset = UIEdgeInsetsZero;
if (pageSize.width > zoomedImageSize.width) {
inset.left = (pageSize.width - zoomedImageSize.width) / 2;
inset.right = inset.left;
}
if (pageSize.height > zoomedImageSize.height) {
inset.top = (pageSize.height - zoomedImageSize.height) / 2;
inset.bottom = inset.top;
}
self.contentInset = inset;
}
this code shifts my image view between screen. Every thing works fine here. My problem is i have to show only zoom portion of the imageView rest will be black. For calculating the zoomed area i am doing the following calculation.
CGSize imageSize = rect.size;
CGSize zoomedImageSize = CGSizeMake(imageSize.width * scroll.maximumZoomScale, imageSize.height *scroll.maximumZoomScale);
CGSize pageSize = scroll.bounds.size;
UIEdgeInsets inset = UIEdgeInsetsZero;
if (pageSize.width > zoomedImageSize.width) {
inset.left = (pageSize.width - zoomedImageSize.width) / 2;
inset.right = inset.left;
}
if (pageSize.height > zoomedImageSize.height) {
inset.top = (pageSize.height - zoomedImageSize.height) / 2;
inset.bottom = inset.top;
}
CGRect zoomRect = [scroll convertRect:rect fromView:image];
CGRect zoomRect1 = CGRectMake(inset.left, inset.top+50, zoomRect.size.width, zoomRect.size.height);
this zoomRect1 is the zoomed area of the image this value is not exact what i am seeing in the screen. some where i am doing calculation mistake please help me out. But i am not getting the exact value of the zoomed image area.
Thanks in advance
Use this.. Works Perfectly
- (void)handleDoubleTap:(UIGestureRecognizer *)recognizer
{
if(isAlreadyZoomed)
{
CGPoint Pointview = [recognizer locationInView:recognizer.view];
CGFloat newZoomscal = 3.0;
CGSize scrollViewSize = self.scrollContainer.bounds.size;
CGFloat width = scrollViewSize.width/newZoomscal;
CGFloat height = scrollViewSize.height /newZoomscal;
CGFloat xPos = Pointview.x-(width/2.0);
CGFloat yPos = Pointview.y-(height/2.0);
CGRect rectTozoom=CGRectMake(xPos, yPos, width, height);
[self.scrollContainer zoomToRect:rectTozoom animated:YES];
[self.scrollContainer setZoomScale:3.0 animated:YES];
isAlreadyZoomed = NO;
}
else
{
[self.scrollContainer setZoomScale:1.0 animated:YES];
isAlreadyZoomed = YES;
}
}
isAlreadyZoomed is a BOOL value

Resources