animate textlabel in uitableviewcell using willTransitionToState - iphone

I am trying to animate the textlabel in a UItableviewcell when I press the edit button.
I am trying to make it fade out and fade in.
fading in works but when I press 'edit' the textlabel disappears and when I press on 'done' I fades in just perfectly.
Can anyone tell me why it isn't working?
thanks in advance
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if ((state & UITableViewCellStateEditingMask) || (state & UITableViewCellStateShowingDeleteConfirmationMask)) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
label.alpha = 0.0;
[UIView commitAnimations];
}
}
- (void)didTransitionToState:(UITableViewCellStateMask)state {
[super didTransitionToState:state];
if (!(state & UITableViewCellStateEditingMask) && !(state & UITableViewCellStateShowingDeleteConfirmationMask)) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
label.alpha = 1.0;
[UIView commitAnimations];
}
}

I noticed that when entering willTransitionToState that animations were disabled. The following fixed it.
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
//Should be enabled by default...but apparently not
[UIView setAnimationsEnabled:YES];
...
}

From everything I had read I thought for sure the willTransitionToState was the way to go. It even works perfectly if you use didTransitionToState though the transition starts after the normal editing transition finishes.
As it turns out I think you want to use setEditing
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[super setEditing:editing animated:animate];
if(editing) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
label.alpha = 0.0;
[UIView commitAnimations];
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
label.alpha = 1.0;
[UIView commitAnimations];
}
}

Related

UIButton actions

I have a UIButton set up in a storyboard that when pressed animates a UIVIew. I would like the same button to animate/move the UIView back to it's original point when it is pressed a second time.
What is the best way to do this?
thanks for any help or pointers.
this is the code I'm using to animate and move the UIVIew:
I'm using a BOOL named buttonCurrentStatus.
-(IBAction)sortDeals:(UIButton*)sender {
if (buttonCurrentStatus == NO)
{
buttonCurrentStatus = YES;
CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.menuTop.frame = menuTopFrame;
[UIView commitAnimations];
NSLog(#"Sort Deals touched button status = yes");
} else {
buttonCurrentStatus = NO;
CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.menuTop.frame = menuTopFrame;
[UIView commitAnimations];
NSLog(#"Sort Deals touched button status = no");
}
Another more general way is to use an IBAction that will execute one of two methods. Like previous answers, you can use a BOOl value, or int in order to determine what action to use. This is a more general answer, and can be used for many purposes.
First, you will need a BOOL variable. For this example I put the Bool variable as boolVarible (Yes, I know I spelled it wrong). By default I have it set to YES.
bool boolVarible = YES;
I made that a class variable.
-(IBAction)buttonAction:(id)sender {
if (boolVarible == YES)
{
//Execute first action
[self firstAction];
}
else
{
//Execute second action
[self secondAction];
}
}
-(void)firstAction {
//Do something here...
}
-(void)secondAction {
//Do something here...
}
Hopefully, you get the idea. You can then swap actions whenever you want. Just simply change the BOOl value, and then when the button is pressed, it will execute another method. I find this cleaner than doing it all in one action. Good luck.
-(IBAction)sortDeals:(UIButton*)sender {
sender.selected = !sender.selected;
int moveby = (sender.selected) ? 30: -30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
menuTop.frame = CGRectMake(menuTop.frame.origin.x, menuTop.frame.origin.y + moveby, menuTop.frame.size.width, menuTop.frame.size.height);
[UIView commitAnimations];
}
Very Simply Solution for your Question is if you Declare two Boolean Variables in Class where you need to implement this method.your Code will be look like this
In .m file
BOOL Action1;
BOOL Action2;
Now In ViewDidload method
(void)viewDidLoad
{
Action1=TRUE;
Action2=FALSE;
[super viewDidLoad];
}
And now your Method
(IBAction)sortDeals:(UIButton*)sender {
if (Action1 == TRUE)
{
CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.menuTop.frame = menuTopFrame;
[UIView commitAnimations];
NSLog(#"Sort Deals touched button status = yes");
Action1=FALSE;
Action2=TRUE;
}
else
if (Action2 == TRUE)
{
CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.menuTop.frame = menuTopFrame;
[UIView commitAnimations];
NSLog(#"Sort Deals touched button status = no");
Action1=TRUE;
Action2=FALSE;
}
Hope it Works for you.Thanks

How to Move View up when Click on UITextField in CustomCell of UITableview?

I have TextField in Custom Cell of UITableview .my Tableview is lower part of MainView so When i Click on TextField in CustomCell keyboard appear Which HIde my textfield.My below image show my problem
Here i have a Code which works when i have UITextField in MainView.please edit the Below Code Beacuse i want whole view Animation when i click on UITextfield.
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField.tag > 0) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y-165.0,
self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textresign{
[[self.view viewWithTag:1]resignFirstResponder];
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
if (textField.tag > 0) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view .frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+165.0,
self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
But i did't know how to Fix it for my Custom cell UITextField.Any help will be appriated.
here is the code ,just need to change code little bit
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField.tag > 0)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y-165.0,
self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
use below method for repositioning the view on keyboard resigning instead of textFieldDidEndEditing.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField.tag > 0)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view .frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+165.0,
self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
[textField resignFirstResponder];
return YES;
}
using below method it mandatory to set the delegate to the textfield.
i hope you could get the solution.

Flip View Animation Not Working

I am working on an iPad app that presents a question to the user in a view. When they answer the question, I would like the view to transition to another view that contains the next question. To make it look all fancy, I am trying to add a curl transition to it but the code I wrote does not work can I can't see to find the problem. It does show the correct view but there is no transition animation. What's with that? Here is the method I use to transition:
- (void)pageChangedTo:(NSInteger)page {
if ( (page == currentQuestionNumber) || (page > ( [self.allQuestions count] - 1 ) ) || (page < 0) ) {
return;
}
AskQuestionView *view = [self.questionViews objectAtIndex:page];
UIViewAnimationTransition transition;
if (page > currentQuestionNumber) {
transition = UIViewAnimationTransitionCurlUp;
}
else {
transition = UIViewAnimationTransitionCurlDown;
}
if (self.containerView1.superview) {
self.containerView2 = view;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:transition forView:self.containerView1 cache:YES];
[self.containerView1 removeFromSuperview];
[askQuestionsView addSubview:self.containerView2];
[UIView commitAnimations];
}
else {
self.containerView1 = view;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:transition forView:self.containerView2 cache:YES];
[self.containerView2 removeFromSuperview];
[askQuestionsView addSubview:self.containerView1];
[UIView commitAnimations];
}
currentQuestionNumber = page;
}
Can anyone tell me why this isn't working? I would very much appreciate it!
Set your animation transition forView: _container of_self.containerView2: not the one that's being removed, but the one it's being removed from.

Problem with simple animation

I am using animation on a button click first time show a view and second ti me hide a view.
here is my code for hiding a view
-(IBAction)clickme
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDelegate:self];
[view1 setAlpha:0.0];
[UIView commitAnimations];
}
similar code is there for showing the view.
But the problem arises when user click the button many times again and again....means i am using 2 seconds for my animation but if user presses the same button in during the animation then the output result is very bad.
I don't want to disable that button during the period of animation.
Is there any other way?
You need to keep track of whether there's an animation going on, and ignore the click if it is.
Declare an instance variable BOOL animating; in your class header, and initialise it to NO in your init.
Then,
-(IBAction)clickme
{
if (animating) return;
animating = YES;
[UIView beginAnimations:nil context:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDelegate:self];
[view1 setAlpha:0.0];
[UIView commitAnimations];
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if (context == self)
animating = NO;
}
try to use + (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState:
-(IBAction)clickme
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDelegate:self];
[view1 setAlpha:0.0];
[UIView commitAnimations];
}

Animation code doesnt animate?

I have a view with which I have a button calling the following method. The view hides/shows but without any animation
- (void) displayEvent:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.5];
modal.hidden = !modal.hidden;
[UIView commitAnimations];
}
Any ideas?
There's no states in between hidden and non-hidden. How to animate?
To have the fade-in effect you should modify the alpha property.
- (void) displayEvent:(id)sender {
BOOL wasHidden = modal.hidden;
modal.hidden = ! wasHidden;
modal.alpha = ! wasHidden; // wasHidden ? 0 : 1;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.5];
modal.alpha = wasHidden; // wasHidden ? 1 : 0;
[UIView commitAnimations];
}