adapting screen to portrait bug objective-c - objective-c

Alright so i was having a little bit of a problem with the view when the device went into landscape so i decided to change the location of my textfields by code with this:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
NSLog(#"SPIN");
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
CGRect frame = [self.a frame];
frame.origin.x = 20;
frame.origin.y = 57;
[self.a setFrame:frame];
CGRect frame2 = [self.b frame];
frame2.origin.x = 20;
frame2.origin.y = 132;
[self.b setFrame:frame2];
CGRect frame3 = [self.c frame];
frame3.origin.x = 20;
frame3.origin.y = 207;
[self.c setFrame:frame3];
} else {
CGRect frame = [self.a frame];
frame.origin.x = 83;
frame.origin.y = 21;
[self.a setFrame:frame];
CGRect frame2 = [self.b frame];
frame2.origin.x = 204;
frame2.origin.y = 21;
[self.b setFrame:frame2];
CGRect frame3 = [self.c frame];
frame3.origin.x = 330;
frame3.origin.y = 21;
[self.c setFrame:frame3];
}
}
it worked just fine, everything went where it was suppost to, the problem is when click on any of the textfields to write something all of them bug out and end up looking like this:
uh.. nevermind that apparently i need at least 10 reputation before i can post an image, so here's a description of what the image showed instead:
image shows 3 uisegmented controls and 3 textfields crammed stacked on top of each other on the very top of the screen
So i'm more of a hobbyist than a pro at this so i apologize if this is a stupid question, but what am i doing wrong here? and additionaly is there a better way of going about adapting your view?

Try using auto layout or may be auto resizing masks on different views which you are using. There is a property called as autoresizingmask in uiview.
You can check this for reference:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html

Related

Webview Scroll Apply self.view IOS

In my application i have webview and some items above the webview.when i scroll the webview i want to scroll these items also.i tried so many thing it is not possible in scrollViewDidScroll.i need something else.I tried these codes but it is not working efficiently..
- (void)scrollViewDidScroll:(UIScrollView *)sender {
CGRect webViewFrame = self.contentWebView.frame;
webViewFrame.origin.y -= 3;
CGRect imageViewFrame = self.featuredImageView.frame;
imageViewFrame.origin.y -= 3;
CGRect overlayFrame = self.overlayView.frame;
overlayFrame.origin.y -= 3;
[UIView animateWithDuration:0.001 animations:^{
self.contentWebView.frame = webViewFrame;
self.featuredImageView.frame = imageViewFrame;
self.overlayView.frame = overlayFrame;
}completion:^(BOOL finished) {
CGPoint offset2 = sender.contentOffset;
offset2.y = 0;
sender.contentOffset = offset2;
}];
}
Try to add subView (Other items) to your webView.scrollView ;)
Example: [myWebView.scrollView addSubView: myItem];

How to Generate a Shockwave Effect

I would like to generate a shockwave effect when I tap on a circular UIView. The effect I'm looking for is very similar to 'Circadia' on the App Store.
(source: mzstatic.com)
Note the circular line that is expanding from the centre view. I couldn't find any kind of tutorial, so I tried the effect using a cheat:
-(void)startShockwave:(UIView *)target
{
// Replace the view tapped with one that will expand
UIView *wave = [[UIView alloc]initWithFrame:target.frame];
wave.backgroundColor = target.backgroundColor;
wave.alpha = 0.5;
wave.layer.masksToBounds = YES;
wave.layer.cornerRadius = wave.frame.size.width / 2;
// Hide it below the original view
[self.view insertSubview:wave belowSubview:target];
CGRect frame = wave.frame;
// Create a view that is the same colour as self.view to make it look like a hole
UIView *center = [[UIView alloc]initWithFrame:CGRectMake(frame.origin.x + 10, frame.origin.y + 10, frame.size.width - 20, frame.size.height - 20)];
center.backgroundColor = self.view.backgroundColor;
center.layer.masksToBounds = YES;
center.layer.cornerRadius = center.frame.size.width / 2;
[self.view insertSubview:center aboveSubview:wave];
// Hide the original view
target.alpha = 0;
// IMPORTANT: I send these views behind the others so that center does not overlap them
[self.view sendSubviewToBack:center];
[self.view sendSubviewToBack:wave];
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
CGAffineTransform waveTransform = wave.transform;
CGAffineTransform centerTransform = center.transform;
// Expand the views to look like a shockwave
wave.transform = CGAffineTransformScale(waveTransform, 4, 4);
center.transform = CGAffineTransformScale(centerTransform, 5.75, 5.75);
// Fade the wave out to nothing
wave.alpha = 0;
} completion:^(BOOL finished) {
// Remove the shockwave
[wave removeFromSuperview];
[center removeFromSuperview];
}];
}
This works quite well...
...With only one shockwave. However, when they intersect, one center overlaps the other due to the most recent shockwave being sent to the back.
I would prefer intersecting shockwaves to be more like this:
I'm not sure how to create this type of effect, however, so any help is much appreciated!
Use [UIColor clearColor] to make the center of your circle transparent (so you can see interface elements residing beneath it).
center.backgroundColor = [UIColor clearColor];
It's a quick fix! You already did most of it. Just change this line:
center.backgroundColor = self.view.backgroundColor;
to
center.backgroundColor = [UIColor clearColor];

Frame size relative to device and orientation

I have created a paginated UIScrollView with three subviews. It works great when testing on an iPhone 5 in landscape (the orientation I designed at) but it breaks whenever the device resolution changes.
How can I make the frame scale to the correct resolution, no matter the device or orientation?
- (void)viewDidLoad {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
}
- (IBAction)changePage {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
Put your scroll view inside another, custom view. In the custom view, implement layoutSubviews something like this.
#interface ViewScalesOneSubview : UIView
#property UIView *scalingSubview;//subview to scale
#end
#implementation ViewScalesOneSubview
-(void) layoutSubviews {
[super layoutSubviews];
CGRect parentBounds = [self bounds];
CGRect childBounds = [scalingSubview bounds];//unscaled
CGFloat scale = parentBounds.width / childBounds.width;
CGAffineTransform transform = CGAffineTransformMakeScale( scale , scale );
//fiddle with x,y translation to position as you like
scalingSubview.transform = transform;
}
#end
Give the custom view autoresizing so it fits the window or whatever container and changes with rotation. Do not give the scroll view autoresizing, as it will conflict with this custom layoutSubviews. When the custom view changes size, it will scale the scalingSubview to fit. By using the same scale for both axis, it will preserve aspect ratio. You could scale to fit instead, or use height instead of width or whatever.
Edit:
To resize the view, as opposed to scaling the view, set the autoresizing mask.
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
https://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/autoresizingMask
You can also do this in interface builder.

Change UIView layout programmatically for iPhone 5

I am trying to change the layout of my UIView programmatically, and not by using Auto Layout, as I am trying to deploy this app for iOS 4 and up. I basically set my view up with the screen height at 455(iPhone 5)(I know the actual screen size is 568 points, but I have a tab bar on the bottom so I am assuming the size shown in IB has taken that into account). I recorded the origin of all of my buttons and labels relative to the longer screen size and programmatically changed them to how I preferred them. The problem is, some of the buttons and labels are going off the screen now, so I think I have to perform some kind of conversion to keep them in bounds, but I am not sure what. See the pictures attached below:
Image before my initMethod is called to change button and label origins
Image after my initMethod is called to change the origin
View properties in XCode for the old screen size
This is how I want the view to Look. I moved the buttons around and recorded the origin of each.
Picture of my init method in viewWillAppear. Notice how the frame of the label is zero.
Here is the code I used:
-(void)initView
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 480) {
//iphone 4,4s
//done
}else if(screenBounds.size.height == 568) {
//iphone 5
CGRect frame;
//Static Professor label
frame = [staticProfessorLabel frame];
frame.origin.x = 48;
frame.origin.y = 218;
[staticProfessorLabel setFrame:frame];
//Professor Label
frame = [professorLabel frame];
frame.origin.x = 192;
frame.origin.y = 218;
[professorLabel setFrame:frame];
//show button
frame = [showProfessorButton frame];
frame.origin.x = 67;
frame.origin.y = 258;
[showProfessorButton setFrame:frame];
//clear proff button
frame = [clearProfessor frame];
frame.origin.x = 240;
frame.origin.y = 258;
//note label
frame = [bottomNote frame];
frame.origin.x = 160;
frame.origin.y = 310;
[bottomNote setFrame:frame];
//search button
frame = [searchButton frame];
frame.origin.x = 158;
frame.origin.y = 424;
[searchButton setFrame:frame];
//spinner
frame = [actIndicator frame];
frame.origin.x = 266;
frame.origin.y = 424;
[actIndicator setFrame:frame];
}
}
Also, I am calling this in the viewDidAppear method because when I call it in viewDidLoad, none of the buttons and labels give me a valid frame(I am guessing they are not initialized yet).
Your Xcode screenshots show that your storyboard contains constraints, so we can tell that your storyboard has autolayout turned on. If you want to set the view frames directly in code, you have to turn off autolayout.
Take a look at my answer here if you need help turning off autolayout on your storyboard.
Your hard-coded coordinates are simply wrong. Just double-check your values.
For example, bottomNote starts at x 160, which is clearly too far to the right.

Hide and show scrollview with animation

In my app I have a scrollview and when I press a button it is hidden when I press again it shows up.
I use scrollview.hidden = YES (or NO) to do it.
But I want to do it with an animation. For example, it may disappear from the bottom of the screen by moving and shows up with the same way. How can I do that?
edit:
[UIView beginAnimations:#"myAnimation" context:nil];
CGRect Frame = bottomScroller.frame;
if(Frame.origin.y == 380){
Frame.origin.y = 460;
}else{
Frame.origin.y = 380;
}
bottomScroller.frame = Frame;
[UIView commitAnimations];
This solved my problem...
You may check UIView animations. It's pretty easy. For example to animate translation you may use something like:
[UIView beginAnimations:#"myAnimation" context:nil];
CGRect Frame = yourView.frame;
Frame.origin.y = 0;
yourView.frame = Frame;
[UIView commitAnimations];
Then to move it back:
[UIView beginAnimations:#"myAnimation" context:nil];
CGRect Frame = yourView.frame;
Frame.origin.y = 100;
yourView.frame = Frame;
[UIView commitAnimations];
Changes in frame, alpha and some other parameters will be automatically animated.
You can use animations like so:-
[UIView animateWithDuration:2.0 animations:^{
[scrollview setframe:CGRectMake(xpos, ypos, width, height)];
}];
If you want to slide the scrollview on or off the screen set its y position - the bottom of the view is you want to hide it, the normal y position if you want to show it.
The 2.0 is an animation length, this can be changed to whatever you need it to be!
You can do this by using simple view animations. Here's an example of how you might do this:
// myScrollView is your scroll view
-(void)toggleShow{
CGFloat targetAlpha = myScrollView.alpha == 1 ? 0 : 1;
CGFloat yPosition = targetAlpha == 1 ? 0 : self.view.frame.size.height;
[UIView animateWithDuration:1.0 animations:^{
myScrollView.frame = CGRectMake(myScrollView.frame.origin.x, yPosition, myScrollView.frame.size.width, myScrollView.frame.size.height);
myScrollView.alpha = targetAlpha;
}];
}
targetAlpha will always be the opposite of the current state (1 or 0), and if it's 0 then the y position will be set to the bottom of the parent view. Using the new-school UIView animations API with blocks we can execute these changes to the scroll view over 1 second (in my example).
ScrollView appers from down side of screen with animation. I think this up-animation is what you want.
// ScrollView appearing animation
- (void)ScrollViewUpAnimation
{
// put the scroll view out of screen
CGRect frame = ScrollView.frame;
[self.view addSubview:ScrollView];
ScrollView.frame = CGRectMake(0, 460, frame.size.width, frame.size.height);
// setting for animation
CGPoint fromPt = ScrollView.layer.position;
CGPoint toPt = CGPointMake(fromPt.x, fromPt.y - frame.size.height - 44);
CABasicAnimation* anime =
[CABasicAnimation animationWithKeyPath:#"position"];
anime.duration = 0.2;
anime.fromValue = [NSValue valueWithCGPoint:fromPt];
anime.toValue = [NSValue valueWithCGPoint:toPt];
// change the position of Scroll View when animation start
[ScrollView.layer addAnimation:anime forKey:#"animatePosition"];
ScrollView.layer.position = toPt;
}