After build code, it doesn't have any animation on my cellphone
but the code looks good, i don't know how to figure it out ,
and my image is 829 * 445, does it cause this problem ?
Could someone help me to solve this problem i will really appreciate it, thanks
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.jpg"],
[UIImage imageNamed:#"2.jpg"],
[UIImage imageNamed:#"3.jpg"],
[UIImage imageNamed:#"4.jpg"],nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,150,150)];
imageView.animationImages = animationImages ;
imageView.animationRepeatCount = 2;
imageView.animationDuration= 4.0;
[imageView startAnimating];
[NSTimer scheduledTimerWithTimeInterval:4.0 target:self
selector:#selector(animationDone:)
userInfo:nil repeats:NO];
}
-(void)animationDone:(NSTimer*)inTimer
{
[inTimer invalidate];
inTimer = nil;
NSLog(#"animationDone ");
}
#end
You don't added imageView to your ViewController view.
Try [self.view addSubview:imageView]; in -viewDidLoad
Please use animation block code instead of the older method. The animation will tell you when its done. You don't need to set a timer.
NSArray *animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.jpg"],
[UIImage imageNamed:#"2.jpg"],
[UIImage imageNamed:#"3.jpg"],
[UIImage imageNamed:#"4.jpg"],nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,150,150)];
[UIView animateWithDuration:1.0f animations:^{
for (int loop = 0; loop < 4; loop++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFornmat:#"%d.jpg", (loop + 1)]];
[imageView setImage:image];
}
} completion:^(BOOL finished) {
NSLog(#"animationDone");
}];
It looks like you're not adding the animating UIImageView to your view hierarchy. Right before your [imageView startAnimating]; line add the below line of code:
[self.view addSubview:imageView];
Related
As title, I am beginner so i don't have idea to solve it.
In my code, it just have zoom animation when i turn on the emulator first time,
I want to have the zoom animation when each photo change,
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.jpg"],
[UIImage imageNamed:#"2.jpg"],
[UIImage imageNamed:#"3.jpg"],
[UIImage imageNamed:#"4.jpg"],nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,300,300)];
imageView.animationImages = animationImages ;
imageView.animationRepeatCount = 0;
imageView.animationDuration= 12.0;
[imageView startAnimating];
[self.view addSubview:imageView];
imageView.frame = CGRectMake(150, 150, 20, 20);
CGPoint center = imageView.center;
[UIView animateWithDuration: 1.0f animations:^{
imageView.frame = CGRectMake(10, 10, 300, 300);
imageView.center = center;
}];
}
Thank you in advance for any assistance that can be provided here.
It's not the cleanest code to use animationImages if you want a zoom animation on each image transition. I would create my own custom UIView class to handle the transitions myself so the animations can be tightly synchronized with each transition. But, for the sake of answering your question with a quick unreliable hack, I will use a timer to run an animation at approximately the same frequency as the image transitions. Timers become less accurate over time due, so eventually the zoom effect no longer lines up with the image transitions.
#interface NameOfYourViewController ()
#property(nonatomic, strong) UIImageView* imageView;
#property(nonatomic, strong) NSTimer* imageTransitionTimer;
#property(nonatomic, assign) CGFloat imageScale;
- (void)animateImage;
#end
#implementation NameOfYourViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.jpg"],
[UIImage imageNamed:#"2.jpg"],
[UIImage imageNamed:#"3.jpg"],
[UIImage imageNamed:#"4.jpg"],nil];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,300,300)];
selfimageView.animationImages = animationImages ;
self.imageView.animationRepeatCount = 0;
self.imageView.animationDuration= 12.0;
[self.imageView startAnimating];
[self.view addSubview:self.imageView];
self.imageScale = 1.0f;
self.imageTransitionTimer = [NSTimer scheduledTimerWithTimeInterval:imageView.animationDuration target:self selector:#selector(animateImage) userInfo:nil repeats:YES];
[self.imageTransitionTimer fire];
}
- (void)animateImage
{
[UIView
animateWithDuration:1.0
delay:0.0
options:0
animations:^void () {
self.imageView.transform = CGAffineTransformMakeScale(
self.imageScale,
self.imageScale
);
}
completion:^void(BOOL finished) {
self.imageScale += 0.3f;
}
}];
}
#end
I am currently trying to build an image viewer that moves to the next photo every 30 seconds, which also allows to swipe left / right to the next/previous photo manually.
So far I have built two separate apps, one with the swiping functionality and the second with the timer. I have tried many tutorials on the web and am looking for someone to point me in the right direction.
This code switches images every thirty seconds
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *animatedImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0, 55, 400, 550)];
[self.view addSubview:animatedImageView];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"IMG_0052.JPG"],
[UIImage imageNamed:#"IMG_0054.JPG"],
[UIImage imageNamed:#"IMG_0081.JPG"],
nil];
animatedImageView.animationDuration = 30.0 * [animatedImageView.animationImages count];
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];
}
This second piece of code has the swiping functionality, I am looking to combine the two
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image1 = [UIImage imageNamed:#"workExample.jpg"];
UIImage *image2 = [UIImage imageNamed:#"IMG_0054.JPG"];
UIImage *image3 = [UIImage imageNamed:#"IMG_0052.JPG"];
imageArray = [NSArray arrayWithObjects:image1, image2, image3, nil];
for (int i = 0; i < [imageArray count]; i++) {
//This is to create a imageview for every single pagecontrol
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = [imageArray objectAtIndex:i];
[self.scrollView addSubview:imageView];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [imageArray count], scrollView.frame.size.height);
}
I am not using a scrollview and implementing in a different way. I hope it suits your needs
In .h
#interface ViewController : UIViewController
{
NSMutableArray *imagesToAnimate;
UIImageView *animatedImageView;
}
#property (strong, nonatomic) IBOutlet UIView *imageGalleryView;
Im viewDidLoad
animatedImageView = [[UIImageView alloc] initWithFrame:self.imageGalleryView.bounds];
imagesToAnimate = [NSMutableArray arrayWithArray:[NSArray arrayWithObjects:
[UIImage imageNamed:#"IMG_0052.JPG"],
[UIImage imageNamed:#"IMG_0054.JPG"],
[UIImage imageNamed:#"IMG_0081.JPG"],
nil]];
[self.imageGalleryView addSubview:animatedImageView];
//Start a timer
[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector(changeImge) userInfo:nil repeats:YES];
animatedImageView.image = imagesToAnimate[0];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(gallerySwiped)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.imageGalleryView addGestureRecognizer:swipeGesture];
//Give the gallery a border
self.imageGalleryView.layer.borderColor = [UIColor grayColor].CGColor;
self.imageGalleryView.layer.borderWidth = 2.0f;
self.imageGalleryView.layer.cornerRadius = 15.0f;
Now function to update the image as as per scheduled time
-(void)changeImge
{
int index = [imagesToAnimate indexOfObject:animatedImageView.image];
index++;
//this is for the left to right animation ;
CATransition *transition = [CATransition new];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
if (index > imagesToAnimate.count-1) {
animatedImageView.image = imagesToAnimate [0];
}else {
animatedImageView.image = imagesToAnimate[index];
}
[animatedImageView.layer addAnimation:transition forKey:#"transition"];
}
this would handle the swipe
-(void)gallerySwiped
{
[self changeImge];
}
To stop the timer store it in a global variable and call invalidate.I hope this helps.
I've searched everywhere but I did not find a proper solution for my problem. I have a image-sequence playing on start up. After this sequence I want to trigger a function. A function that activates an Image which. So the animation will run fluently into the image without break. I found a lot of solutions on the internet e.g. doing it with a timer but running the code via simulator it works, on device it doesn't and gets delayed :(. Is it a good way to set the timer just by testing when the animation is finished? The method "animationDone" is always triggered on the wrong time!
Here my example code:
- (void)viewDidLoad {
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Rad_start_00000.png"],
[UIImage imageNamed:#"Rad_start_00001.png"],
[UIImage imageNamed:#"Rad_start_00002.png"],
[UIImage imageNamed:#"Rad_start_00003.png"],
[UIImage imageNamed:#"Rad_start_00004.png"],
[UIImage imageNamed:#"Rad_start_00005.png"],
[UIImage imageNamed:#"Rad_start_00006.png"],
[UIImage imageNamed:#"Rad_start_00007.png"],
[UIImage imageNamed:#"Rad_start_00008.png"],
[UIImage imageNamed:#"Rad_start_00009.png"],
[UIImage imageNamed:#"Rad_start_00010.png"],
[UIImage imageNamed:#"Rad_start_00011.png"],
[UIImage imageNamed:#"Rad_start_00012.png"],
[UIImage imageNamed:#"Rad_start_00013.png"],
[UIImage imageNamed:#"Rad_start_00014.png"],
[UIImage imageNamed:#"Rad_start_00015.png"],
[UIImage imageNamed:#"Rad_start_00016.png"],
[UIImage imageNamed:#"Rad_start_00017.png"],nil];
[imageView setAnimationDuration:0.7];
[imageView setAnimationRepeatCount:1];
[imageView startAnimating];
[self performSelector:#selector(animationDone) withObject:nil afterDelay:1.2];
}
First, you should try using the same interval for both the afterDelay parameter to performSelector and the setAnimationDuration (e.g., 0.7 seconds for both).
If that doesn't quite do what you want, you can programmatically call timer, constantly checking to see if the animation is done. For example, you could set up a display link (it's like a timer, but linked to updates to the display), and check imageView.isAnimating, e.g.:
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (nonatomic, strong) CADisplayLink *displayLink;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView.animationImages = #[[UIImage imageNamed:#"Rad_start_00000.png"],
[UIImage imageNamed:#"Rad_start_00001.png"],
[UIImage imageNamed:#"Rad_start_00002.png"],
[UIImage imageNamed:#"Rad_start_00003.png"],
[UIImage imageNamed:#"Rad_start_00004.png"],
[UIImage imageNamed:#"Rad_start_00005.png"],
[UIImage imageNamed:#"Rad_start_00006.png"],
[UIImage imageNamed:#"Rad_start_00007.png"],
[UIImage imageNamed:#"Rad_start_00008.png"],
[UIImage imageNamed:#"Rad_start_00009.png"],
[UIImage imageNamed:#"Rad_start_00010.png"],
[UIImage imageNamed:#"Rad_start_00011.png"],
[UIImage imageNamed:#"Rad_start_00012.png"],
[UIImage imageNamed:#"Rad_start_00013.png"],
[UIImage imageNamed:#"Rad_start_00014.png"],
[UIImage imageNamed:#"Rad_start_00015.png"],
[UIImage imageNamed:#"Rad_start_00016.png"],
[UIImage imageNamed:#"Rad_start_00017.png"]];
[self.imageView setAnimationDuration:0.7];
[self.imageView setAnimationRepeatCount:1];
[self.imageView startAnimating];
[self startDisplayLink];
}
- (void)startDisplayLink
{
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:#selector(handleDisplayLink:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)stopDisplayLink
{
[self.displayLink invalidate];
self.displayLink = nil;
}
- (void)handleDisplayLink:(CADisplayLink *)displayLink
{
if (!self.imageView.isAnimating) {
[self stopDisplayLink];
NSLog(#"done");
// do whatever else you want here
}
}
#end
try using the delegate of the animation
and the setAnimationDidStopSelector:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/clm/UIView/setAnimationDelegate:
I have successfully created a UIView which plays an animation of images from an NSArray of images. I am trying to get this image to move at the same time the animation is playing. Use of the .center property of UIView just isn't working, and I must have some sort of syntactic error, which I cannot for the life of me figure out.
Original post:
I'm playing with brandontreb's tutorial. I can successfully get the animated sprite to dance in place, but I want it to actually scoot across the screen. Can some dear soul:
Please help me fix what I'm doing wrong to make the center of the UIimage
actually move across screen?
Or even better, update this to
"animation block" best practices?
I will surely study your fixes for hours.
- (IBAction)startWalking:(UIButton *)sender {
NSArray * imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"11.png"],
[UIImage imageNamed:#"12.png"],
nil];
UIImageView * ryuJump = [[UIImageView alloc] initWithFrame:
CGRectMake(100, 125, 150, 130)];
ryuJump.animationImages = imageArray;
ryuJump.animationDuration = 1.1;
ryuJump.animationRepeatCount=5;
CGPoint p = ryuJump.center;
p.x += 100;
ryuJump.center = p;
[UIView animateWithDuration:20
delay:0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
ryuJump.center = CGPointMake(p.x, p.y);
}
completion:^(BOOL finished){
}];
ryuJump.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview:ryuJump];
[ryuJump startAnimating];
}
Remove:
ryuJump.center = p;
You're basically telling uiview to animate to the exact same position you just set it to (so nothing will happen).
I have an idea about how to add animated UIImageView using frame by frame method BUT my question is about How to animate UIImageView ALREADY added on view controller storyboard as IBOutlet UIImageView .
what will be changed at this peace of code ?!
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"bookmark1.png"],
[UIImage imageNamed:#"bookmark2.png"],
[UIImage imageNamed:#"bookmark3.png"],
[UIImage imageNamed:#"bookmark4.png"],
[UIImage imageNamed:#"bookmark5.png"],nil];
myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:CGRectMake(0, 0, 131, 125)];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25;
myAnimatedView.animationRepeatCount = 1;
[myAnimatedView startAnimating];
[self.navigationController.view addSubview:myAnimatedView];
I want to animate this image like that over my viewController
http://imageshack.us/a/img717/3051/bookmarkl.gif
It's even easier. First, add in .h file:
#property (nonatomic, retain) IBOutlet UIImageView *iV;
After, connect this outlet to the actual UIImageView on your storyboard. Change your code:
iV.animationImages = myImages;
iV.animationDuration = 0.25;
iV.animationRepeatCount = 1;
[iV startAnimating];
And that's all. Hope this helped
p.s. And yes, don't forget iV = nil; in - (void)viewDidUnload method
UPD: added
[self performSelector:#selector(animationDone) withObject:nil afterDelay:0.25];
After startAnimating call, and obviously added animationDone method:
- (void)animationDone {
[iV stopAnimating];
[iV setImage:[UIImage imageNamed:#"bookmark5.png"]];
}
Well it will be pretty much the same. You dont need to allocate your image view [[UIImageView alloc] init] nor do you need to add it to the viewController. The rest is the same.