定时器动画的问题

发布于 2024-11-25 17:57:08 字数 1102 浏览 0 评论 0原文

这就是我想做的http://www.youtube.com/watch?v=rD3MTTPaK98 我已经完成了图像出现在随机位置的部分,但随后它对我来说变得困难。我不想使用 UIKit 动画,因为我使用计时器来制作动画,例如:imageView.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);但我不能同时制作多张图像的动画,我不知道为什么?我该如何解决这个问题,

这是我的代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阿楠 2024-12-02 17:57:08

由于您没有在这里发布所有代码,所以我必须猜测。

您是否在 onTimer 中创建 imageView,然后尝试在 onTimer2 中移动“它们”?

如果是,那么

因为当您尝试将 imageView 移动到onTimer2 方法,您仅移动您创建的最新一个。

为了移动您创建的每个 imageView,您最好在 onTimer2 方法中添加一个 for 循环

另外,你最好创建一个 NSMutableArray 来保存你所有的 imageView,或者你可以使用 imageView.tag 再次获取你的 imageView,而不必创建一个 NSMutableArray 并稍后释放它。

以下是如何使用 NSMutableArray 保存和读取所有 imageView 的示例:

将其添加到类的 .h 文件中:

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

编辑您的 .m 文件:

...
    //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];
    ...
}

或者您可以使用imageView.tag 像这样:

将其添加到类的 .h 文件中:

NSInteger imageViewCount;

编辑您的 .m 文件:

...
#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);
    }
}

这两个代码是由 Brain 编译的,如果有任何错误请纠正我。

推荐第一种方式,因为它的性能更好。

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.

末が日狂欢 2024-12-02 17:57:08

每次调用 -onTimer 时,您都会覆盖 ivar imageView 。您可能应该将每个创建的图像视图放入一个数组或集合中,或者您可以根据您的视图结构迭代self.view.subviews

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文