animation hide/show only occurring once - ios

im trying to do an animation on UIPicker where when my app runs its hidden and when the button is pressed it will appear sliding from the buttom and when i tap the button again it will slide away. well, my code does that but only just once.
it does:
first click -> show the picker, isPickerHidden = NO
second click -> hides picker, isPickerHidden = YES
third click -> does not do anything, but it returns isPickerHidden = NO
where in viewDidLoad declaration are;
and same as the fourth click it doesnt do anything but return the correct BOOL value.
isPickerHidden = YES;
[self.picker setHidden:isPickerHidden];
if(isPickerHidden == NO){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGRect frame = self.picker.frame;
[self.picker setFrame:CGRectOffset(frame, self.picker.frame.origin.x, self.picker.frame.origin.y)];
[UIView commitAnimations];
isPickerHidden = YES;
NSLog(#"hidden yes");
}else if(isPickerHidden == YES) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, -200);
self.picker.transform = transfrom;
[self.picker setHidden:NO];
[UIView commitAnimations];
NSLog(#"hidden no ");
isPickerHidden = NO;
}
i would like to know on whats the problem i am facing, and what am i missing. thanx

add some logging for the frame and picker to see what is happening...
NSLog(#"picker: %.0f, %.0f, %.0f, %.0f", self.picker.frame.origin.x, self.picker.frame.origin.y, self.picker.frame.size.width, self.picker.frame.size.height);

thanx NS bro for the reply but i did a different approach now instead of offsetting the points from the original one i used CGRectMake, i did this
if(isPickerHidden == NO) {
CGRect frame = self.picker.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.6];
[self.picker setFrame:CGRectMake(frame.origin.x, frame.origin.y + 216, frame.size.width, frame.size.height)];
[UIView commitAnimations];
isPickerHidden = YES;
} else if (isPickerHidden == YES) {
CGRect frame = self.picker.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.6];
[self.picker setFrame:CGRectMake(frame.origin.x, frame.origin.y - 216, frame.size.width, frame.size.height)];
[UIView commitAnimations];
isPickerHidden = NO;
}
both the sliding down and sliding up are doing well animated in the same manner. but now the problem is i wanted to make the UIPicker disappear on app load so in the viewDidLoad i did try to use the [self.picker setHidden:YES]; but the frames i made using CGRect dont appear when i press the button and i also tried alpha still nop.
i did also put in the body of the if-else statement the setHidden to yes and to know with respect to their values.
would like to know what im missing now.

Related

Button flip animation out of frame on the uiview

Update, SOLVED
Sorry for my english, but i will do my best.
I have a problem with a button flip animation.
I want the UIButtons to fill the UIView. But how do i do that?
Here is my problem.
I have two UIButtons in a UIView.
When i press the first button it will flip correctly but when i press the second button that it have flipped to the UIView is still the same size but the UIButton image is changing size to the original size of the image and it also move the image to the upper left corner off the view controller. The same thing will repeat every time it flip.
Here is the flip animation code.
- (IBAction) buttonPressedFlip {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
if (flipState == 0) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:buttonContainer cache:YES];
[self.btn1 removeFromSuperview];
[buttonContainer addSubview:btn2];
flipState = 1;
}
else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:buttonContainer cache:YES];
[self.btn2 removeFromSuperview];
[buttonContainer addSubview:btn1];
flipState = 0;
}
[UIView commitAnimations];
}
Have a nice day and thanks for any help!
I solved the problem like this
- (IBAction) buttonPressedFlip {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
if (flipState == 0) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:buttonContainer cache:YES];
[self.buttonContainer bringSubviewToFront:btn2];
flipState = 1;
}
else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:buttonContainer cache:YES];
[self.buttonContainer bringSubviewToFront:btn1];
flipState = 0;
}
[UIView commitAnimations];
}
I solved the problem
See my update code.

scrollView.setContentOffset does not scroll the view, at all

I've been following this and a bunch of other tutorials/blogs,etc trying to get my view to scroll when the user hits "return" and ends on a UITextField that is being covered by the keyboard but nothing is actually working.
I'm wondering what must I be doing or missing that is causing this?
Basically:
the user wants to do something in the app: add a credit card
show a UIView that contains all the CC fields
1/2 of the UITextFields are covered by the keyboard
scroll the view when the user gets there.
Nothing is happening. The code for detecting the 'covered by keyboard' case is being hit and executed but: [self.scrollView setContentOffset:scrollPoint animated:YES] has no effect at all.
Thoughts?
Reveal screenshot:
code: same as the tutorial in the link. Nothing new there.
Did you check the contentSize property?
To make a scroll view scrollable, the content must be larger than the display area(usually the bounds of the scrollview).
You can achieve this by explicitly setting the contentSize property which is CGSizeZero by default
In addition, setting the contentInset property to adjust the size of display area(in this case you can set bottom value equals keyboard height) when the keyboard is popped up
Try this,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.25 animations:^{
[yourTextField setFrame:CGRectMake(yourTextField.frame.origin.x, yourTextField.frame.origin.y-keyboardHeight, yourTextField.frame.size.width, yourTextField.frame.size.height)];
}];
}
and
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.25 animations:^{
[yourTextField setFrame:CGRectMake(yourTextField.frame.origin.x, yourTextField.frame.origin.y+keyboardHeight, yourTextField.frame.size.width, yourTextField.frame.size.height)];
}];
}
What I do to avoid the textField being covered by the keyboard is to animate the textfield to the top of the screen when the user touches it to start entering something with the keyboard and animate it back when done. It is much more elegant in my opinion. Maybe that could be an alternative solution for you... I do it for the different screensizes with different numbers... you might need to adjust it of course...
So listen to the textField delegate and in
- (void)textFieldDidBeginEditing:(UITextField *)textField {
you do
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -196; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 568){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -196; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 667){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -238; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 736){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -251; [self.view setFrame:frame]; [UIView commitAnimations];});
}
and in
- (void)textFieldDidEndEditing:(UITextField *)textField {
you just animate back
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = 0; [self.view setFrame:frame]; [UIView commitAnimations];});

UIViewanimation animationDidStop method

I have a 8 imagaviews that I have added to my view with size (0,0). Now what I want to do is to pull op each imageView after each other with a UIViewanimation. I want to do 2 animations. First the size should go to from (0,0) --> (105,85) and after that is finished the imageview size should go from (105,85) --> (93,75)
Now I have a method where I first place all my imageViews on the correct place.All the images have size (0,0) At the end I call the method startAnimating
-(void)startAnimating{
[self animageButton:[arrButtons objectAtIndex:0]];
}
Then the first view animation ( from (0,0) --> (105,85) )
-(void)animageButton:(UIImageView *)imgButton{
loopIndex++;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelegate:self];
CGRect btnFrame = imgButton.frame;
btnFrame.size.width = 105;
btnFrame.size.height = 85;
[UIView transitionWithView:self.view
duration:0.5f
options:UIViewAnimationOptionCurveEaseIn
animations:^{
imgButton.frame = btnFrame;
}
completion:nil];
[UIView commitAnimations];
[self animageButton2:imgButton];
}
this calls the second method (from (105,85) --> (93,75))
-(void)animageButton2:(UIImageView *)imgButton{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: #selector(animationDidStop:finished:context:)];
CGRect btnFrame2 = imgButton.frame;
btnFrame2.size.width = 93;
btnFrame2.size.height = 75;
[UIView transitionWithView:self.view
duration:0.5f
options:UIViewAnimationOptionCurveEaseIn
animations:^{
imgButton.frame = btnFrame2;
}
completion:nil];
[UIView commitAnimations];
}
Now at this point the first imageView should be animated. Now when the animation has finished. I to this.
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
//do smth
if(loopIndex <= 7){
NSLog(#"called");
UIImageView *imgObj = [arrButtons objectAtIndex:loopIndex];
[self animageButton:imgObj];
}else{
NSLog(#"done");
return;
}
}
What it is doing at the moment it is animating all the images at the same time. But I want that the next imageview starts animating when the previous has done.
Can anyone help me with this ?
-(void)startAnimating
{
NSTimeInterval delay = 0.0;
for(UIImageView *imageView in arrButtons)
{
[self performSelector:#selector(animageButton:) withObject:imageView afterDelay:delay];
delay +=1.0;
}
}
-(void)animageButton:(UIImageView *)imgButton{
CGRect btnFrame = imgButton.frame;
btnFrame.size.width = 105;
btnFrame.size.height = 85;
[UIView animateWithDuration:0.5f animations:^{
imgButton.frame = btnFrame;
} completion:^(BOOL finished) {
[self animageButton2:imgButton];
}];
}
-(void)animageButton2:(UIImageView *)imgButton
{
CGRect btnFrame2 = imgButton.frame;
btnFrame2.size.width = 93;
btnFrame2.size.height = 75;
[UIView animateWithDuration:0.5f animations:^{
imgButton.frame = btnFrame2;
} completion:^(BOOL finished) {
}];
}
I have not tested the code. Let me know if there are any issues. Thanks.

How do I animate my subviews on adding them?

I have developed a font selection that utilizes UIPickerView and UIToolbar, which are both added on touching a UIButton.
But they just kind of pop in which looks sloppy.
is there a way to animate them?
here is the method that adds them (it's called on clicking the button) :
-(void)showPicker
{
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
}
You can animate them in a couple of ways, but probably the easiest is to just fade them in.
Fade In
[someView setAlpha:0]
[self.view addSubview:someView];
[UIView beginAnimations:#"FadeIn" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:1];
[UIView commitAnimations];
Fade Out
float fadeDuration = 0.5;
[UIView beginAnimations:#"FadeOut" context:nil];
[UIView setAnimationDuration:fadeDuration ];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:0];
[UIView setAnimationDidStopSelector:#selector(removeView)];
[UIView commitAnimations];
-(void) removeView {
[someView removeFromSuperview];
}
Something as simple as that.
Depends on what type of animation you want to use. For example, this would look like dissolve.
Anyways look into UIView animateWithDuration...
pickerTB.alpha = _fontPicker.alpha = 0;
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
[UIView animateWithDuration:1 animations:^{_fontPicker.alpha = 1; pickerTB.alpha = 1}];
You could use transforms or change frame origins to make the views fly in from positions outside the screen too. In that case, inside the animations block, specify the new on screen position. You can do this in two ways, change the frame with an updated origin or apply a CGAffineTransformMakeTranslation(dx,dy)
You need to add the subview with a frame value that is where you want the animation to begin from (off-screen?) then change the frame value inside your animation block. The frame will change over the duration, causing the appearance of movement. The view must already be a subview - I don't believe adding a subview is something you can animate, it makes no sense.
-(void) showPicker{
[self.view addSubview: _fontPicker];
_fontPicker.frame = CGRectMake(0, 0, 120, 40);// somewhere offscreen, in the direction you want it to appear from
[UIView animateWithDuration:10.0
animations:^{
geopointView.frame = // its final location
}];
}
Initially set your picker view frame and tool bar frame like this one..
-(void)viewDidLoad
{
[super viewDidLoad];
pickerTB.frame = CGRectMake(0,568,320,44);
fontPicker.frame = CGRectMake(0, 568, 320, 216);
}
-(void)showPicker
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.30];
if(isiPhone5)
{
pickerTB.frame = CGRectMake(0,288,320,44);
fontPicker.frame = CGRectMake(0, 332, 320, 216);
}else{
pickerTB.frame = CGRectMake(0,200,320,44);
fontPicker.frame = CGRectMake(0,244, 320, 216);
}
[UIView commitAnimations];
}

how to move view upside while entering any text in input field

I have app i want that when user enters any data then view should slide up.
I am writing this code this code works fine in another app but not working in this app.I am following same way.
-(void)showAnimationBack{
NSLog(#"Back Animation is Working");
[UIView animateWithDuration:0.5
animations:^{
self.subViewLand.frame = CGRectMake(0,-10,1024,768);
}];
}
-(void) showAnimationPests{
NSLog(#" Animation is Working");
[UIView animateWithDuration:0.5
animations:^{
self.subViewLand.frame = CGRectMake(0,-200,1024,768);
}];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
self.subViewLand.frame = CGRectMake(0,-200,1024,768); // or to self.view.frame = CGRectMake(0,-200,1024,768);
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
self.subViewLand.frame = CGRectMake(0,-200,1024,768); // or to self.view.frame = CGRectMake(0,-200,1024,768);
[UIView commitAnimations];
}
This code will work
in textfielddidBeginEditing Method set view frame like this
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.subViewLand setFrame:CGRectMake(0, -200, 320, 480)];
}
in textfielddidEndEditing Method set view frame as it is.
- (void)textFieldDidEndEditing:(UITextField *)textField{
[self.subViewLand setFrame:CGRectMake(0, 0, 320, 480)];
}
It will definitely work. :)
In textFieldDidBeginEditing() call the your method showAnimationPests() and similarly in textFieldDidEndEditing() call showAnimationBack() .
This will work fine for you.

Resources