0

NSTimerを使用して画像を上からボタンに移動するために、以下のコードを記述しました。ただし、実行すると、画像を個別に取得することはできません。基本的に、画像は痕跡を残します。私が欲しいのは、それが動いているフレームごとに画像を持っていることです。私は何が欠けていますか?

-(void)printOut:(NSTimer*)timer1;
{

    image = [[UIImageView alloc] initWithFrame:CGRectMake(50, -50+m, 50, 50)];
    [image setImage:[UIImage imageNamed:[NSString stringWithFormat:@"image.png"]]];

    CGRect oldFrame = image.frame;
    image.frame = CGRectMake(oldFrame.origin.x+5, oldFrame.origin.y, oldFrame.size.width, oldFrame.size.height);

    [self.view addSubview:image];

    m++;
    if (m == 1000) {
        [timer1 invalidate];
    }

}

編集:アニメーションは使いたくない。後で衝突検出を使用する必要があり、アニメーションの座標を制御できないためです。

4

2 に答える 2

3

毎回 subView を自己に割り当てて追加しています。ImageView を削除して以前の ImageView を消すのはいつですか。そのため、割り当てる前に画像を削除してみてください。このようにしてみてください

-(void)printOut:(NSTimer*)timer1;
{
[image removeFromSuperview];
image = nil;
image = [[UIImageView alloc] initWithFrame:CGRectMake(50, -50+m, 50, 50)];
[image setImage:[UIImage imageNamed:[NSString stringWithFormat:@"image.png"]]];

CGRect oldFrame = image.frame;
image.frame = CGRectMake(oldFrame.origin.x+5, oldFrame.origin.y, oldFrame.size.width, oldFrame.size.height);

[self.view addSubview:image];

m++;
if (m == 1000) {
    [timer1 invalidate];
}
}
于 2012-08-27T10:12:47.083 に答える
2

これを行う:

image.frame = (top frame here)

[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
//change time duration according to requirement
// animate it to the bottom of view
image.frame = (bottom frame here)
} completion:^(BOOL finished){
// if you want to do something once the animation finishes, put it here
}];
于 2012-08-27T09:34:11.577 に答える