I am using UIView's animation features to swim one image of a fish onto the screen and when there already exists a fish in the middle of the screen, swim it off and swim a new one on. The selection of the fish is sent from a separate tableview controller.
To accomplish this, I m keeping two instance variables of UIImageView and exchanging self.fishViewCurrent for self.fishViewNext once the animation is completed (see code below).
My problem is when the user presses on new table cells quickly, before the next fish has had a chance to arrive. My quick solution is to delay a new message that will add the fish and just do nothing at the moment. This works for 2 quick touches, but not more. I am looking for ideas if anyone knows how I can improve the code below, so that the animation is smooth and you can click on whatever while the final displayed image is the one last clicked. Here re the important methods:
Creating the first image view:
- (void) createFish {
self.fishViewCurrent = [[[UIImageView alloc] init] autorelease];
self.fishViewCurrent.contentMode = UIViewContentModeScaleAspectFit;
self.fishViewCurrent.bounds = CGRectMake(0.0f, 0.0f, kFishWidth, kFishHeight);
self.fishViewCurrent.center = CGPointMake(-kFishWidth, kFishYPos); // off the screen to the left
[self addSubview:self.fishViewCurrent];
}
Public method called when a tableview cell is selected:
- (void) addFish:(UIImage *)fish {
if (self.fishViewNext) {
[self performSelector:#selector(addFish:) withObject:(UIImage *)fish afterDelay:1.0];
return;
}
self.fishViewNext = [[[UIImageView alloc] initWithImage:fish] autorelease];
self.fishViewNext.contentMode = UIViewContentModeScaleAspectFit;
self.fishViewNext.bounds = CGRectMake(0.0f, 0.0f, kFishWidth, kFishHeight);
self.fishViewNext.center = CGPointMake(self.bounds.size.width + kFishWidth, kFishYPos); // off the screen right
[self addSubview:self.fishViewNext];
[UIView beginAnimations:#"swimFish" context:nil];
[UIView setAnimationDuration:2.0f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(fishSwamOffHandler:finished:context:)];
[UIView setAnimationRepeatAutoreverses:NO];
self.fishViewNext.center = CGPointMake(self.center.x, kFishYPos); // center
self.fishViewCurrent.center = CGPointMake(-kFishWidth, kFishYPos); // exit stage left
[UIView commitAnimations];
}
Handler to switch the 'next' fish (UIImageView) to the 'current':
- (void)fishSwamOffHandler:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[self.fishViewCurrent removeFromSuperview];
self.fishViewCurrent = self.fishViewNext;
self.fishViewNext = nil;
}
Simplest thing would be to create an ivar BOOL, say _fishIsSwimming, and a _queuedFishView then change the beginning of addFish: to look like this:
- (void) addFish:(UIImage *)fish
{
// queue the most recent fish, if one was passed in
if(fish)
self.queuedFishView = [[[UIImageView alloc] initWithImage:fish] autorelease];
// If something is swimming, leave now
if(_fishIsSwimming)
return;
// If we get here and there's no queued fish, leave now
if(!self.queuedFishView)
return;
// We have a queued fish and nothing is swimming, so do the queued fish next
// and clear the queued fish
_fishIsSwimming = YES;
self.nextFishView = self.queuedFishView;
self.queuedFishView = nil;
// Then carry on as normal
}
Then in your animation completion callback, you do this:
-(void) fishSwamOffHandler:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context
{
// We're done animating, so ready to do another swimming fish
_fishIsSwimming = NO;
[self.fishViewCurrent removeFromSuperview];
self.fishViewCurrent = self.fishViewNext;
self.fishViewNext = nil;
// now call to add another fish with nil, so that it will use
// the queued fish if there is one and otherwise just early out
[self addFish:nil];
}
So what will happen is that you'll always maintain the most recently passed in fish in the queuedFishView, and then as soon as the animation is over you can drop back into addFish, and if there's a fish waiting to go then you send it. If not you just wait for the next one sent by a button press
Related
My application has VOIP calling. In that I want to implement animation like iPhone's default Phone application does when User Clicks on Call button on dial Pad and animation that is done on End call button.
I have researched alot about this but haven't find anything yet. Any help will be appreciated on this topic.
Right now I have implemented scaling animation like below:
- (void)animateFadingOut{
self.view.transform = CGAffineTransformMakeScale(1.00, 1.00);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[self performSelector:#selector(push) withObject:nil afterDelay:0.35];
self.view.transform = CGAffineTransformMakeScale(0.00, 0.00);
//set transformation
[UIView commitAnimations];
}
- (void)push
{
self.view.transform = CGAffineTransformMakeScale(1.00, 1.00);
// push navigation controloller
CallViewController *objCallViewController = [[CallViewController alloc] initWithNibName:#"CallViewController_iPhone" bundle:nil];
[self setHidesBottomBarWhenPushed:YES];
[self.navigationController pushViewController:objCallViewController animated:NO];
[objCallViewController release];
[self setHidesBottomBarWhenPushed:NO];
[[AppDelegate shared] setTabHidden:TRUE];
}
But It is not giving me exact animation that default Phone application has
Here is what I might try if I was trying to create animations.
CGRect movementFrame = self.view.frame;
//Make position and size changes as needed to frame
movementFrame.origin.x = 0;
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
/*
Animation you want to commit go here
Apply movementFrame info to the frame
of the item we want to animate
*/
//If you wanted a fade
self.view.alpha = !self.view.alpha //Simply says I want the reverse.
self.view.frame = movementFrame;
//Example of what you could do.
self.view.transform =CGAffineTransformMakeScale(1.00, 1.00);
} completion:^(BOOL finished) {
//Things that could happen once the animation is finished
[self push];
}];
This has not been tested for your case. Therefore I am also not sure if it will help you, but hopefully it will. Good luck to you.
*Edit*
So I reviewed the animation on an iPhone and it appears to me to be a series of animations happening at once.
You have what I presume to be the UIActionSheet animating down.
The top section overlay sliding up its y-axis.
Then you have, which I haven't mastered yet, a split in the back view that animates its x-axis in opposite directions which cause the split.
Finally you have the new view scale up to take frame for the effect.
I can't say 100% this how they have done it, however if I was going to attempt to recreate this, I would likely start here.
Hello there so after just quickly coming up with an animation I got pretty close it could use some tweaks.
It took me three views to do a
topView, bottomView, and backView.
Also took into account the view that you would be loading in. viewToLoadIn
`-(void)animation{
CGRect topMovementFrame = self.topView.frame; //Set dummy frame to modify
CGRect bottomViewFrame = self.bottomview.frame;
topMovementFrame.origin.y = 0 - self.topView.frame.size.height; //modify frame's yAxis so that the frame sits above the screen.
bottomViewFrame.origin.y = self.view.frame.size.height; //modify frame's yAxis to the it will sit at the bottom of the screen.
[self.viewToLoadIn setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
//Animation
self.topView.frame = topMovementFrame; //Commit animations
self.bottomview.frame = bottomViewFrame;
self.backView.alpha = !self.backView.alpha;
self.backView.transform = CGAffineTransformMakeScale(100, 100);
self.viewToLoadIn.transform = CGAffineTransformMakeScale(2.0, 3.0);
*MAGIC*
} completion:^(BOOL finished) {
//Completion
//Clean up your views that you are done with here.
}];
}`
So then When they pressed the button I had setup this animation would happen.
Also at first I thought that the setup might contain a UIActionStyleSheet. which it still might but this is a pretty handy built in functionality. But the fact that you can interact with the screen lead me to think a custom view. It would be easier in my opinion.
I hope this helps you even if it just a little bit.
Take care ^^
I am using ARC in my app.
Now I faced a real problem. I need to animate two view controllers from left to right. So I just animate the the two views in button action like,
// thanks to stackoverflow for this code
test *control = [[test alloc] initWithNibName: #"test" bundle: nil];
control.view.frame = self.view.frame;
control.view.center = CGPointMake(self.view.center.x + CGRectGetWidth(self.view.frame), self.view.center.y);
[self.view.superview addSubview: control.view];
// Animate the push
[UIView beginAnimations: nil context: NULL];
[UIView setAnimationDelegate: self];
[UIView setAnimationDidStopSelector: #selector(pushAnimationDidStop:finished:context:)];
control.view.center = self.view.center;
self.view.center = CGPointMake(self.view.center.x - CGRectGetWidth(self.view.frame), self.view.center.y);
[UIView commitAnimations];
- (void) pushAnimationDidStop: (NSString *) animationID finished: (NSNumber *) finished context: (void *) context
{
[self.view removeFromSuperview];
}
and in the second view controller I did the same but change the animation direction.
My real problem is when I run this code, every time I create an object of the another class and it is not deallocating. By profiling this code, found that the every time the object is alive and it is not dying and the memory allocation get increasing.
You can run the allocation instrument with reference count recording enabled. Then evaluate where the offset differs from your expectations, based on that data.
I'm trying to implement an animation that happens when a user taps one of my tableview cells. Basically, the animation is just a little label with text like "+5" or "+1" that appears, then moves upwards whilst fading (basically like points appear in video games as the user scores them).
In the tableView:didSelectRowAtIndexPath: implementation of my controller, I'm doing the following (paraphrased for simplicity here):
CGRect toastFrame = /* figure out the frame from the cell frame here */;
UILabel *toast = [[UILabel alloc] initWithFrame:toastFrame];
toast.text = [NSString stringWithFormat:#"+%d", 5];
toast.backgroundColor = [UIColor clearColor];
toast.userInteractionEnabled = NO; // hoped this would work but it doesn't
[tableView addSubview:toast];
[UIView
animateWithDuration:1.0
animations:^
{
toast.alpha = 0.0;
toast.transform = CGAffineTransformMakeTranslation( 0.0, -44.0 );
}
completion:^ (BOOL finished)
{
[toast removeFromSuperview];
}];
[toast release];
The toast is appearing nicely and looks great. The problem is that until the animation completes, the tableview stops receiving touch events. This means that for one second after tapping a cell in the tableview, you can't tap any other cells in the tableview.
Is there a way to stop this from happening and allow the user to keep interacting with the tableview as if the animations weren't happening at all?
Thanks in advance for any help with this.
Another option may be to use animateWithDuration:delay:options:animations:completion: (untested, from the top of my head)
Take a look at the options parameter and the possible flags, defined by UIViewAnimationOptions. Included there is a flag called UIViewAnimationOptionAllowUserInteraction. This could be a solution, maybe you should try it out!
Have you tried doing it the other way? For example, after adding toast as a subview, you can do something like this:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration: 1.0];
toast.alpha = 0.0;
toast.frame.origin.y -= 44.0;
[toast performSelector:#selector(removeFromSuperview) withObject: nil afterDelay: 1.0];
[UIView commitAnimations];
and then release toast. You can try it this way and see if it works.
I am trying to get a busy view displayed during a search process however it never seems to display.
What I am trying to achieve
User clicks on search button once they have entered the text in a UISearchBar.
The text entered is delegated to searchBarSearchButtonClicked.
Show a "Busy view" which is a UIView containing a UIActivityIndicatorView to indicate the search is underway.
Perform a search which communicates with a website.
On search completion remove the "Busy View".
Whats wrong
The search process is working fine, using the debugger I can see it moving through the searchBarSearchButtonClicked function fine however the "Busy View" never appears. The search takes 2-3 seconds so in theory I should see my "Busy View" appear for those 2-3 seconds.
Code Attempt 1 - Add and remove the "Busy View" from the superview**
- (void)searchBarSearchButtonClicked:(UISearchBar *)activeSearchBar {
[activeSearchBar setShowsCancelButton:NO animated:YES];
[activeSearchBar resignFirstResponder];
[busyView activateSpinner]; //start the spinner spinning
[self.view addSubview:busyView]; //show the BusyView
Search *search = [[Search alloc]init];
results = [search doSearch:activeSearchBar.text];
[busyView deactivateSpinner]; //Stop the spinner spinning
[busyView removeFromSuperview]; //remove the BusyView
[search release]; search = nil;
}
Results Of Attempt 1 - It does not appear, however if I comment out the removeFromSuperview call the Busy View remains on screen.
Code Attempt 2 - using animations
- (void)searchBarSearchButtonClicked:(UISearchBar *)activeSearchBar {
[activeSearchBar setShowsCancelButton:NO animated:YES];
[activeSearchBar resignFirstResponder];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop)];
[busyView activateSpinner]; //start spinning the spinner
[self.view addSubview:busyView]; //show the busy view
[UIView commitAnimations];
Search *search = [[Search alloc]init];
results = [search doSearch:activeSearchBar.text];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop)];
[busyView deactivateSpinner]; //sets stops the spinner spinning
[busyView removeFromSuperview]; //remove the view
[UIView commitAnimations];
[search release]; search = nil;
}
Results of attempt 2 - Did not see the "Busy View" appear
Well, Here's my solution:
Create singleton class MyLoadingView. This class should contain show and hide static methods.
In MyLoadingView constructor you should manually create a view with semi-transparent black background and activityview inside of it. Place this view into [[UIApplication sharedApplication] keyWindow]. Hide it.
[MyLoadingView show] invocation just brings myLoadingView object to front in it's container: [[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:myLoadingViewSharedInstance];. Also don't forget to start activityview animation.
[MyLoadingView hide] stops activityview animation and hides sharedInstanceView.
Please notice, that you need to invoke [MyLoadingView show] in a separate thread.
UI changes in Cocoa only take effect the next time your code returns control to the run loop. As long as your search task runs synchronously on the main thread, it will block execution, including UI updates.
You should execute the time-consuming task asynchronously in the background. There are a number of options for this (NSOperation, performSelectorInBackground:..., Grand Central Dispatch, ...).
I am going to end up with an array of RSS feeds, and would like a label or some such to display them at the bottom of the view. I would like to animate through each feed in the array.
This is what i have so far to animate, which, works for the fade, but only animates the last item of the array.
feed = [[UILabel alloc] initWithFrame:CGRectMake(0,380,320,43)];
[self.view addSubview:feed];
feed.alpha=1;
NSArray *feeds = [NSArray arrayWithObjects:[NSString stringWithFormat:#"1234567"],[NSString stringWithFormat:#"qwerty"],[NSString stringWithFormat:#"asdfgh"],nil];
for (NSString* f in feeds){
feed.text=f;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2.0f];
feed.alpha=0;
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
}
Im sure its simple.
Thanks
First off you should really consider a better naming convention. Calling a UILabel a feed isn't very helpful for the future when you have to come back and look at your code. I would name it feedLabel. Then when you iterate through your list of feeds, you can just for (NSString *feed in feeds) and it will make more sense. And so will feedLabel.text = feed;.
Anyhow, the issue I see with your code is that your are repeatedly setting the alpha to zero in your loop, but you never set it back to one. In other words, you're not making a change to the alpha value. It stays the same in every iteration.
So maybe you could clarify what you're trying to do. If you want to fade the text between changes in the text, you'll need a different animation and methodology. Instead of a loop, chain your animations such that when your didStopSelector, you set the text and start the next one. Something like:
- (void)performAnimation;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2.0f];
feed.alpha=0;
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:)];
[UIView commitAnimations];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
feed.alpha = 1.0;
NSString *nextFeed = [self getNextFeed]; // Need to implement getNextFeed
if (nextFeed)
{
// Only continue if there is a next feed.
[feed setText:nextFeed];
[self performAnimation];
}
}
i tried your code, it fades out on first feed, but it doesn't enter the animationDidStop event. thats why it cant call performAnimation again. is there any set for animation (delegate or protocol etc..)