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.
Related
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 :)
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
For the app i am currently working on i am trying to take a series of images and link them to a UISlider. Interacting with the slider will create an animated effect, cycling through the frames. I have been able to create a standalone animation from the series of images, however i am not too certain about how to add the UISlider and make the appropriate links to achieve the desired effect.
Here is what i have so far...
- (void) viewDidLoad{
//---Animate Images Array----
/*
NSArray *images = [NSArray arrayWithObjects:
[UIImage imageNamed:#"frame1.png"],
[UIImage imageNamed:#"frame2.png"],
[UIImage imageNamed:#"frame3.png"],
[UIImage imageNamed:#"frame4.png"],
[UIImage imageNamed:#"frame5.png"],
[UIImage imageNamed:#"frame6.png"],
[UIImage imageNamed:#"frame7.png"],
[UIImage imageNamed:#"frame8.png"],
[UIImage imageNamed:#"frame9.png"],
[UIImage imageNamed:#"frame10.png"],
nil];
CGRect frame = CGRectMake(0,44,703,704);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.animationImages = images;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.animationDuration = 4;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:imageView cache:YES];
[self.view addSubview:imageView];
[imageView release];
[super viewDidLoad];
Any clues?
I don't think you can get a UISlider object to work with animationImages property of an UIImageView. However you can maintain an array of UIImages and then set the UIImageView object's image property. For this you will have to set an action for the UIControlEventValueChanged event. Set the max value of the slider to the number of images i.e. [imageArray count] and then based on the current value of the slider, update the image.
- (void)valueChanged:(UISlider *)slider {
NSInteger currentImageIndex = lroundf(slider.value);
imageView.image = [imageArray objectAtIndex:currentImageIndex];
[slider setValue:roundf(slider.value) animated:YES];
}
In my Iphone App, I used the following code for the animation of those 3 images in viewDidLoad() method and I use left transform to load this ViewController. But these imageView is not displayed on the view.
NSArray *newArray = [[NSArray alloc]initWithObjects:[UIImage imageNamed:#"search1.png"],
[UIImage imageNamed:#"search2.png"],
[UIImage imageNamed:#"seach3.png"],
nil];
UIImageView *imageView = [UIImageView alloc];
[imageView initWithFrame:CGRectMake(240, 60, 60, 30)];
imageView.animationImages = newArray;
imageView.animationDuration = 0.25;
imageView.animationRepeatCount = 3;
[self.view addSubview:imageView];
Animation won't start until you tell it to start. You will need to add a command to startAnimating. I also cleaned up your alloc/init and I added a command to make the image view still keep showing when it is not animating. Setting an animation will not make the image appear. The image property is for the still image and the animationImages property is for the animating images of a UIImageView.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 60, 60, 30)];
imageView.image = [UIImage imageNamed:#"search3.png"]; //This will be the image that is visible when it is NOT animating.
imageView.animationImages = newArray;
imageView.animationDuration = 0.25;
imageView.animationRepeatCount = 3;
[imageView startAnimating]; //This will make the animation start
[self.view addSubview:imageView];
Finally, your third image is missing an r. I think you want #"search.png" and not #"seach.png"
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];