UIImageView Animation

Discussion in 'iOS Programming' started by AxoNeuron, Sep 16, 2014.

  1. AxoNeuron, Sep 16, 2014
    Last edited: Sep 16, 2014

    AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #1
    Hey everyone,

    I am very new to iOS development and programming in general so please excuse my ignorance. I have a tiny app with the moon and the earth. It uses Newton's and Kepler's equations of planetary motion and gravitational attraction.

    In order to move the planet and the moon, I use a UIImageView with the .png image. 30 times a second, I feed in a CGPoint with the display locations of both objects. I first use the method removeFromSuperview on both objects, then set the new frame with the new location and add it back as a subview of self.view. This happens 30 times a second, and now that I am using the actual images instead of simple circles, I am getting a bit of lag even on the A7.

    Does anyone know of a better way to animate this dynamically with a changing CGPoint location 30x a second?

    Thanks!

    Here is the actual code. This method gets called 30 times a second and uses two CGPoints containing updated location coordinates, one for each object.

    Code:
    -(void)drawSpaceObjects
    {
        if (self.timerHasFired == NO) {
            self.timerHasFired = YES;
            [self.model kinematics];
        }
        
        if (self.earthImageView != nil) {
            [self.earthImageView removeFromSuperview];
            [self.moonImageView removeFromSuperview];
        }
        
        
        NSMutableArray *spaceObjectsArray = [self.model getSpaceObjects:self.timeInSeconds];
    
        
        BHModel *moon = spaceObjects[0];
        BHModel *earth = spaceObjects[1];
        
        self.earthImageView = [[UIImageView alloc] initWithFrame:CGRectMake(earth.location.x, earth.location.y, 40, 40)];
        self.moonImageView = [[UIImageView alloc] initWithFrame:CGRectMake(moon.location.x, moon.location.y, 20, 20)];
        
        [self.earthImageView setImage:self.earthImage];
        [self.moonImageView setImage:self.moonImage];
        [self.view addSubview:self.moonImageView];
        [self.view addSubview:self.earthImageView];
    }
    
    As you can see, at t=0 the very first time the timer has fired, it simply creates the objects on the display, but after that, it first removes it from the superview. I am hoping there might be a better method out there that is a lot more efficient and can accept movements via changing CGPoints for location.

    [​IMG]
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You should probably look into using one of the various 2D game engines out there, such as SpriteKit, Cocos2D, etc.
     
  3. TheWatchfulOne macrumors 6502

    Joined:
    Jun 19, 2009
    #3
    Ultimately, deja's advice is better, but...

    If you want to keep going this direction, you don't have to remove the images and add them back. From reading the docs, it seems you can just set the frame and UIKit will animate it.

    UIImagView is a subclass of UIView and when I looked up UIView, I found this:

     
  4. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #4
    The Core Animation framework, which is included with the iOS SDK, can be used to handle animations. In fact, using it is better than using a loop to change the CGPoints.
     
  5. AxoNeuron thread starter macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #5
    I am going to start with spritekit shortly but I figured it was a bit much for something like this. Thank all of you! I think for this particular program the redraw content mode would work, I may move it all over to spritekit in a few weeks though. This is sort of like my pet project and I like adding/changing things as my skills grow.
     

Share This Page