Hi guys I want to create an intro image (fade in and fade out ) for my application.
Here is my code but, when I run the app on the device I have a problem:
My main nib page has been displayed
My image fades in and then fades out and then again fades in and fades out 3 times
Here is my code and my attached file. Please check it out:
.h
#interface myProject : UIViewController {
float alphaValue, imageAlphaValue;
}
UIImageView *IntroImage;
NSTimer *AlphaTimer;
#end
.m
-(void)viewDidLoad
{
imageAlphaValue = 0.0;
alphaValue = 0.035;
IntroImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:IntroImage];
[IntroImage setImage:[UIImage imageNamed:#"yourimagenamehere.png"]]; //use your image here
[IntroImage setBackgroundColor:[UIColor blueColor]];
[IntroImage setAlpha:imageAlphaValue];
[IntroImage release];
AlphaTimer = [NSTimer scheduledTimerWithTimeInterval:(0.05) target:self selector:#selector(setAlpha) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:(2.0) target:self selector:#selector(changeAlpha) userInfo:nil repeats:NO];
}
-(void)setAlpha
{
imageAlphaValue = imageAlphaValue + alphaValue;
[IntroImage setAlpha:imageAlphaValue];
}
-(void)changeAlpha
{
alphaValue = -0.035;
}
my sample code
wow.... why you cannot just use something like that to change your image's alpha?
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationDelegate:self];
[introImage setAlpha:0.0f];
[UIView commitAnimations];
Seems to be not surprising that the main nib is shown, because it is the viewDidLoad method!
I'd also prefer to make use of the animation stuff like in morion's answer. Even though: The repeated fading is probably caused by viewDidLoad called more than once. Check it out with debugging or logging.
Related
I am animating an UIImageview in horizontal position for this purpose i have used the below code i have used the NSTimer
timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
-(void)onTimer
{
[UIView animateWithDuration:10.0f animations:^{
//Moving_Cloud is an image view
Moving_Cloud.frame = CGRectMake(200.0f, 150.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
}];
}
now the problem i am facing is i need to get the coordinates of the "Moving_Cloud" after the animate duration
please help me
Thanks in advance.
Abhijit Chaudhari from the comment in my previous post I understand that what you want is not the position "after the animate duration" but instead the position during the animation.
If I still didn't get it right please clarify your question. (Or buy me an new brain)
-(void) animateMe(){
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
[UIView animateWithDuration:10.0f animations:^{
//Moving_Cloud is an image view
Moving_Cloud.frame = CGRectMake(200.0f, 150.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
}];
}
-(void)onTimer:(id)sender{
NSLog(#"Currently in x=%f", [[ddddd.layer presentationLayer] frame].origin.x);
NSLog(#"Currently in y=%f", [[ddddd.layer presentationLayer] frame].origin.y);
}
[UIView animateWithDuration:10.0f animations:^{
Moving_Cloud.frame = CGRectMake(200.0f, 150.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
} completion:^(BOOL finished){
CGPoint newPost = Moving_Cloud.frame.origin;
CGFloat xPos = newPost.x;
CGFloat yPos = newPost.y;
// do stuff ?
}];
At the end of your animation the block "completion:" will be trigger.
Normally your image should be in x = 200, y = 150.
Be aware that those coordinates are relative to it superview (the view wrapping Moving_Cloud view).
Note:
By convention I recommend changing "Moving_Cloud" to "movingCloud".
Instance class start in lower cap in objective-C.
Also don't use _ but a capital letter instead.
I am drawing a NSString using drawRect and is it possible for me to animate this NSString once it has been drawn? I want it to fade in/out after 3.0 seconds. How can I do so?
Make a UILabel, then use animation to perform your desires
- (void)faceInLabel{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
label.text = #"My String";
[myView addSubview:label];
label.alpha = 0;
[UIView animateWithDuration:0.5 animations:^{
label.alpha = 1;
}
completion:^(BOOL finished)
{
[NSTimer timerWithTimeInterval:3 target:self selector:#selector(fadeOutLabel) userInfo:nil repeats:NO];
}];
}
- (void)fadeOutLabel
{
[UIView animateWithDuration:0.5 animations:^{
label.alpha = 0;
}];
}
You cannot directly fade the drawn string itself. You can fade layers and you can fade views. Draw the string onto one of these and fade the layer or view. This is generally most easily done with a UILabel or CATextLayer, but you can draw onto your own layer and fade that if you prefer.
Everybody..
How to set animation on button click ?
I have two buttons, both have different frame..
First CGRectMake (10 , 10 , 100 , 80 ); It has Black Image,
Second CGRectMake (200, 10 , 210 , 80 ); It has White Image,
When I click on First button of those, It should be animate to move from one frame to another frame..
Only seen like first's button Black Image move to second button location.. Like Image changes of Buttons..
After clicked button frame does not change... Should be remain as First and Second..
I have tried but not got the exact
buttonFirst.frame = buttonSecond.frame;
buttonFirst.center = buttonSecond.center;
buttonFirst.transform = CGAffineTransformMakeTranslation( buttonSecond.frame.origin.x - buttonFirst.frame.origin.x , buttonSecond.frame.origin.y - buttonFirst.frame.origin.y);
How to animate this ?
Editting :
I need to move one checkers from one CGRect to Another CGRect.. So, It want to display like moving checkers, But i dont want to change CGRect, Only button images looks like that...
Please tell me..
Thanks...
very simple logic
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.20f];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationBeginsFromCurrentState:YES];
[firstButton setFrame:CGRectMake(5, 50, 30, 30)];//moves to second button place
[secondButton setFrame:CGRectMake(5, 5, 30, 30)];//moves to first button place
[UIView commitAnimations];
use the logic accordingly to your use..
well..
you can try this link
[UIView beginAnimation/endAnimation] bock
CGRect oldRectFirst = buttonFirst.frame;
CGRect oldRectSecond = buttonSecond.frame;
[UIView animateWithDuration:0.2f animations:^{
buttonFirst.frame = oldRectSecond;
buttonSecond.frame = oldRectFirst;
}];
Here is example from working program:
[self.buttonFirst setUserInteractionEnabled:NO];
[UIView transitionWithView:self.buttonFirst
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionCurveLinear
animations:^{
buttonFirst.frame = buttonSecond.frame;
buttonFirst.center = buttonSecond.center;
}
completion:^(BOOL finished){
[self.buttonFirst setUserInteractionEnabled:YES];
}];
Finally i got the answer...
I have set two button in two separate view. after set one timer, and move first button image to another button's position..
Here is a sample code for that..
Step 1 : Set view with Timer
UIView *oldView = [[UIView alloc] init];
oldView = [viewArray objectAtIndex:selectedIndex];
UIView *newView = [[UIView alloc] init];
newView = [viewArray objectAtIndex:[sender tag]];
oldRect = [oldView frame];
newRect = [newView frame];
movingTimer = [[NSTimer alloc] init];
movingTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(movingCheckers:) userInfo:nil repeats:YES];
imageViewMove = [[UIImageView alloc] init];
[imageViewMove setFrame:oldRect];
[imageViewMove setImage:[UIImage imageNamed:blackManName]];
[self.view bringSubviewToFront:imageViewMove];
[self.view addSubview:imageViewMove];
Step 2 : Moving Image to second position
CGPoint aPoint = CGPointMake(newRect.origin.x - oldRect.origin.x, oldRect.origin.y - newRect.origin.y);
imageViewMove.frame = CGRectMake(imageViewMove.frame.origin.x + (aPoint.x / 6) , imageViewMove.frame.origin.y - (aPoint.y / 6), imageViewMove.frame.size.width, imageViewMove.frame.size.height);
if (imageViewMove.frame.origin.x >= newRect.origin.x)
{
[selectButton setBackgroundImage:nil forState:UIControlStateNormal];
[moveButton setBackgroundImage:imageViewMove.image forState:UIControlStateNormal];
[imageViewMove removeFromSuperview];
[movingTimer invalidate];
}
Now its work.....
I'm trying to get this animation to delay for 60 seconds and take 125 seconds to complete it's animation cycle. then repeat infinitely. the problem is that the delay only lasts 20 seconds. Is there a limit on the delay you can specify? or, perhaps a better way to do what I'm attempting?
here's my code:
- (void)firstAnimation {
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"f1.png"],
[UIImage imageNamed:#"f2.png"],
[UIImage imageNamed:#"f3.png"],
[UIImage imageNamed:#"f4.png"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:CGRectMake(0, 0, 320, 400)];
myAnimatedView.animationImages = myImages;
[UIView setAnimationDelay:60.0];
myAnimatedView.animationDuration = 125.0;
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever
[myAnimatedView startAnimating];
[self.view addSubview:myAnimatedView];
[self.view sendSubviewToBack:myAnimatedView];
[myAnimatedView release];
}
thanks for any help.
You're using the setAnimationDelay method in the wrong way.
setAnimationDelay is intended to be used when animating changes to animatable properties on views within a UIViewAnimations block like so:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:60];
//change an animatable property, such as a frame or alpha property
[UIView commitAnimations];
That code will delay the animation of a property change by 60 seconds.
If you want to delay a UIImageView from animating its images you'll need to use an NSTimer.
[NSTimer scheduledTimerWithTimeInterval:60
target:self selector:#selector(startAnimations:)
userInfo:nil
repeats:NO];
Then define the startAnimations: selector, like so:
- (void)startAnimations:(NSTimer *)timer
{
[myAnimatedView startAnimating];
}
That way, after 60 seconds, the timer will fire the method startAnimations: which will start your image view animating.
How to create that black/gray modal popup kind of view that many apps use, when some long pending operation is in progress?
Like when using location based services, loading a webpage, the screen goes dim and there is a modal view showing a spinning icon "Please wait..."
Example in the following screenshot:
This is actually the undocumented (in 2.2.1 anyway) UIProgressHUD. Create one like this:
In your .h:
#interface UIProgressHUD : NSObject
- (UIProgressHUD *) initWithWindow: (UIView*)aWindow;
- (void) show: (BOOL)aShow;
- (void) setText: (NSString*)aText;
#end
In your .m:
- (void) killHUD: (id)aHUD
{
[aHUD show:NO];
[aHUD release];
}
- (void) presentSheet
{
id HUD = [[UIProgressHUD alloc] initWithWindow:[contentView superview]];
[HUD setText:#"Doing something slow. Please wait."];
[HUD show:YES];
[self performSelector:#selector(killHUD:) withObject:HUD afterDelay:5.0];
}
If you want to avoid undocumented api you can also take a look at MBProgressHUD. It's similar to UIProgressHUD and even has some additional features.
I think the simplest (a few lines of code), fully documented and most beautiful way is to use the UIAlertView with a UIActivityIndicatorView:
http://iosdevelopertips.com/user-interface/uialertview-without-buttons-please-wait-dialog.html
(source: iosdevelopertips.com)
If you add a UIView as a subview of the main window it will cover the entire UI. Make it partially transparent and partially translucent and it will look like a popup.
This example shows how to fade the Default.png splash screen, starting with that it's pretty straightforward to add a couple methods to your application delegate (that has a pointer to the main window) to present and dismiss the progress view.
Take a look at the Wordpress iPhone app (http://iphone.wordpress.org/) for an example of how to do this without using any undocumented API's.
I use LoadingHUDView for this purpose, and it works always.
get LoadingHUDView.m and LoadingHUDView.h and do the following in your base class (or whatever)
#pragma mark ActivityIndicator Methods
-(void) showModalActivityIndicator:(NSString *)message
{
loadingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]retain];// origional
//loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; //testing
loadingView.backgroundColor = [UIColor grayColor]; //[UIColor colorWithRed:1 green:1 blue:1 alpha:0.3];
LoadingHUDView *loadHud = [[LoadingHUDView alloc] initWithTitle:message];
loadHud.center = CGPointMake(160, 290);
[loadingView addSubview:loadHud];
[loadHud startAnimating];
[loadHud release];
[loadingView setAlpha:0.0];
[self.tableView addSubview:loadingView];
[UIView beginAnimations:#"fadeOutSync" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[loadingView setAlpha:0.5];
[UIView commitAnimations];
}
-(void) hideModalActivityIndicator {
if (loadingView) {
[UIView beginAnimations:#"fadeOutSync" context:NULL];
[UIView setAnimationDidStopSelector:#selector (removeTranparentView) ];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[loadingView setAlpha:0];
[UIView commitAnimations];
}
}
-(void)removeTranparentView
{
[loadingView removeFromSuperview];
[loadingView release];
loadingView = nil;
}
HOPE THIS HELPS.
thank you
In XIB file, place UIView and UIActivityIndicatorView.
Set ActivityIndicatorView Property SetAnimated to Yes.
#property (retain, nonatomic) IBOutlet UIView* m_LoadingView;
While Starting long operations, Add the LoadingView to the view
m_LoadingView.layer.cornerRadius = 10;
m_LoadingView.center = self.view.center;
[self.view addSubview:m_LoadingView];
After completing the process, Remove LoadingView from super view.
[m_LoadingView removeFromSuperView];