UIView beginanimations: subview shall not animate - iphone

Hi I have a UIView to which I add a UIButton in its initWithFrame: method like this:
UIButton * dropB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
dropB.frame = CGRectMake(190, 22, 52, 22);
dropB.backgroundColor = [UIColor clearColor];
dropB.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:11];
[dropB setTitle:#"DROP" forState:UIControlStateNormal];
[dropB addTarget:viewController
action:#selector(dropSelectedObject)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:dropB];
Then, as I try to animate the view with a resizing like this:
[UIView beginAnimations:#"MoveIt" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.dropButton.center = CGPointMake(self.dropButton.center.x + 80, self.dropButton.center.y -10);
self.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
self.center = CGPointMake(CENTER_X, CENTER_Y);
[UIView commitAnimations];
The button only gets translated to a new position which is the previous one (the original coordinates were relative to the view i guess). How can I make it translate to the point i specify?

Ok it was pretty simple (and stupid).
Since I needed the UIButton to remain at a fixed height I had to specify what space was 'flexible' while resizing. This can be done using the autoresizingMask property of an UIView this way:
dropB.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleBottomMargin;
and not calling anithing among the animation instructions

Related

SubView hierarchies not working (SubView hidden below another)

I'm having a problem here with designing my UI in Xcode.
I've programmatically created(when a button is pressed) a UIImageView(black, transparent overlay), UITextView(editable), and a UIButton to clear these former two UISubviews.
This problem is all appearing both on the Simulator, and my iPhone.
This is my code
- (IBAction)openCommentTextView:(id)sender {
doneButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 40, 60, 35)];
doneButton.alpha = 0.0;
doneButton.layer.cornerRadius = 7;
doneButton.layer.borderColor = [[UIColor colorWithWhite:1.0 alpha:1.0] CGColor];
doneButton.layer.borderWidth = 3;
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
[doneButton setTitleColor:[UIColor colorWithWhite:1.0 alpha:1.0] forState:UIControlStateNormal];
doneButton.titleLabel.font = [UIFont fontWithName:#"AvenirNext-Regular" size:15];
[doneButton addTarget:self action:#selector(finishEditing:) forControlEvents:UIControlEventTouchUpInside];
commentTextView = [[UITextView alloc] initWithFrame:CGRectMake(20, 90, 280, 230)];
[commentTextView becomeFirstResponder];
commentTextView.alpha = 0.0;
if ([commentsTextViewViewer.text isEqual: #"Comment..."]) {
commentsTextViewViewer.text = #"";
} else {
commentTextView.text = commentsTextViewViewer.text;
}
commentTextView.keyboardAppearance = UIKeyboardAppearanceDark;
commentTextView.layer.cornerRadius = 7;
commentTextView.font = [UIFont fontWithName:#"GillSans" size:20];
commentTextView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.4];
backgroundOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 330, 568)];
backgroundOverlay.backgroundColor = [UIColor colorWithWhite:0.0 alpha:1.0];
backgroundOverlay.alpha = 0.0;
[self.view insertSubview:doneButton atIndex:11];
[self.view insertSubview:commentTextView atIndex:10];
[self.view insertSubview:backgroundOverlay atIndex:9];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
commentTextView.alpha = 1.0;
doneButton.alpha = 1.0;
[UIView commitAnimations];
[UIView setAnimationDuration:0.3];
backgroundOverlay.alpha = 1.0;
[UIView commitAnimations];
}
However, somehow the UIButton(doneButton) and the UITextView(commentTextView) is under the UIImageView(black overlay). Both the UIButton and the UITextView responds. (I set the UIImageView(black overlay) alpha to 1.0, and the UIButton and UITextView didn't appear, although both responded)
It turned out like this (sorry I couldn't include the image directly into here... I didn't have enough reputation)
But when I copied this code and pasted into a new single-view application, it worked perfectly as I wished it to. (like this)
I really have no idea why this is happening... I've searched all over the web but with no success.
Is this something to do with XCode, or my iPhone itself?
I'd be very happy if someone can help me work this out.
Thank you.
A couple of things that might help. I've never had much luck in terms of the atIndex: method of view insertion except index 0. Try using relative position, since you know the button and textview NEED to be above the background overlay.
[self.view addSubview:backgroundOverlay];
[self.view insertSubview:doneButton aboveSubview:backgroundOverlay];
[self.view insertSubview:commentTextView aboveSubview:backgroundOverlay];
Also, you'll need a second call to [UIView beginAnimations:nil context:nil]; before animating the second bit of code. Or, alternatively, use the UIView animation blocks which is recommended if you're targeting iOS 4 or higher.

beginAnimations: subview's animation slower then view's animation

I have a little problem, I try to display a view with an animation in this way:
self.vistaAiuti = [[UIView alloc] initWithFrame:CGRectMake(10, -200, 300, 200)];
self.vistaAiuti.backgroundColor = [UIColor blueColor];
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
closeButton.frame = CGRectMake(50, 140, 220, 40);
[closeButton setTitle:#"Go back" forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(closeView:) forControlEvents:UIControlEventTouchUpInside];
[self.vistaAiuti addSubview:closeButton];
[self.view addSubview:self.vistaAiuti];
[UIView beginAnimations:#"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
self.vistaAiuti.frame = CGRectMake(10, 0, 300, 200);
[UIView commitAnimations];
and this for close it:
[UIView beginAnimations:#"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
self.vistaAiuti.frame = CGRectMake(10, -200, 300, 0);
[UIView commitAnimations];
the problem is that the button on vista aiuto is slower then the vistaAiuti, so when I close the view the button remains behind for some second...what I have to do for for have the same velocitiy?
The problem is that the vistaAiuti frame is being set to zero height in the close animation. The button appears to lag, but what's really happening is that the parent view underneath is shrinking to zero height and -200 origin.y.
Change the close animation target frame to:
self.vistaAiuti.frame = CGRectMake(10, -200, 300, 200);
Also, some other advice:
Separate the creation of the view from showing it. This way you don't add another subview every time you want to show it.
- (void)addVistaAiutiView {
// your creation code
self.vistaAiuti = [[UIView alloc] initWithFrame:CGRectMake(10, -200, 300, 200)];
self.vistaAiuti.backgroundColor = [UIColor blueColor];
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
closeButton.frame = CGRectMake(50, 140, 220, 40);
[closeButton setTitle:#"Go back" forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(closeView:) forControlEvents:UIControlEventTouchUpInside];
[self.vistaAiuti addSubview:closeButton];
[self.view addSubview:self.vistaAiuti];
}
Use block animation, it's much more compact to write and easy to read
- (BOOL)vistaAiutiIsHidden {
return self.vistaAiuti.frame.origin.y < 0.0;
}
- (void)setVistaAiutiHidden:(BOOL)hidden animated:(BOOL)animated {
if (hidden == [self vistaAiutiIsHidden]) return; // do nothing if it's already in the state we want it
CGFloat yOffset = (hidden)? -200 : 200; // move down to show, up to hide
NSTimeInterval duration = (animated)? 0.5 : 0.0; // quick duration or instantaneous
[UIView animateWithDuration:duration animations: ^{
self.vistaAiuti.frame = CGRectOffset(0.0, yOffset);
}];
}

searchDisplayControllerWillEndSearch not resizing searchbar

Just wondering why and need to solve it. i have this searchbar looks like this.
which clicked will move up to the top and expend to 0,0,320,searchbar's height.
which clicked Cancel, it should resize back to 33,55,270, searchbar's height. but it doesn't
the searchDisplayControllerDidEndSearch will work to resize back, however there is a 1 sec lag that user could see the changes. any body why in searchDisplayControllerWillEndSearch the animation resize wound't work ?.
thanks for the comments and feedback.
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
[UIView animateWithDuration:.1
animations:^{
controller.searchResultsTableView.frame = frogTableView.frame;
searchDisplayController.searchResultsTableView.frame = frogTableView.frame;
searchDisplayController.searchBar.frame = CGRectMake(32,
55,
270,
searchDisplayController.searchBar.frame.size.height);
searchBar.frame = CGRectMake(32,
55,
270,
searchBar.frame.size.height);
frogTableView.frame = CGRectMake(frogTableView.frame.origin.x,
121,
frogTableView.frame.size.width,
frogTableView.frame.size.height);
notePad.frame = CGRectMake(notePad.frame.origin.x,
45,
notePad.frame.size.width,
notePad.frame.size.height);
searchDisplayController.searchResultsTableView.frame = frogTableView.frame;
}
completion:^(BOOL finished){
//whatever else you may need to do
}];
}
I had the same trouble after I started to use UISearchBar with UISearchDisplayController. The solution I came up with is to place a search bar on a view:
UIView * searchBarSubview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MENU_WORK_AREA_WIDTH, SEARCH_BAR_HEIGHT)] ;
searchBarSubview.autoresizesSubviews = YES;
searchBarSubview.backgroundColor = [UIColor clearColor];
UISearchBar * globalSearchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, MENU_WORK_AREA_WIDTH, SEARCH_BAR_HEIGHT)] autorelease];
globalSearchBar.tintColor = [UIColor blackColor];
globalSearchBar.delegate = self;
[globalSearchBar setBackgroundImage:[UIImage alloc]];
and resize the view instead of search bar in searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
// Slide in to the final destination
searchBarSubview.frame = CGRectMake(0, 0, MENU_WORK_AREA_WIDTH, SEARCH_BAR_HEIGHT);
[UIView commitAnimations];

Loading a view from the bottom of the window using animations

i wanted to use animations to load a view from the bottom of the window, i know this can be done via presentmodalViewController but in as per the requirement of my app its not valid as i only want to load the view at half of the window. So i used Animations and heres a look at what i did
-(void) displayPicker
{
UIButton *done = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[done setFrame:CGRectMake(197, 199, 103, 37)];
[done setBackgroundImage:[UIImage imageNamed:#"button_0.png"] forState:UIControlStateNormal];
[done setTitle:#"Done" forState:UIControlStateNormal];
[done setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[done addTarget:self action:#selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
[pickerDisplayView addSubview:datePicker];
[pickerDisplayView addSubview:done];
//animating here
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
[self.view addSubview:pickerDisplayView];
[UIView commitAnimations];
}
The view called pickerDisplayView has two components called Picker and Button, but the problem is that Picker is not working correctly and neither the view (pickerDisplayView) is loading in a smooth way.
#Matt: I have followed ur instructions and did as u advised here's what i did
-(void) animationIn
{
CGPoint p = {x:0,y:185};
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
//pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
[pickerDisplayView setCenter:p];
[UIView commitAnimations];
}
But the prob is that the view is going at the top of the window and is not coming at exact y axis of 185. I took these coordinates from IB. Please help me sir
You will have to add the view offscreen and then animate it in.
// In your viewDidLoad or wherever you initialize the view.
pickerDisplayView = [[UIView alloc]initWithFrame:offscreenRect];
[self.view addSubview:pickerDisplayView];
[self performSelector:#selector(animateIn) withObject:nil afterDelay:0.25];
// Declare animateIn
- (void)animateIn
{
UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[pickerDisplayView setCenter:onscreenPosition];
[UIView commitAnimations];
}
The value offscreenRect is a CGRect for the size and position where you want the view to start. onscreenPosition is a CGPoint for the position where you want the animation to finish. Animating the view's center is adequate since you don't want to change the size of the view while animating.
Also, I placed the animation in a separate method that gets called with -performSelector:withObject:afterDelay to ensure that the animation with fire. Sometimes you won't see the animation run if you start it in the same run loop where you added the view you're animating.
You need to init and addSubview before beginAnimations:. When you init pickerDisplayView position it so that the top of it is inline with the bottom of the superview. The animation will then slide up pickerDisplayView. Ideally you should create the view in a nib or in the loadView method. (I strongly recommend using nibs over 'loadView').
Do this in loadView (or move to a nib):
CGRect offScreenFrame = ?????; //Figure this out from self.view
self.pickerDisplayView = [[UIView alloc]initWithFrame:offScreenFrame];
[self.view addSubview:self.pickerDisplayView];
This is the animation:
[UIView beginAnimations:nil context:NULL];
self.pickerDisplayView.frame = CGRectMake(0, 185, 320, 275);
//[UIView setAnimationDuration:0.5]; //this line is probably not needed
[UIView commitAnimations];
Also I'd advice against using 'magic numbers' in the frame. Instead you can derive the pickerDisplayView frame from the superviews frame. If you do use 'magic' numbers be sure explain them in some way (ie assigning them to a appropriately named variable or a comment).

A Label Entering From Top Animated to current View in iPhone Application

How do I add a label which enters to my current view controller,
Animating from top;
i.e.
like a slider, it should come on screen,
I want to display details on it,
i know
[self.view addSubView:myNewLabel];
but it doesn't make any animation,
I need animation.
I think, I got the Answer by r&d.
Go to your view controller,
get to the viewDidLoad Method
add following code,
UIView *aNewView;
aNewView=[[UIView alloc] initWithFrame:CGRectMake(0, -90, 320, 90)];
[aNewView setBackgroundColor:[UIColor brownColor]];
[aNewView setAlpha:0.87];
UILabel *titleLabel;
titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(20, 25, 280, 30)];
titleLabel.font=[UIFont systemFontOfSize:15];
titleLabel.text=#"SRK - Sagar R Kothari";
titleLabel.textColor=[UIColor whiteColor];
titleLabel.backgroundColor=[UIColor clearColor];
[aNewView addSubview:titleLabel];
CGRect frame = aNewView.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 90;
frame.origin.x = 0;
frame.size.height=400;
self.view.frame = frame;
[UIView commitAnimations];
[self.view addSubview:aNewView];