iOS Question ImageView animation

ilan

Expert
Licensed User
Longtime User
hi

i just found out that the native UIimageview has an animating function.
so instead of using a timer and loading images from an array we could use imageview.startanimating.

is it possible to access that function with nativobject?

B4X:
   // Load images
    NSArray *imageNames = @[@"win_1.png", @"win_2.png", @"win_3.png", @"win_4.png",
                        @"win_5.png", @"win_6.png", @"win_7.png", @"win_8.png",
                        @"win_9.png", @"win_10.png", @"win_11.png", @"win_12.png",
                        @"win_13.png", @"win_14.png", @"win_15.png", @"win_16.png"];
    NSMutableArray *images = [[NSMutableArray alloc] init];
    for (int i = 0; i < imageNames.count; i++) {
        [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
    }
    // Normal Animation
    UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 193)];
    animationImageView.animationImages = images;
    animationImageView.animationDuration = 0.5;
    [self.view addSubview:animationImageView];
    [animationImageView startAnimating];

http://www.appcoda.com/ios-programming-animation-uiimageview/

thanx
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim images As List = Array(LoadBitmap(File.DirAssets, "crash.png"), LoadBitmap(File.DirAssets, "missile2.png"))
Dim no As NativeObject = ImageView1
no.SetField("animationImages", images)
no.SetField("animationDuration", 0.5) '0.5 seconds (complete cycle)
no.RunMethod("startAnimating", Null)
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Not sure if this question is related to your SpriteKit project. But that's the way you would do it in SpriteKit:

B4X:
    Dim Texture1 As SKTexture
    Texture1.Initialize
    Texture1.TextureWithImageNamed("Pic1")
   
    Dim Texture2 As SKTexture
    Texture2.Initialize
    Texture2.TextureWithImageNamed("Pic2")
   
    Dim TextureAnimation As SKAction
    TextureAnimation.Initialize
    TextureAnimation.AnimateWithTextures(Array(Texture1, Texture2),0.2)
   
    Dim TextureAction As SKAction
    TextureAction.Initialize
    TextureAction.RepeatActionForever(TextureAnimation)

Now you can run the SKAction on any Node.

Jan
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Not sure if this question is related to your SpriteKit project. But that's the way you would do it in SpriteKit:

B4X:
    Dim Texture1 As SKTexture
    Texture1.Initialize
    Texture1.TextureWithImageNamed("Pic1")

    Dim Texture2 As SKTexture
    Texture2.Initialize
    Texture2.TextureWithImageNamed("Pic2")

    Dim TextureAnimation As SKAction
    TextureAnimation.Initialize
    TextureAnimation.AnimateWithTextures(Array(Texture1, Texture2),0.2)

    Dim TextureAction As SKAction
    TextureAction.Initialize
    TextureAction.RepeatActionForever(TextureAnimation)

Now you can run the SKAction on any Node.

Jan


thanx @JanPRO. i saw how you did it in your flappy bird example but then i found out that the native UIimageview also has such a method so i was wondering if b4i uses native views and if it does we could enter those methods with a native object and @Erel confirmed that.

so those are very good news because it will save a lot of headache using the animation method of the imageview instead of timers and calculating frames for the duration of the image animation...

much more simpler like this. maybe erel could implement those methods in the next b4i update so we could access them directly. :rolleyes:

EDIT: oh btw like this its shorter:

B4X:
    Dim Textures(2) As SKTexture
    For i = 0 To Textures.length-1
        Textures.Initialize
        Textures.TextureWithImageNamed("Pic" & i)
    Next

    Dim TextureAnimation As SKAction
    TextureAnimation.Initialize
    TextureAnimation.AnimateWithTextures(Textures,0.2)

    Dim TextureAction As SKAction
    TextureAction.Initialize
    TextureAction.RepeatActionForever(TextureAnimation)

;)
 
Upvote 0
Top