UIImageView stopAnimation - ios

In Xcode I've got 2 buttons: Start and Stop.
When you click Start startClickis called, when you click Stop stopClick is called.
When you click Start a UIImageView image will flash different images from the NSArray. When you click Stop, it stops, but I want it to stop on the image that was showing when I clicked Stop. At the moment it just stops and disapears... Any help?
Heres the part of the code:
- (IBAction)startClick:(id)sender {
color.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed: #"img1.png"],
[UIImage imageNamed: #"img2.png"],
[UIImage imageNamed: #"img3.png"],
[UIImage imageNamed: #"img4.png"],nil];
[color setAnimationRepeatCount: 0];
color.animationDuration = 1;
[color startAnimating];
}
- (IBAction)stopClick:(id)sender {
[color stopAnimating];
//What code do i put here to display the last image? (as i clicked stop).
}
I would also like to add if statments for if the color displayed is img1.png then do this... and if its img2.png do that...

This code will pause your Animation:
color.frame = [[color.layer presentationLayer] frame];
[color.layer removeAllAnimations];

Related

iOS make screenshot of entire screen issue

I have self view with image view:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
[self setBluredImageView:imageView];
[self addSubview:imageView];
- (UIImage *)takeSnapshotOfView:(UIView *)view
{
CGFloat reductionFactor = 1;
UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width/reductionFactor, view.frame.size.height/reductionFactor));
[view drawViewHierarchyInRect:CGRectMake(0, 0, view.frame.size.width/reductionFactor, view.frame.size.height/reductionFactor) afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
In method when I want to show my self view over other view I make this:
- (void)showMe; {
AppDelegate* app = [AppDelegate shared];
[app.window addSubview:self];
UIImage *image = [self blurWithImageEffects:[self takeSnapshotOfView:app.window]];
[[self bluredImageView] setImage:image];
[UIView animateWithDuration:0.4 animations:^{
[self setAlpha:1.0];
}];
}
So as you can see I want to blur "graphic context" that based on main window view. At first time when I present my self view it works perfect, but then, something like blurred image multiply each other.
So this is image when I just first show my view:
When I present my view few times the blurred image looks like this:
So as you can see each time blurred screenshot is different, but I use the same method for getting screenshot and don't update content of the view controller or other ui parts.
Some methods and image categories found here.
You've created a loop of blurring. When you take the screenshot of the view a second time, the bluredImageView is also in the screenshot. That is why you see the effect multiplied. Try removing it and only capturing the context without the effect, then adding it back
- (UIImage *)takeSnapshotOfView:(UIView *)view
{
//Remove the blured image before taking another screenshot.
[self bluredImageView] removeFromSuperview];
CGFloat reductionFactor = 1;
UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width/reductionFactor, view.frame.size.height/reductionFactor));
[view drawViewHierarchyInRect:CGRectMake(0, 0, view.frame.size.width/reductionFactor, view.frame.size.height/reductionFactor) afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Add it back now that the effect is done
[self addSubview:[self bluredImageView];
return image;
}

Animation not running in the order that code is written

I am trying to set up a button that plays an animation in an image view that's part of my root view controller and then proceed to change the sub view controller.
But for some reason, the code changes the view controller and THEN plays the animation.
I would think that the animation segment would be implemented before the view controller change takes place because, well, it's first in the code.
The code does everything fine, it just does it backwards. Any one have any ideas of what's going wrong?
- (IBAction)next:(UIButton *)sender {
clouds.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"cloudAnim.0001.png"],
[UIImage imageNamed:#"cloudAnim.0002.png"],
[UIImage imageNamed:#"cloudAnim.0003.png"],
[UIImage imageNamed:#"cloudAnim.0004.png"],
[UIImage imageNamed:#"cloudAnim.0005.png"],
[UIImage imageNamed:#"cloudAnim.0006.png"],
[UIImage imageNamed:#"cloudAnim.0007.png"],
[UIImage imageNamed:#"cloudAnim.0008.png"],
[UIImage imageNamed:#"cloudAnim.0009.png"],
[UIImage imageNamed:#"cloudAnim.0010.png"],
[UIImage imageNamed:#"cloudAnim.0011.png"],
[UIImage imageNamed:#"cloudAnim.0012.png"],
[UIImage imageNamed:#"cloudAnim.0013.png"],
[UIImage imageNamed:#"cloudAnim.0014.png"],
[UIImage imageNamed:#"cloudAnim.0015.png"],
[UIImage imageNamed:#"cloudAnim.0016.png"],
[UIImage imageNamed:#"cloudAnim.0017.png"],
[UIImage imageNamed:#"cloudAnim.0018.png"],
[UIImage imageNamed:#"cloudAnim.0019.png"],
[UIImage imageNamed:#"cloudAnim.0020.png"],
[UIImage imageNamed:#"cloudAnim.0021.png"],
[UIImage imageNamed:#"cloudAnim.0022.png"],
[UIImage imageNamed:#"cloudAnim.0023.png"],
[UIImage imageNamed:#"cloudAnim.0024.png"],
[UIImage imageNamed:#"cloudAnim.0025.png"],
[UIImage imageNamed:#"cloudAnim.0026.png"],
[UIImage imageNamed:#"cloudAnim.0027.png"],
[UIImage imageNamed:#"cloudAnim.0028.png"],
[UIImage imageNamed:#"cloudAnim.0029.png"],
[UIImage imageNamed:#"cloudAnim.0030.png"],
[UIImage imageNamed:#"cloudAnim.0031.png"],
[UIImage imageNamed:#"cloudAnim.0032.png"],
[UIImage imageNamed:#"cloudAnim.0033.png"],
[UIImage imageNamed:#"cloudAnim.0034.png"],
[UIImage imageNamed:#"cloudAnim.0035.png"],
[UIImage imageNamed:#"cloudAnim.0036.png"],
[UIImage imageNamed:#"cloudAnim.0037.png"],
[UIImage imageNamed:#"cloudAnim.0038.png"],
[UIImage imageNamed:#"cloudAnim.0039.png"],
[UIImage imageNamed:#"cloudAnim.0040.png"],
[UIImage imageNamed:#"cloudAnim.0041.png"],
[UIImage imageNamed:#"cloudAnim.0042.png"],
[UIImage imageNamed:#"cloudAnim.0043.png"],
[UIImage imageNamed:#"cloudAnim.0044.png"],
[UIImage imageNamed:#"cloudAnim.0045.png"],
[UIImage imageNamed:#"cloudAnim.0046.png"],
[UIImage imageNamed:#"cloudAnim.0047.png"],
[UIImage imageNamed:#"cloudAnim.0048.png"], nil];
[clouds setAnimationRepeatCount:1];
clouds.animationDuration = 2.0;
[clouds startAnimating];
if (currentPage == 1){
self.page02ViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"page02"];
[self.view insertSubview:_page02ViewController.view atIndex:0];
[self.page01ViewController.view removeFromSuperview];
}
if (currentPage == 2){
self.page03ViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"page03"];
[self.page02ViewController.view removeFromSuperview];
[self.view insertSubview:_page03ViewController.view atIndex:0];
}
currentPage = currentPage +1;
if (currentPage >3){
currentPage = 3;
}
}
Animations are asynchronous, meaning the code below will be executed immediately.
If you want to wait until one loop of the animation has completed, you'll need to delay the code afterward. The simplest way is to use dispatch_after:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// Your view controller switching code will be executed after 2 seconds...
});
Beware that, since the method returns immediately, there is nothing stopping the user from hitting the button again and queuing up another transition. You may wish to disable user interaction during the transition, though a 2-second transition will feel like an eternity to the user if they have to switch often.

Image view animation

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.

Change background image and return to the previous view in Xcode

I`m making simple game where you can select the background image with this code:
-(IBAction)FirstButtonPressed:(id)sender
{
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"picture4.jpg"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
//[[self parentViewController] dismissModalViewControllerAnimated:YES]; <-- Not returning to previous view
}
I couldn`t figure it out which would be the appropriate code to return the user in the previous view. The uncommented code above is not working, selects the image, but stays in current view. Any ideas? Thanks

iOS animationImages: I like to move it while it plays

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).

Resources