Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AxoNeuron

macrumors 65816
Original poster
Apr 22, 2012
1,251
855
The Left Coast
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_4772.PNG
 
Last edited:

TheWatchfulOne

macrumors 6502a
Jun 19, 2009
838
972
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:

Changing the frame rectangle automatically redisplays the receiver without invoking the drawRect: method. If you want the drawRect: method invoked when the frame rectangle changes, set the contentMode property to UIViewContentModeRedraw.
 

moonman239

Cancelled
Mar 27, 2009
1,541
32
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.
 

AxoNeuron

macrumors 65816
Original poster
Apr 22, 2012
1,251
855
The Left Coast
You should probably look into using one of the various 2D game engines out there, such as SpriteKit, Cocos2D, etc.
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.