定时器动画的问题
这是我的代码:
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您没有在这里发布所有代码,所以我必须猜测。
您是否在
onTimer
中创建 imageView,然后尝试在onTimer2
中移动“它们”?如果是,那么
因为当您尝试将
imageView
移动到onTimer2
方法,您仅移动您创建的最新一个。为了移动您创建的每个 imageView,您最好在
onTimer2
方法中添加一个for 循环
。另外,你最好创建一个 NSMutableArray 来保存你所有的 imageView,或者你可以使用
imageView.tag
再次获取你的 imageView,而不必创建一个 NSMutableArray 并稍后释放它。以下是如何使用 NSMutableArray 保存和读取所有 imageView 的示例:
将其添加到类的
.h
文件中:编辑您的
.m
文件:或者您可以使用
imageView.tag
像这样:将其添加到类的
.h
文件中:编辑您的
.m
文件:这两个代码是由 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" inonTimer2
?If YES, then
Because you are always setting the
imageView
var to the latest imageView you created inonTimer
method, when you trying move theimageView
inonTimer2
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 youronTimer2
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:Edit your
.m
file:Or you can use
imageView.tag
like this:Add this to your class's
.h
file:Edit your
.m
file:The two code are compiled by brain, correct me if there is any mistype.
The first way is recommended, because it has better performance.
每次调用
-onTimer
时,您都会覆盖 ivarimageView
。您可能应该将每个创建的图像视图放入一个数组或集合中,或者您可以根据您的视图结构迭代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 overself.view.subviews
.