Swift - Stopping an animation after the last image has appeared? - swift

I'm trying to learn animation in swift. I have an explosion composed of 77 images, and I've stumbled upon a couple issues.
1) I'm trying to make the animation automatically stop once 77.png has appeared. Here is what I have so far. Obviously, it is currently in a continuous animation loop.
2) There is ~1 second delay for the animation to start. However, after it has animated once, and I click animate again, it is instant from then on. How can I make the first animation instant as well?
#IBOutlet var explosionSequence: UIImageView
var imgListArray :NSMutableArray = []
for countValue in 1...77 {
var strImageName : String = "\(countValue).png"
var image = UIImage(named:strImageName)
imgListArray .addObject(image)
}
explosionSequence.animationImages = imgListArray as [AnyObject];
explosionSequence.startAnimating()
//i want to stop animation here after all 77 .pngs have appeared
thank you in advance!!

You can use UIImageView method animationRepeatCount to limit your animation loop to 1.
The default value is 0, which specifies to repeat the animation
indefinitely:
explosionSequence.animationRepeatCount = 1
You can also use animationDuration to adjust the time of you animation.

Related

SpriteKit - change animation speed dynamically

I am new to the SpriteKit framework and cannot figure out how to do the following:
In the middle of the scene I have a sprite to which I apply an .animateWithTextures SKAction. Now I simply want to increase the speed of that animation, or decrease its duration for the same effect.
I made the SKAction a property of my GameScene class so I can access its properties from everywhere, but neither changing its speed nor its duration affects the animation.
I read several threads, the closest to my problem being this one:
How to change duration of executed SpriteKit action
which is from seven years ago, and none of the answers is working for me.
Here is basically what I do:
class GameScene: SKScene {
var thePlayer: SKSpriteNode = SKSpriteNode()
var playerAnimation: SKAction?
...
...
func setInitialAnimation() {
let walkAnimation = SKAction.repeatForever(SKAction(named: "PlayerWalk", duration: 1)!)
self.playerAnimation = walkAnimation
self.thePlayer.run(walkAnimation)
}
func changeAnimationDuration(to duration: CGFloat) {
self.playerAnimation?.duration = duration
}
}
The animation starts when the setInitialAnimation method is called, but changing the action's duration has no effect. The only thing that works so far is removing the old action from the player sprite and running a new one.
Should it not be possible to change the properties of a running SKAction or is that some kind of fire and forget mechanism?
Play around with the timePerFrame to get the correct animation speed.
var frames = [SKTexture]()
for i in 1...10 {
frames.append(SKTexture(imageNamed: "YourTextureAnimationFrame\(i)"))
}
let animate = SKAction.animate(with: frames, timePerFrame: 0.025)
//timePerFrame setting the speed of the animation.
node.run(animate)

Move and Jump animation for UIImageView : Swift 4

I need to make my image jump and move down with animation. I am using sprites to show the jump animation. But, when I try to combine it with move down animation, it does not work. I am trying it on click of a button. Below is my code.
My animation works properly for second click but for first click it just jumps at its current location and at second click it jumps and moves down. I am trying to make it jump and move down at first button click.
#IBAction func targetTouchAction(_ sender: Any) {
let images: [UIImage] = (1...10).map { return UIImage(named: "Jump_\($0)")! }
self.jackImage.frame.origin.y = self.jackImage.frame.origin.y + 100.0
self.jackImage.animationImages = images
self.jackImage.animationDuration = 2.0
self.jackImage.animationRepeatCount = 1
self.jackImage.startAnimating()
}
I am trying this from last two three hours but no luck.
You are mixing 2 different types of animation. Don't do that. Using the animationImages property of a UIImageView is separate from using UIView animation methods like UIView.animate(duration:animations:)
Get rid of that UIView animation wrapper, and just specify the animation images and other settings for your image view:
#IBAction func targetTouchAction(_ sender: Any) {
let jumpImages = ["Jump_1","Jump_2","Jump_3","Jump_4","Jump_5","Jump_6","Jump_7","Jump_8","Jump_9","Jump_10"]
var images = [UIImage]()
for image in jumpImages{
images.append(UIImage(named: image)!)
}
self.imageView.frame.origin.y = self.imageView.frame.origin.y + 100.0
self.imageView.animationImages = images
self.imageView.animationDuration = 2.0
self.imageView.animationRepeatCount = 1
self.imageView.startAnimating()
}
Note that loading the array of images into the image view each time you trigger the animation is unnecessary, but it should still work.
I haven't worked with SpriteKit, so I'm not sure how your image view animation will interact with that. (Plus you didn't show that part of your code.)
Also, note that you could create your array of images with 1 line of code, and without using a hard-coded array of filenames:
let images: [UIImage] = (1...10).map { return UIImage(named: "Jump_\($0)") }
That code creates a sequence from 1 to 10. It then uses the map statement to map that sequence into an array of images by feeding the value into a string, and using that string as the image name in a call to UIImage(named:)

Spritekit Animation not working

I'm trying to make a game where the main user is animated and i converted the gif to png and took each frame and put them all into a folder. However, right now I'm simply trying to get the first frame to appear, but nothing appears. Sorry if i worded this weird my English isn't very good.
Right now I think just the first frame, "trumpyhappy1.png" should show when I try to run it, but when I run it nothing shows up.
For anyone that stumbles upon this and is still wondering why their animations are blank. I found that programmatically adding the actions would cause this before viewDidAppear got called. However, setting the animation in the scene file directly I was able to get around the blank animations
So basically in spritekit to animate a spritenode you should be doing this
override func didMove() {
let mainGuy = SKSpriteNode()
self.addChild(mainGuy)
// Give your size and position and everything
// Then to animate that spriteNode with frames
let textureAtlas = SKTextureAtlas(named: "trump")
let frames: [SKTexture] = []
for i in 0...textureAtlas.textureNames.count {
var name = "trumphappy\(i).png"
frames.append(SKTexture(named: name))
}
// Then to create the animation add this code
let animation = SKAction.animate(with: frames, timePerFrame: ) // Whatever time you want your animation to spend on each frame
}
That should work, hope it works!

Alternating animation loop

I have a repeating animation that fades a UILabel and UIImage asynchronously but I cant figure out how to make the UIImage appear for longer than the UILabel, i want the animation to alternate between the label and image so the image appears for a duration of 5 seconds and the label appears for a duration of 2 seconds:
override func viewDidLoad() {
super.viewDidLoad()
UIView.animate(withDuration: 5, delay: 2.0, options:[UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse], animations: {
self.fadeIn()
self.fadeOut()
}, completion: nil)
}
func fadeIn () {
self.label.alpha = 0.0
self.image.alpha = 1.0
}
func fadeOut () {
self.label.alpha = 1.0
self.image.alpha = 0.0
}
You can achieve the effect you are after is multiple ways.
In the viewDidLoad instead of creating an animation block and calling your fade methods from within, just simply have separate animation blocks within each of the methods with different delay value.
So remove the UIView.animate from the ViewDidLoad and simply jus call fadeIn() and fadeOut(), and inside your fade methods you would add the UIView.animate.... and set ur desired duration and timing. This way you have a lot more control over you animation and you can tweak the values until you achieve the effect you desire.
Second option would be as previously mentioned to use key frame animation.
Use a repeating keyframe animation with a duration of 7 seconds, consisting of two keyframes:
The first keyframe starts at the start and has a duration that is 5/7 of the total duration, and fades one way.
The second keyframe starts 5/7 of the way through and has a duration that is 2/7 of the total duration, and fades the other way.

Animated view on mono touch iPhone project with c#

public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
List<UIImage> myImages = new List<UIImage>();
myImages.Add(UIImage.FromFile(#"image/1.jpeg"));
myImages.Add(UIImage.FromFile(#"image/2.jpeg"));
var myAnimatedView = new UIImageView(this.hatchAgeSubView.Bounds);
myAnimatedView.AnimationImages = myImages.ToArray();
myAnimatedView.AnimationDuration = 3; // Seconds
myAnimatedView.AnimationRepeatCount = 4; // 0 = Loops Forever
myAnimatedView.StartAnimating();
hatchAgeSubView.AddSubview(myAnimatedView);
}
as you see above there is an animation with two images for 12 seconds.
I want to start another another animation just after 12 seconds. (at the end of the animation above)
Are there any call back method or are there any idea to make this next animation playing automatically.??
If you're using iOS 4 or better you can use the new Animate method and get notified upon completion of the first animation sequence:
var delay = 0.0f;
UIView.Animate(delay, () => {
UIView.SetAnimationRepeatCount(4);
//Regular animation code goes here.
}, () => {
// This fires when the first animation block above is completed
//Second animation sequence code goes here.
});
UPDATE: Please do not call animations from ViewDidLoad it will make the experience laggy and your users unhappy. Try calling it async from ViewDidAppear. For more information on View Events check out my blog post on the subject:
http://blog.devnos.com/uiviewcontroller-y-u-no-fire-view-events