1

Quarts2Dを使用して次の機能を実現するための「適切な」方法に興味があります。

ビューがあり、任意の座標で円を追加できるようにしたいです。円を追加するとすぐに、事前定義された速度で拡大するはずです。また、このプロセスを繰り返して、これらの拡大する円の場合は番号を付けたいと思います。

ミサイルコマンドを考える:

黄色い斑点が拡大し続ける

一般に、SDLまたはその他のグラフィックライブラリを使用してC ++でこれを記述している場合は、次のようになります。

「成長する円」を表すクラスがあります。私が作成するすべての「成長する円」へのポインタを保持するベクトル/配列があります。

すべての円の直径はティックごとに大きくなり、renderloopでリストを繰り返し、適切な円をバッファに描画します。

ただし、これは、以前のiPhone開発でビューを一般的に使用していた方法とはうまく合わないようです。

だから私はそれが一種のオープンエンドだと思いますが、このようなもののための「正しい」方法はありますか?

それは(上記のように)ゲームループスタイルでしょうか、それともUIView「円」オブジェクトをサブクラス化してオーバーライドする必要がありますdrawRectか?次に、ビューを作成してメインビューに追加することにより、各円を追加する必要があると思いますか?

最初の調査でも、CAShapeLayerクラスへの参照が見つかりましたが、これはUIViewサブクラス化手法の実装とほとんど同じである可能性があると思います。

4

1 に答える 1

2

これを行う1つの方法があります。次のコードをUIViewControllerサブクラスに追加すると、タッチする場所で成長してから消えていく円が得られます。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self addGrowingCircleAtPoint:[[touches anyObject] locationInView:self.view]];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (flag && [[anim valueForKey:@"name"] isEqual:@"grow"]) {
        // when the grow animation is complete, we fade the layer
        CALayer* lyr = [anim valueForKey:@"layer"];
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        animation.fromValue = [lyr valueForKey:@"opacity"];
        animation.toValue = [NSNumber numberWithFloat:0.f];
        animation.duration = .5f;
        animation.delegate = self;
        lyr.opacity = 0.f;  
        [animation setValue:@"fade" forKey:@"name"];
        [animation setValue:lyr forKey:@"layer"];
        [lyr addAnimation:animation forKey:@"opacity"];
    } else if (flag && [[anim valueForKey:@"name"] isEqual:@"fade"]) {
        // when the fade animation is complete, we remove the layer
        CALayer* lyr = [anim valueForKey:@"layer"];
        [lyr removeFromSuperlayer];
        [lyr release];
    }

}

- (void)addGrowingCircleAtPoint:(CGPoint)point {
    // create a circle path
    CGMutablePathRef circlePath = CGPathCreateMutable();
    CGPathAddArc(circlePath, NULL, 0.f, 0.f, 20.f, 0.f, (float)2.f*M_PI, true);

    // create a shape layer
    CAShapeLayer* lyr = [[CAShapeLayer alloc] init];
    lyr.path = circlePath;

    // don't leak, please
    CGPathRelease(circlePath);
    lyr.delegate = self;

    // set up the attributes of the shape layer and add it to our view's layer
    lyr.fillColor = [[UIColor redColor] CGColor];
    lyr.position = point;
    lyr.anchorPoint = CGPointMake(.5f, .5f);
    [self.view.layer addSublayer:lyr];

    // set up the growing animation
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [lyr valueForKey:@"transform"];
    // this will actually grow the circle into an oval
    CATransform3D t = CATransform3DMakeScale(6.f, 4.f, 1.f);
    animation.toValue = [NSValue valueWithCATransform3D:t];
    animation.duration = 2.f;
    animation.delegate = self;
    lyr.transform = t;  
    [animation setValue:@"grow" forKey:@"name"];
    [animation setValue:lyr forKey:@"layer"];
    [lyr addAnimation:animation forKey:@"transform"];
}
于 2010-07-14T00:29:28.637 に答える