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];
}
Related
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
I am havng one textfield and two textViews...while writting something in 1 conrol the keyboard is poped up...but when I want to shift on another control my keyboard doesn't let me write as it covers the whole view...hw can I solve my problem?
you should move your view up, so that the keyboard doesnt cover the textfield/ textview. something like this...
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == *textFieldName*)
{
[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 - 65.0), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField == *textFieldName*)
{
[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 + 65.0), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
and for the textView use:
- (void)textViewDidBeginEditing:(UITextView *)textView
and
- (void)textViewDidEndEditing:(UITextView *)textView
Use delegate functions provided for uitextview and uitextfiled ....
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 150; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
[self animateTextField: textField up: YES];
}
- (void)textViewDidEndEditing:(UITextView *)textView{
[self animateTextField: textField up: NO];
}
you can shift your whole view up or just shift the controls (text field and text view) up .. or make your view scrollable so the user can scroll down while the keyboard is visible.
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.
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];
}
}
I would like to do a fade in and out of a UIImageView using different times, let's say, using the following parameters:
t = 0 ... UIImageView's alpha = 0
t = 0.5s ... UIImageView's alpha = 0.7
t = 0.7s ... UIImageView's alpha = 0
Is this possible to do with CAAnimation or other method?
How can that be done?
thanks for any help!
if (imgDefault.alpha == 0.0) {
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration: 3.0];
[UIView setAnimationDelegate: self];
imgDefault.alpha = 1.0;
[UIView commitAnimations];
}
else {
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration: 3.0];
[UIView setAnimationDelegate: self];
imgDefault.alpha = 0.0;
[UIView commitAnimations];
}
hope it helps
You should probably look into CAKeyframeAnimation. It'll let you set values for multiple time points.
UIView has a setAnimationDidStopSelector: method which you can use. Simply setup your fade in animation using a beginAnimations block and set the didStop selector to another method which contains only the fade out animation block. Each of these animation blocks can have different animation durations.
Something like this:
[UIView beginAnimations:next context:context];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(fadeOut:finished:context:)];
myView.alpha = 0.7;
[UIView commitAnimations];
-(void)fadeOut:(NSString*)animationID finished:(BOOL)finished context:(void*)context {
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:0.2];
myView.alpha = 0.0;
[UIView commitAnimations];
}