2011-06-28 8 views
-1

でUIViewのアニメーションを置き換える私のコードです:はここにもタイマーアニメーション

- (CGPoint)randomPointSquare { 
CGRect frame = [[self view] frame]; 

//CGFloat rand_x = ((float)arc4random()/(float)UINT_MAX) * (float)_window.frame.size.width; 

NSInteger side = arc4random()/(UINT_MAX/4); 
CGFloat offset = 0; 

switch(side) { 
    case 0: /* top */ 
     offset = ((float)arc4random()/(float)UINT_MAX) * (float)frame.size.width; 
     return CGPointMake(offset, -10); 

    case 1: /* bottom */ 
     offset = ((float)arc4random()/(float)UINT_MAX) * (float)frame.size.width; 
     return CGPointMake(offset, frame.size.height-150); 

    case 2: /* left */ 
     offset = ((float)arc4random()/(float)UINT_MAX) * (float)frame.size.height; 
     return CGPointMake(-10, offset); 

    default: 
    case 3: /* right */ 
     offset = ((float)arc4random()/(float)UINT_MAX) * (float)frame.size.height; 
     return CGPointMake(frame.size.width+170, offset); 
} 
} 



-(void)createNewImageView { 
UIImage * image = [UIImage imageNamed:@"abouffer_03.png"]; 
imageView = [[UIImageView alloc] initWithImage:image]; 


// Add it at a random point 
[imageView setCenter:[self randomPointSquare]]; 
[[self view] addSubview:imageView]; 

// Animate it into place 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:8.0f]; 
[imageView setCenter:CGPointMake(240, 160)]; 
[UIView commitAnimations]; 
[imageView release]; 
} 

SOあなたが見ることができるようにUIViewのアニメーションがありますが、今私は変更してタイマーでそれを交換したいです。しかし、私はそれを行う方法を知らない。さて、もしあなたがそれを行う方法を知っていれば、imageViewがランダムな位置に作成されてから、画面の中央に移動することを忘れないようにしてください。ありがとうございます

答えて

0

すべてのコードを書くのではなく、私はあなたにあなたはそれを変更する(学習の最善の方法、私は思う)。

最初に、randomPointSqaureへの呼び出しで位置を作成するときは、作成したポイントをインスタンス変数として保存します。その後

theLocation = [self randomPointSquare]; 

オブジェクトは、あなたがそれが(240、160)との量であることを分割したいポイントからどれだけ離れているかを計算し、この

[imageView setCenter:theLocation]; 

するImageViewのの場所を設定しましたあなたがそれを移動させたい時。たとえば、場所が(0,160)の場合、240ピクセル移動します。 10秒以上にしたい場合、1秒あたり24ピクセルになります。これがうまくいけばべき今 を位置

[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updatePosition) userInfo:nil repeats:YES]; 

を更新するためのメソッドを呼び出して、タイマーを作成し、updatePositionメソッドを作成

- (void)updatePosition { 
    ... 
    // Calculate the new position 
    // delta is divided by 100 as the timer runs every 0.01 second 
    // and delta was calculated previously as being distance per second 
    theLocation = theLocation + (delta/100); 
    ... 
    [imageView setCenter:theLocation]; 

    // Check if the location is at the "collision point" and do whatever if it is 
    if (theLocation.x == 240 && theLocation.y == 160) { 
     // We've hit the "collision point" 
    } 
} 

「デルタ」

としてインスタンス変数でこれを保存アニメーションの代わりにタイマーを使ってどのように行うことができるか、いくつか考えてください。これは、ゲームが同じことをする方法です。ちょうど小さな小さなステップでオブジェクトの位置を更新し、毎回衝突などをチェックするだけです。

+0

私は実際に部品を理解していません: "あなたはそれが(240,160)になる点からオブジェクトがどれだけ離れているかを計算し、それをあなたがそれを移動させたい時間の長さです。なぜなら、画像の位置はどこがランダムな位置なので作成されたのかわからないからです。コードを開発してください。 –

+0

あなたはそれが作成されたランダムな位置を持っています - see theLocation = [self randomPointSquare];(240,160)からの距離を計算するには、単純にxの位置とyの位置を互いに引くことができます。したがって、もしxが120であり、theLocation.yが100なら、距離はxに沿って120、yに沿って60になります。 –

+0

はい私は理解していますが、私が必要とするのは距離と座標を計算する方法を知っていることです。私がサンプルコードを与えると理解しやすくなります:/ –

0

イメージをランダムに数秒後に表示するか、翻訳アニメーションをランダムな位置から中心の位置に移動したいかわかりません。とにかく、私はここの上に両方のオプションを入れている

1)すべての

まず、あなたは下の画像ビュー通話機能作成したら:

[CATransaction begin]; 
[CATransaction setValue:[NSNumber numberWithFloat:2.0f] forKey:kCATransactionAnimationDuration]; 

CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
CGMutablePathRef positionPath = CGPathCreateMutable(); 
CGPathMoveToPoint(positionPath, NULL, randomX , randomY); 
CGPathAddLineToPoint(positionPath, NULL, self.view.center.x , self.view.center.y); 
positionAnimation.path = positionPath; 

[[imageView layer] addAnimation:positionAnimation forKey:@"PositionAnimation"]; 

[CATransaction commit]; 

希望これを

-(void)randomlySetImage{ 
    int x = arc4random() % (int)self.view.frame.size.width; 
    int y = arc4random() % (int)self.view.frame.size.height; 

    imageView.center = CGPointMake(x,y); 

    [NSTimer scheduledTimerWithTimeInterval:1.0f 
            target:self 
            selector:@selector(randomlySetImage) 
            userInfo:nil 
            repeats:YES]; 
} 

2)助けてください

+0

randomXとrandomYはどこから来たのですか?方法はありますか? –

+0

randomXとrandomYを見つけることができますint randomX = arc4random()%(int)self.view.frame.size.width;int randomY = arc4random()%(int)self.view.frame.size.height; – iMOBDEV

+0

はい、私は既にランダムな位置のメソッドを実行していましたが、私はあなたのものではなくランダムな位置を持っていました。私にあなたのものを関連付けることは可能でしょうか? –

関連する問題