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).
Related
I am trying to make a simples application where the user can go through notes and then do a quiz. I am presenting this notes as images and the image would be assigned to an UIImageView to be shown to the user.
In my view did load I am mostly setting the UI. Then I would be creating my array containing my array of images (notes)
- (void)viewDidLoad {
UIColor *colourForNavigationBar = [self colorWithHexString:#"919D9F"];
[[UINavigationBar appearance] setBarTintColor:colourForNavigationBar];
UIColor *colourForBackground = [self colorWithHexString:#"F0F3F4"];
self.view.backgroundColor = colourForBackground;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:27.0];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
self.navigationItem.titleView = label;
label.text = #"Notes";
[label sizeToFit];
UIColor *colourForButton = [self colorWithHexString:#"919D9F"];
self.previousButton.backgroundColor = colourForButton;
self.nextButton.backgroundColor = colourForButton;
finishedNotes = NO;
playerPlaying = NO;
arrayOfImages = [NSArray arrayWithObjects:
[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"],
[UIImage imageNamed:#"13.png"],
nil];
}
Then there is the 'next' button which allows the user to continue on to the next note. Below is the code for the action
- (IBAction)nextNote:(id)sender {
if (finishedNotes == YES) {
[self performSegueWithIdentifier:#"quizFromNotes" sender:self];
}
self.previousButton.enabled = YES;
stateOfPhoto++;
if (stateOfPhoto < 13) {
UIImage *image = [arrayOfImages objectAtIndex:stateOfPhoto-1];
self.imageView.image = image;
}
if (stateOfPhoto == 13) {
UIImage *image = [arrayOfImages objectAtIndex:stateOfPhoto-1];
self.imageView.image = image;
[self.nextButton setTitle:#"Begin Quiz" forState:UIControlStateNormal];
finishedNotes =YES;
}
if (playerPlaying == YES) {
[self.notesPlayer stop];
playerPlaying = NO;
}
}
Everything is working fine and smoothly. However whenever there is the change of image, the memory used by the application increasing by a few megabytes. Below is the photo of the memory usage.
From the black line onwards, when I press the 'next' button, the memory used increases. It just repeats itself until the user has continued onto the quiz. If I end the quiz and start over the notes, the memory usage would continue to rise from the previous memory usage (73 mb). Eventually, the app would crash and I cannot launch the app anymore.
I have never faced memory leakage issues before and hence, I am not sure of what to do. I have tried googling for the past hour and am not able to come up with a solution to this. So my question is how would I be able to release the memory or reduce the memory usage.
I think it's because of your images, how big are those images you're showing?
Based on this answer: https://stackoverflow.com/a/22662754/3231194, you can either use this method to scale your images before putting them into your UIImageView:
-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
But that will take up a CPU usage (I'm not sure though if it is a big deal).
OR
You can just resize your images by yourself before importing them to your Xcode project.
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];
I have a animation assigned to the uiimageview with the following code
Fire.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"firework1.png"],
[UIImage imageNamed:#"firework2.png"],
[UIImage imageNamed:#"firework3.png"],
[UIImage imageNamed:#"firework10.png"],nil];
[Fire setAnimationRepeatCount:0];
Fire.animationDuration = 0.5;
[Fire startAnimating];
I want the animation to stop when it reaches 0.5 I tried a if statement but it didn't work. Please help.
According to the UIImageView documentation setting the animation repeat count to 0 will make it repeat indefinitely, try using [Fire setAnimationRepeatCount:1]; instead.
This question already has answers here:
iOS: How to detect when an animation is finished?
(3 answers)
Closed 8 years ago.
I have this code for my animation:
facebookImage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Animation01.png"],
[UIImage imageNamed:#"Animation02.png"],
[UIImage imageNamed:#"Animation03.png"],
[UIImage imageNamed:#"Animation04.png"],
[UIImage imageNamed:#"Animation05.png"],
nil];
facebookImage.animationDuration = 1.3;
facebookImage.animationRepeatCount = 1;
[facebookImage startAnimating];
How do I use delegate callback (or something else) for doing something after this animation is complete?
I can't do this :
[UIView setAnimationDelegate:self];
because I don't have this option in UIImageview
thanks :)
just add like this:
float animationDuration = 1.3;
facebookImage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Animation01.png"],
[UIImage imageNamed:#"Animation02.png"],
[UIImage imageNamed:#"Animation03.png"],
[UIImage imageNamed:#"Animation04.png"],
[UIImage imageNamed:#"Animation05.png"],
nil];
facebookImage.animationDuration = animationDuration;
facebookImage.animationRepeatCount = 1;
[facebookImage startAnimating];
[self performSelector:#selector(someMethodDelayedToEndOfAnimation) withObject:nil afterDelay: animationDuration];
I have two different images, that are essentially just inverses of each other. I would like to animate them back and forth to create a somewhat moving effect. In the AppDelegate didFinishLaunching I have:
myImageWalk = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 568)];
[self.myImageWalk setImage: [self loadingImage]];
[window addSubview:myImageWalk];
[window bringSubviewToFront:myImageWalk];
Then, also in the App Delegate, I have:
-(UIImage*)loadingImage
{
NSArray *animationFrames = [NSArray arrayWithObjects:
[UIImage imageNamed:#"original.png"],
[UIImage imageNamed:#"second.png"],
[UIImage imageNamed:#"original.png"],
[UIImage imageNamed:#"second.png"],
[UIImage imageNamed:#"original.png"],
[UIImage imageNamed:#"second.png"],
[UIImage imageNamed:#"original.png"],
[UIImage imageNamed:#"second.png"],
[UIImage imageNamed:#"original.png"],
[UIImage imageNamed:#"second.png"],
[UIImage imageNamed:#"original.png"],
[UIImage imageNamed:#"second.png"],
[UIImage imageNamed:#"original.png"],
[UIImage imageNamed:#"second.png"],
[UIImage imageNamed:#"original.png"],
[UIImage imageNamed:#"second.png"],
[UIImage imageNamed:#"original.png"],
[UIImage imageNamed:#"second.png"],
nil];
return [UIImage animatedImageWithImages:animationFrames duration:6.0f];
}
But, nothing happens, no image ever shows up. What am I doing wrong?
Some code below you can try.
Note you don't need to repeat the series of imageNamed:, just set duration as needed for one loop.
myImageWalk = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 568)];
myImageWalk.image = [self loadingImage];
self.myImageWalk.animationRepeatCount = 0;
[window addSubview:myImageWalk];
[window bringSubviewToFront:myImageWalk];
//when you want the animation to start
[self animateImages];
//then later at some appropriate time:
[self stopAnimatingImages];
- (UIImage*)loadingImage {
NSArray *animationFrames = [NSArray arrayWithObjects:
[UIImage imageNamed:#"original.png"],
[UIImage imageNamed:#"second.png"],
nil];
return [UIImage animatedImageWithImages:animationFrames duration:6.0f];
}
- (void)animateImages {
if (![self.myImageWalk isAnimating]) {
[self.myImageWalk startAnimating];
}
}
- (void)stopAnimatingImages {
if ([self.myImageWalk isAnimating]) {
[self.myImageWalk stopAnimating];
}
}
Note: unless this is a test app or class assignment, you should move this logic from AppDelegate to ViewContoller, as it is best practice to minimize the work done during didFinishLaunching.
I haven't tried it, but maybe try wrapping your code inside this block:
[UIView animateWithDuration:6f animations:^{
[UIImage animatedImageWithImages:animationFrames duration:6.0f]
} completion:^(BOOL finished) {
//Callback after animation was completed
[view removeFromSuperview];
}];