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);
}];
}
Related
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.
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];
}
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];
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
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];