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.
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.
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.
Last edited: