Now i am working in iPhone application, Using UIImageView to create a animation and it's working fine, then i tried to call one method for when the animation once completed then the button to show on the screen, but that button showing first then the animation starts, i want once animation completed then the button showing on the screen, how to fix this issue? please help me
Thanks in Advance
I tried this for your reference:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray * imageArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"cardopen.png"],
[UIImage imageNamed:#"middlecard.png"],
[UIImage imageNamed:#"thirdcard.png"],
[UIImage imageNamed:#"a.png"],
[UIImage imageNamed:#"cardopen1.png"],
[UIImage imageNamed:#"middlecard2.png"],
[UIImage imageNamed:#"thirdcard1.png"],
[UIImage imageNamed:#"2.png"],
nil];
UIImageView * cardsImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 400, 300)];
cardsImage.animationImages = imageArray;
cardsImage.animationDuration = 8.0;
cardsImage.animationRepeatCount=1; // one time looping only
[self.view addSubview:cardsImage];
[cardsImage startAnimating];
[self method1]; // To Call a method1 to show UIButton (Once the animation is completed, then to call a method1)
}
-(void)method1
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(10, 40, 50, 50);
[self.view addSubview:btn];
}
(Why doesn't anybody understand the concept of asynchronous methods? Is this that hard?)
[cardsImage startAnimating];
returns immediately, not after the animation finished. You have to set an explicit timeout:
NSTimer *tm = [NSTimer scheduledTimerWithTimeInteral:imageView.animationDuration * imageView.animationCount // assuming animationCount is not 0
target:self
selector:#selector(method1)
repeats:NO
userInfo:nil];
Use
[self performSelector:#selector(method1) withObject:nil afterDelay:8.0];
instead of [self method1];
This is because you are putting all of your code in ViewDidLoad. The whole method will be processed . Plus you have your animation duration set to 0.8 which is a slow animation.So the button would appear normally and the animation would continue along with it and then even more. So what you actually have to do is use an NSTimer or use the answers provided to you here :)
Related
Is there a way to animate a UIButton background image view with a set of images?
I've managed to do so with the imageView property, but couldn't find an equivalent background property.
10x
Here's what I did (inside a UIButton subclass):
Set the button images, like regular:
[self setBackgroundImage:[[UIImage imageNamed:#"button"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)] forState:UIControlStateNormal];
[self setBackgroundImage:[[UIImage imageNamed:#"button_pressed"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)] forState:UIControlStateHighlighted];
Override highlighted:
- (void)setHighlighted:(BOOL)highlighted {
[UIView transitionWithView:self duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent animations:^{
[super setHighlighted:highlighted];
} completion:nil];
}
To change a buttons backround image, or text for that matter - you need to change it for the particular UIControlState... i.e Highlighted, Selected, Disabled
Use
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
I'm not sure if you can animate them, I've never tried. If you can't animate them, then you could put a UIImageView behind a transparent button and animate the images in that instead - just a thought.
You can animate an UIButton the way you animate an UIImageView like this...
-(void) addAnimationToButton:(UIButton*) button{
UIImageView* animationView = [button imageView];
NSArray* animations=[NSArray arrayWithObjects:
[UIImage imageNamed:#"sprite1.png"],
[UIImage imageNamed:#"sprite2.png"],
[UIImage imageNamed:#"sprite2.png"],
//and so on if you need more
nil];
CGFloat animationDuration = 2.0;
[animationView setAnimationImages:animations];
[animationView setAnimationDuration:animationDuration];
[animationView setAnimationRepeatCount:0]; //0 is infinite
}
And you use it like this:
[self addAnimationToButton:yourButton];
[[yourButton imageView] startAnimating]; //or stopAnimating
Yes it is possible. Use NSTimer ,
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:5.00 target:self selector:#selector(changeImage) userInfo:nil repeats:YES];
then, method is as follows,
- (void)changeImage
{
static int counter = 0;
if([bannerArray count] == counter)
{
counter = 0;
}
[button setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bannerArray[counter]]]] forState:UIControlStateNormal];
counter++;
}
It worked for me. But adding animation like flip etc need to figure out. Hope this also one way to help your need.
-(IBAction)startClick:(id)sender{
stick.highlighted = YES;
}
-(void)viewDidLoad{
[super viewDidLoad];
stick = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"ball1.png"]];
stick.userInteractionEnabled= YES;
stick.highlightedImage = [UIImage imagedNamed:#"ball2.png"];
[self.view addSubview:stick];
}
Button does not work after i type viewDidLoad method. Pls help. Thx. stick is linked to a uiimageview.
Hello as you said stick is linked to UIImageView , then it is a bit ambiguous. It's not clear whether you have declared an IBOutlet for this and connected to an UIImageview in xib or you have declared it as a variable in header file . There are two methods both of which works fine
Method 1:
Declare an IBoutlet in .h file and connect it to an Imageview in xib . Calling methods as follows
-(void)viewDidLoad{
[super viewDidLoad];
stick.image = [UIImage imageNamed:#"firstImage.png"];
stick.highlightedImage = [UIImage imageNamed:#"secondImage.png"];
}
-(IBAction)startClick:(id)sender{
stick.highlighted = YES;
}
would serve your purpose. The method is called from a button in xib on touch up inside event. In case you want to call this method from bar button all you need is to hook up selector of bar button to startclick method.
//2nd Method
If you have declared your Imageview in header as UIImageView* stick;
in viewDidLoad method you need to allocate it as
-(void)viewDidLoad{
[super viewDidLoad];
if (!stick) {
stick = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
[stick setImage:[UIImage imageNamed:#"firstImage.png"]];
[stick setHighlightedImage:[UIImage imageNamed:#"secondImage.png"]];
}
UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithTitle:#"Click" style:UIBarButtonItemStylePlain
target:self action:#selector(startClick:)];
self.navigationItem.rightBarButtonItem = barButton;
[barButton release];
}
-(void)startClick:(id)sender{
stick.highlighted = YES;
}
Hope it helps !!
might be due to :
-(void)viewDidLoad
{
[super viewDidLoad];
stick = [UIImageView alloc]initWithImage:[UIImage imageNamed:#"ball1.png"];
stick.userInteractionEnabled= YES;
// stick.highlightedImage = [UIImage imaged:#"ball2.png"];
stick.highlightedImage = [UIImage imageNamed:#"ball2.png"];
[self.view addSubview:stick];
}
How can I start and stop image animation using button control?
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"p1.jpg"],[UIImage imageNamed:#"p2.jpg"], [UIImage imageNamed:#"p3.jpg"],[UIImage imageNamed:#"p4.jpg"],[UIImage imageNamed:#"p5.jpg"],[UIImage imageNamed:#"p6.jpg"], nil];
imageView.animationDuration = 1.00; //1 second
imageView.animationRepeatCount = 0; //infinite
[imageView startAnimating]; //start the animation
Add two buttons using Interface Builder, create two IBAction methods in your controller, connect Touch Up Inside events with the IBActions.
Use [imageView startAnimating] to start, [imageView stopAnimating] to stop.
I am very to new phone i want to display four images in one line like tabbar and also if the user clicks any of the image i want to show another screen in iPhone.
can any one tell me how can i do that and also if possible please tell me in which files i have to write the code to achieve this requirement.
Try This
In ViewDidLoad
CGRect myImageRect = CGRectMake(10.0f, 5.0f, 80.0f, 95.0f);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
imageView.opaque = YES; // explicitly opaque for performance
[self.view addSubview:imageView];
UIButton *newbutton1 = [[UIButton alloc] init];
[newbutton1 setFrame:myImageRect];
[newbutton1 addTarget:self action:#selector(myMethod:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:newbutton1];
//Method on Button Click
-(void)myMethod:(id)sender {
//Button is pressed
}
Make some xib file and build your UI.
You need to set other screens as IBOutlets and after click on button perform action to switch view.
You can set image background for UIButton.
Try, it isn't so big problem.
UIButton with images tutorial
Is a great tutorial for creating clickable images.
arrange 4 images manually in ur screen
eg
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
myImageView.image = [UIImage imageNamedWith:#"ur image.png"];
UIGestureRecognizer *recognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(showtheUI:)];
[myImageView addGestureRecognizer:recognizer1];
recognizer1.delegate = self;
[recognizer1 release];
[self.view addSubView:myImageView ];
}
//this method will trigger when u tapped the 1st image
-(void)showtheUI(UIImageViee *)sender{
}
create 2nd imageView like
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 10, 100, 100)]; etc
or use a for loop to create 4 image Views. set frame according to the i value
you need to create the custom table or more things to use for the creating a grid type of image
for that you need to use following things to use
TableView
Custom UITableViewCell
Array of images
HJCache for asynchronous image loading.
You can add buttons on your images and handle click events of buttons to Navigate to other screens
This is the strangest error i have come across.
Am trying to add a countdown animation onto a Class that extends UIView. The code is shown below:
NSArray *myImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed:#"3.png"], [UIImage imageNamed:#"2.png"], [UIImage imageNamed:#"1.png"], [UIImage imageNamed:#"Go.png"], nil];
UIImageView *myAnimatedView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 80, 80)];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 3;
myAnimatedView.animationRepeatCount = 0;
[self addSubview:myAnimatedView];
[myAnimatedView startAnimating];
It is just not appearing on the screen. Any Ideas?
Have you made sure those images are added to your project?
Can you add them as a flat image (non-animated) -- as in:
UIImageView *myAnimatedView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"3.png"]];
[self addSubview:myAnimatedView];
(just to make sure that works first)?
If, instead of extending UIView, you extended UIImageView, would that make a difference?
Try switching the last 2 lines. From:
[self addSubview:myAnimatedView];
[myAnimatedView startAnimating];
To:
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];