2

here is what I wanna do http://www.youtube.com/watch?v=rD3MTTPaK98 I have done the part where an image appears at a random position but then it become hard for me. I don't wanna use UIKit Animations because I use a timer to animate like :imageView.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y); but I can't animate more than one image at once I don't know why ? How can I solve this please

here is my code:

-(void) onTimer {


UIImage * image = [UIImage imageNamed:@"abouffer_03.png"];
imageView = [[UIImageView alloc] initWithImage:image];

[imageView setCenter:[self randomPointSquare]];


[[self view] addSubview:imageView];

[imageView release];

ix=imageView.center.x;
iy=imageView.center.y;

X=(240-ix)/230;
Y=(160-iy)/230;
coteA=(240-ix);
coteB=(160-iy);
angleDeRotation=atan(coteB/coteA);
if(ix>250||0>iy>320){
    imageView.transform = CGAffineTransformMakeRotation(angleDeRotation+3.14);
}
else{
imageView.transform = CGAffineTransformMakeRotation(angleDeRotation);
}
}



-(void)onTimer2{
        imageView.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);
label.text= [NSString stringWithFormat:@"%d", count];
bernard langue
  • 147
  • 1
  • 10

2 Answers2

1

Since your are not posting all of your code here, so I have to guess.

Are you creating imageView in onTimer and then trying to move "them" in onTimer2?

If YES, then

Because you are always setting the imageView var to the latest imageView you created in onTimer method, when you trying move the imageView in onTimer2 method, you are only moving the latest one you created.

In order to move every imageView you've created, you'd better have a for loop in your onTimer2 method.

In additional, you'd better make a NSMutableArray to save all your imageViews, or you can use imageView.tag to get your imageView again without having to create an NSMutableArray and release it later.

Here is an example how to use NSMutableArray to save and read all of your imageViews:

Add this to your class's .h file:

NSMutableArray *views;
NSMutableArray *XArray;
NSMutableArray *YArray;

Edit your .m file:

...
    //Create an array some where in your class:
    views = [[NSMutableArray array] retain];
    XArray = [[NSMutableArray array] retain];
    YArray = [[NSMutableArray array] retain];
...

- (void)onTimer {
    [XArray addObject:[NSNumber numberWithFloat:(240.0f - ix)/230.0f]];
    [YArray addObject:[NSNumber numberWithFloat:(160.0f - iy)/230.0f]];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
    [self.view addSubview:imageView];
    [views addObject:imageView];
    [imageView release];
}

//Read:
- (void)onTimer2 {
    for (NSUInteger i = 0; i < [views count]; i++) {
         UIImageView *imageView = [views objectAtIndex:i];
         CGFloat X = [[XArray objectAtIndex:i] floatValue];
         CGFloat Y = [[YArray objectAtIndex:i] floatValue];
         CGPointMake(imageView.center.x + X, imageView.center.y + Y);
    }
} 

- (void)dealloc {
    ...
    [views release];
    [XArray release];
    [YArray release];
    ...
}

Or you can use imageView.tag like this:

Add this to your class's .h file:

NSInteger imageViewCount;

Edit your .m file:

...
#define kImageViewFirstTag       10000
...

...
    //reset the count some where in your class if needed
    imageViewCount = 0;
...

- (void)onTimer {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
    [self.view addSubview:imageView];
    [views addObject:imageView];
    imageView.tag = kImageViewFirstTag + imageViewCount;
    imageViewCount ++;
    [imageView release];
}


//Read:
- (void)onTimer2 {
    for (NSUInteger i = 0; i < imageViewCount; i++) {
         UIImageView *imageView = (UIImageView *)[self.view viewWithTag:kImageViewFirstTag + i];
         CGPointMake(imageView.center.x + X, imageView.center.y + Y);
    }
}

The two code are compiled by brain, correct me if there is any mistype.

The first way is recommended, because it has better performance.

xuzhe
  • 5,100
  • 22
  • 28
  • I use your code with the array and look what happen there is a problem(I took a video of what happen):http://www.youtube.com/watch?v=ZMIdMMWGSvQ – bernard langue Aug 21 '11 at 16:09
  • What is the problem? All of the images moving to the same direction? If you use the same logic to move every image view, of course they will move to the same direction. That's what you need to work on. Set different X & Y for different imageView for example. – xuzhe Aug 22 '11 at 00:59
  • The problem is that :an imageView is created at a random position then it moves to a specific direction then another imageView is created and it goes to a specific direction, but the previous imageView follows the same direction of the new one. I wanted that every imageView created follow its direction without changing it's direction. Please help me – bernard langue Aug 22 '11 at 11:27
  • Because your X and Y in onTimer2 method is always set to every imageView. That's why they are moving to the same direction. You should use different X and Y for different imageView. Maybe do not use the member ivar X and Y but calculate them for each imageView in the for loop. – xuzhe Aug 22 '11 at 11:48
  • how can I do this please ? I don't really understand – bernard langue Aug 22 '11 at 12:00
  • Would you please tell me how did you calculate the X and Y used in onTimer2 method? You can post the code here and I will give you some advice if I can. :) – xuzhe Aug 22 '11 at 12:11
  • it is in my code that I've posted: ix=imageView.center.x; iy=imageView.center.y; X=(240-ix)/230; Y=(160-iy)/230; – bernard langue Aug 22 '11 at 12:25
  • Sorry, I missed that code. I've just edited the first way in my answer. Hope it helps. If it dose, please vote up and accept this answer :) Thanks! – xuzhe Aug 22 '11 at 14:49
0

You're overwriting your ivar imageView each time -onTimer is called. You should probably put each created imageview in an array or a set, or you could depending on your view structure iterate over self.view.subviews.

nesium
  • 160
  • 1
  • 3