开发者

problem with timer animation

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];


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜