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

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];

Related

Inserting a subview and changing position of another view at the same method

I am trying to add a subview (UITextField) to a view and change the frame property of a UITextView in the same method but it's not working as I was expecting. This is the code:
UITextField *textField = [[UITextField alloc] init];
textField.backgroundColor = [UIColor redColor];
textField.frame = CGRectMake(0, 120, 240, 40);
[UIView beginAnimations:#"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
self.tvContent.frame = CGRectMake(0, 80, self.tvContent.frame.size.width, self.tvContent.frame.size.height);
[UIView commitAnimations];
[self.view insertSubview:textField aboveSubview:self.tvContent];
Note: tvContent is the UITextView object which I am trying to reposition.
The problem I am facing is that the UITextView isn't moving at all IF the last line of code (insertSubview) is in place. The UITextField does show up though. However, if I remove the last line , the textview change its position just fine.
I have also tried without animation by just setting the frame property of self.tvContent and same behavior occurred.
Any ideas to fix this?
Do like this,
UITextField *textField = [[UITextField alloc] init];
textField.backgroundColor = [UIColor redColor];
textField.frame = CGRectMake(0, 120, 240, 40);
[UIView beginAnimations:#"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
self.tvContent.frame = CGRectMake(0, 80, self.tvContent.frame.size.width, self.tvContent.frame.size.height);
[UIView setAnimationDidStopSelector:#selector(animationFinish)];
[UIView commitAnimations];
-(void)animationFinish {
[self.view insertSubview:textField aboveSubview:self.tvContent];
}
Is there a reason you are still using the good ol' beginAnimations thing?
This would do the same:
[UIView animateWithDuration:0.5 animations:^(void) {
self.tvContent.frame = CGRectMake(0, 80, self.tvContent.frame.size.width, self.tvContent.frame.size.height);
} completion:^(BOOL finished) {
[self.view addSubview:textField];
}];
EDIT: What happens if you just addSubView it?

Flip SubView on UIScrollView not working

I'm coding an iPhone TabBar Project with UiNavigation, the first view contain UIScrollView and the scrollview contain views every subview contain imageview,anUiImage and a UiButton follows
is the function that build the first screen
//*******************************************************
//* Build the main screen *
//*******************************************************
-(void) buildScreen{
sing.viewsArray = [[NSMutableArray alloc]init];
int Vve = 10;
int Vho = 10;
int wi = 95;
int hi = 95;
int ArrayIndex = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
staticView = [[UIView alloc] initWithFrame:CGRectMake(Vve, Vho, 100, 100)];
[staticView setTag:ArrayIndex];
staticImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[sing.stdPicArray objectAtIndex:ArrayIndex]]];
[staticImage setFrame:CGRectMake(0, 0, wi, hi)];
[staticView addSubview:staticImage];
viewButton = [UIButton buttonWithType:UIButtonTypeCustom];
viewButton.frame = CGRectMake(0, 0, wi, hi);
[viewButton addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[viewButton setTag:ArrayIndex];
[sing.buttonsArray addObject:viewButton];
[staticView addSubview:viewButton];
[sing.viewsArray addObject:staticView];
[MainScroll addSubview:[sing.viewsArray objectAtIndex:ArrayIndex]];
Vve += 100;
ArrayIndex++;
}
Vve = 10;
Vho += 100;
}
}
When I click on the UiButton I want to add another UIView to the UIScrollView and make a "UIViewAnimationTransitionFlipFromLeft" when the view is added to the scroll view
tried the following code
-(void)animateFlip{
[UIView transitionWithView:flipViewBack
duration:1.0
options:UIViewAnimationTransitionFlipFromLeft
animations:^{ [self.view addSubview:flipViewBack]; }
completion:^(BOOL f){ }];
}
didn't work just added the flipViewBack
-(void)animateFlip{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.flipViewFront
cache:YES];
[MainScroll addSubview:flipViewFront];
[UIView setAnimationDidStopSelector:#selector(flipAnimationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
didn't work just like the other one, the next code did flip the main view with the view I wanted, but it didn't flip only the the view I want
-(void)animateFlip{
[UIView beginAnimations:nil
context:nil];
[UIView setAnimationDuration:1.0];
[UIView transitionFromView:self.view
toView:flipViewBack
duration:2
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:NULL];
[UIView setAnimationDidStopSelector:#selector(flipAnimationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
//set transformation
[UIView commitAnimations];
}
Does any body know how to do it' I looked everywhere and all the code I found didn't work for me
Thanks
I tried another option suggested in this link
iPhone subview flip between 2 views and it didn't work for me
this is the function that I use on click in the last 3 lines im adding to the flipView (container) the flipViewFront view then I add the flipView to the mainScroll view and then I go to execute the animation in the animateFlip that I copied from your sample
//*******************************************************
//* Click on the picture as button *
//*******************************************************
-(void) buttonClick:(id) sender{
UIButton *button = (UIButton *)sender;
int row = [button superview].tag;
NSLog(#"Button pressed tag: %i",row);
self.flipView = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipView.backgroundColor = [UIColor clearColor];
self.flipViewBack = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipViewFront = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipViewFront.backgroundColor = [UIColor whiteColor];
self.flipViewImage = [UIImage imageNamed:[sing.stdPicArray objectAtIndex:row]];
self.filpImage = [[UIImageView alloc]initWithImage:self.flipViewImage];
[self.flipViewBack addSubview:self.filpImage];
self.flipCloseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.flipCloseButton setFrame:CGRectMake(0, 0, 24, 24)];
[self.flipCloseButton setImage:[UIImage imageNamed:#"close_button.png"] forState:UIControlStateNormal];
[self.flipCloseButton addTarget:self action:#selector(closwFlip:) forControlEvents:UIControlEventTouchUpInside];
[self.flipViewBack addSubview:flipCloseButton];
self.flipOkButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 0, 190, 190)];
[self.flipCloseButton addTarget:self action:#selector(continueTo:) forControlEvents:UIControlEventTouchUpInside];
[self.flipViewBack addSubview:flipOkButton];
[flipView addSubview:flipViewFront];
[MainScroll addSubview:flipView];
[self animateFlip];
}
found the problem, but not the answer, if it works directly from the ViewDidLoad it and the first view is on screen all is well and the flip works' if I change it so that the want to add a view and flip it in one event it shows me the second(flipped) view without the flip
Your problems seems to be that you are trying to animate the flip on the view before it is added to the view hierarchy.
Instead you should add the back view to a common container view (that later will contain the front view) and then flip the container view while adding the front and removing the back, like this:
// Container view is a view that is already added to your view hierarchy.
// It contains the back view. The front view will be added to it.
[UIView transitionWithView:containerView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[backView removeFromSuperview]; // This is already added to the container
[containerView addSubview:frontView]; // Not yet added ...
}
completion:NULL];
Update
This is all the code I wrote when answering this question. It works for me. If it still doesn't animate for you then you probably haven\t set up your view hierarchy the same way.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
containerView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
[self.view addSubview:containerView];
frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[frontView setBackgroundColor:[UIColor orangeColor]];
backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[backView setBackgroundColor:[UIColor redColor]];
[containerView addSubview:backView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(flip)];
[self.view addGestureRecognizer:tap];
}
- (void)flip {
[UIView transitionWithView:containerView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[backView removeFromSuperview];
[containerView addSubview:frontView];
}
completion:NULL];
}

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];

UIView beginanimations: subview shall not animate

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

moving UILabel upside with NSTimer

HI all,
i want to move a UILabel up for 5 sec and gets hidden.
Something we see in games, when we get bouus or something,
a text comes up ,moves up saying something "+300" etc.
How to do move UIlabel?
regards
Have a look at the UIView animation methods
i.e.
- (void)showScoreLabelFor:(int)score {
// Make a label
UILabel *scoreLabel = [UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)] autorelease];
// Set it's text to the score we want to show
[scoreLabel setText:[NSString stringWithFormat:#"%+i", score]];
// Center it in the view
[scoreLabel sizeToFit];
[scoreLabel setCenter:CGPointMake([[self view] center].x, 200)];
// Add it to the view
[[self view] addSubview:scoreLabel];
// Animate it up 100 pixels ver 2 seconds
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
CGRect frame = [scoreLabel frame];
frame.origin.y -= 100;
[scoreLabel setFrame:frame];
[UIView commitAnimations];
}