I have a little iOS5 app that shows images. I click on the image to show its information. I'd like the information to fade away after a few seconds. Is there a good method to do this?
I can always implement another button action but this would be neater..
Thanks!
Use either NSTimer or performSelector:withObject:afterDelay. Both methods require you to call a separate method that will actually do the fading out which should be fairly straightforward.
Example:
NSTimer
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(fadeOutLabels:) userInfo:nil repeats:NO];
performSelector:withObject:afterDelay:
/* starts the animation after 3 seconds */
[self performSelector:#selector(fadeOutLabels) withObject:nil afterDelay:3.0f];
And you will call the method fadeOutLabels (or whatever you want to call it)
-(void)fadeOutLabels
{
[UIView animateWithDuration:1.0
delay:0.0 /* do not add a delay because we will use performSelector. */
options:UIViewAnimationCurveEaseInOut
animations:^ {
myLabel1.alpha = 0.0;
myLabel2.alpha = 0.0;
}
completion:^(BOOL finished) {
[myLabel1 removeFromSuperview];
[myLabel2 removeFromSuperview];
}];
}
Or you can use the animation block to do all of the work:
-(void)fadeOutLabels
{
[UIView animateWithDuration:1.0
delay:3.0 /* starts the animation after 3 seconds */
options:UIViewAnimationCurveEaseInOut
animations:^ {
myLabel1.alpha = 0.0;
myLabel2.alpha = 0.0;
}
completion:^(BOOL finished) {
[myLabel1 removeFromSuperview];
[myLabel2 removeFromSuperview];
}];
}
Related
Hello I am able to fade an image out automatically. How would I manage to fade the image IN automatically.
Here is my code.
.h
#property (assign) IBOutlet UIImageView *cbizFadeImage;
.m
- (void)viewDidLoad {
//Fade Image Out
CGRect aboutNicheImageFrame = cbizFadeImage.frame;
aboutNicheImageFrame.origin.y = self.view.bounds.size.height;
// iOS4+
[UIView animateWithDuration:0.5
delay:2.0
options: UIViewAnimationCurveEaseOut
animations:^{
cbizFadeImage.alpha = 0;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
}
Ok I was able to fade an image in by changing the CurveEaseOut to CurveEaseIn. However now I would like to fade the image out. So the image would first ease in then It would then ease out. Would I use an if statement?
I was able to use the UIViewAnimationCurveEaseInOut.
Instead of UIViewAnimationCurveEaseInOut, use this for options:
options:UIViewAnimationOptionCurveEaseOut
I think what your really asking for is UIViewKeyframeAnimationOptionAutoreverse Used like this...
Image.alpha = 0;
[UIView animateKeyframesWithDuration:5.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0.9 relativeDuration:0.0 animations:^{
Image.alpha = 1;
}];
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.0 animations:^{
Image.alpha = 0;
}];
} completion:nil];
I'm trying to loop over multiple UIViews and perform animation on each, but I'd like to know when all of the animations have finished. What is the best way to call a function when the loop of animations has finished? Alternately, is there a way to wait until all have finished?
I tried using setAnimationDidStopSelector, but it doesn't fire. In this case, I'm clearing some "chips" off a game board by having them fade out then get removed (NumberedChipView is a subclass of UIView). Play cannot continue until the chips are off the board.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop)];
// clear chips
for (HexagonalTile *aTile in tilesToClear) {
NumberedChipView *chipView = [self chipViewForColumn:aTile.column andRow:aTile.row];
[UIView animateWithDuration:0.5 delay:0.3 options:UIViewAnimationCurveLinear animations:^{
chipView.alpha = 0.0;
}
completion:^(BOOL finished){
if (finished) {
[chipView removeFromSuperview];
[self.chipViews removeObject:chipView];
}
}];
}
[UIView commitAnimations];
I also tried CATransaction to no avail:
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[self animationFinished];
}];
// clear chips
for (HexagonalTile *aTile in tilesToClear) {
NumberedChipView *chipView = [self chipViewForColumn:aTile.column andRow:aTile.row];
[UIView animateWithDuration:0.5 delay:0.3 options:UIViewAnimationCurveLinear animations:^{
chipView.alpha = 0.0;
}
completion:^(BOOL finished){
if (finished) {
[chipView removeFromSuperview];
[self.chipViews removeObject:chipView];
//NSLog(#"clearing finished");
}
}];
}
[CATransaction commit];
Update:
I now can get the selector to fire, however it doesn't wait for the loop of animations to finish.
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:)];
Your for loop is firing off all the animations with the same delay and same duration, so they will all start and finish at the same time.
Your completion method is going to be called once for each of your animations.
You could rewrite your code slightly to set a counter to zero before entering the loop, and increment it after each step. When the index is less than the array count, pass a nil completion block. Once the index == the array count, you're on the last item, so pass in a completion block with the code that you want to invoke once the last animation completes.
Note that you can use this index approach to make each animation start at a different time, by increasing the delay amount based on the index value:
delay = .3 + (0.5 * index);
Here's what ended up working for me. I realized that the UIView animateWithDuration methods weren't being associated to the begin/commit section. Rather, the begin/commit sections mark a region of "implicit" animation operations, so I could just set the properties within the animation and it would be implicitly animated.
For example, within the animation below, I simply set the alpha property of the chipView to "0.0" and the chipView (a UIView) is implicitly animated to disappear.
[UIView beginAnimations:#"tilescleared" context:nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDelay:0.3];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
for (HexagonalTile *aTile in tilesToClear) {
NumberedChipView *chipView = [self chipViewForColumn:aTile.column andRow:aTile.row];
chipView.alpha = 0.0;
}
[UIView commitAnimations];
Then, in my animationDidStop method, I do the "cleanup" and remove and chips from the superview. This animationDidStop method only gets fired when all chipView animations from the loop have finished, which is the tricky thing I was trying to figure out.
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if ([animationID isEqualToString:#"tilescleared"]
&& [finished boolValue]) {
for (HexagonalTile *aTile in self.animationInfo.tilesToClear) {
NumberedChipView *chipView = [self chipViewForColumn:aTile.column andRow:aTile.row];
[chipView removeFromSuperview];
[self.chipViews removeObject:chipView];
}
}
}
I have a looped animation in a UIViewController which is called in the viewDidAppear function:
[UIView animateWithDuration:8.0 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^{
_introImage.center = CGPointMake(INTRO_IMAGE_X_END, INTRO_IMAGE_Y);
}
completion:^(BOOL finished){
_introImage.center = CGPointMake(INTRO_IMAGE_X_START, INTRO_IMAGE_Y);
}];
When I put the app into the background and bring it back to the foreground the animation stops. Why is this and what could I do to restart the animation?
You could put it in it's own method and call that method from viewDidAppear, and also in applicationDidBecomeActive, which is in your AppDelegate.m
-(void)animate {
[UIView animateWithDuration:8.0 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^{
_introImage.center = CGPointMake(INTRO_IMAGE_X_END, INTRO_IMAGE_Y);
}
completion:^(BOOL finished){
_introImage.center = CGPointMake(INTRO_IMAGE_X_START, INTRO_IMAGE_Y);
}];
}
In order to fire the method from "viewDidAppear" call the method with a single line , like this:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self animate];
}
And to fire the animation from the app delegate when the app becomes active:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self.viewcontroller animate];
}
This is all assuming you might want to use viewDidAppear for other things as well. Separating the animation into it's own method lets you call it exclusively, instead of running everything inside of viewDidAppear.
I'm having a little problem. I have an UILabel which have an UILongPressGestureRecognicer. When the UILongPressGestureRecognizer is called my app is supposed to switch to a new view using a flip animation.
This is the code I have used for the GestureRecognizer:
UILongPressGestureRecognizer *labelLongPressRecognizer;
labelLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LoadLabelSettings:)];
labelLongPressRecognizer.numberOfTouchesRequired = 1;
labelLongPressRecognizer.minimumPressDuration = 2.0;
[NewLabel addGestureRecognizer:labelLongPressRecognizer];
and this is the code for the view switching animation:
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[self.view addSubview:LabelSettingsViewController.view];
[UIView commitAnimations];
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight || self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
LabelSettingsViewController.view.frame = CGRectMake(0, 0, 480, 300);
}
My problem is that when I hold down on my UILabel the switch animation begins, but when I release it repeats the animation again. So basically the animation occur twice and I only want it to take place once.
Any ideas?
Thanks in advance :)
Are you checking the sender state, e.g.,
- (void)LoadLabelSettings:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded) // or whatever
// then do the flipping stuff
}
Check out the "Overview" of the "UILongPressGestureRecognizer Class Reference", which talks about the long press being continuous and I presume numerous events could be triggered:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UILongPressGestureRecognizer_Class/Reference/Reference.html
is it possible to create a design something like this?
any help regarding this would be very helpful for me.the functionality i want is to rotate the wheel left-to-right or right to left for selecting time...yellow is selection color and red is shows the remaining time when countdown is running
Use a Gallery view object. Just fill it with the images you want to scroll horizontally.
This would be a good approximation to what you're looking to achieve.
See my video
http://www.youtube.com/watch?v=4acFshAlGJ8
I have use
https://lh3.googleusercontent.com/-WZEDMSmfrK0/TeeD93t8qYI/AAAAAAAAAKw/X9D6jRkLfLk/s800/MUKScale.png
image as Scale in the scrollView this is an incomplete example just to show how can it is achievable.
here is some helpful piece of code,
in this approx everything is static but for the real work one should to work more,
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:scrollView];
UIImageView *backImgg = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,886,15)];
[backImgg setImage: [UIImage imageNamed:#"MUKScale.png"]];//Image in the link above
[scrollView addSubview:backImgg];
[backImgg release];
[scrollView setContentSize:CGSizeMake(886,15)];
return;
}
NSTimer *timer ;
float timeSet =0 ;
-(IBAction) btnPressed
{
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(showTimeInLbl) userInfo:nil repeats:YES];
}
-(void)showTimeInLbl
{
CGRect visibleRect;
visibleRect.origin = scrollView.contentOffset;
visibleRect.size = scrollView.contentSize;
NSLog( #"Visible rect: %#", NSStringFromCGRect(visibleRect) );
float time = visibleRect.origin.x / 8;
timeSet = time;
lblTime.text = [NSString stringWithFormat:#"%f",time];
[UIView animateWithDuration: .1
animations: ^{
[scrollView setContentOffset:CGPointMake(visibleRect.origin.x - 8,0) animated:NO];
}completion: ^(BOOL finished){
}
];
timeSet-=0.1;
lblTime.text = [NSString stringWithFormat:#"%f",timeSet];
if(timeSet<=0)
{
[timer invalidate];
lblTime.text = #"0";
[UIView animateWithDuration: .1
animations: ^{
[scrollView setContentOffset:CGPointMake(0,0) animated:NO];
}completion: ^(BOOL finished){
}
];
}
}
You can use a Never Ended scrollView (For example this you have in it like off paging).
Now by this you will achieve the scale and by using page no or coordinates of the scrollview you can find the scale reading to show on above.
On start count down create animation of UIView contentOffset CGPointMake(10, 100).
for example:
[UIView animateWithDuration: 1.5 /* Give Duration which was also on top */
animations: ^{
[scrollView setContentOffset:CGPointMake(0,0) animated:NO];
}completion: ^(BOOL finished){ }];