iPhone Quartz2D渲染扩展圆
我很好奇使用Quarts2D实现以下功能的“正确”方法: 我想要一个视图,并且能够在任意坐标添加一个圆。一旦我添加圆,它应该以预定义的速率扩展;我还想重复这个过程,如果这些不断扩大的圆圈有一个数字 想想导弹司令部: 一般来说,如果我用SDL或其他图形库在C++中写这个,我会: 让一个类代表一个“成长圈” 有一个向量/数组来保存指向我创建的所有“成长圈”的指针 所有圆的直径都会随着刻度的增加而增加,在我的renderloop中,我会迭代列表并在缓冲区中绘制适当的圆 然而,这似乎与我在以前的iPhone开发中通常使用视图的方式不太相符 所以我想这是一种开放式的,但是对于这样的事情有一种“正确”的方法吗 它是在游戏循环样式中(如上所述),还是应该为“圆圈”对象子类化iPhone Quartz2D渲染扩展圆,iphone,graphics,quartz-2d,Iphone,Graphics,Quartz 2d,我很好奇使用Quarts2D实现以下功能的“正确”方法: 我想要一个视图,并且能够在任意坐标添加一个圆。一旦我添加圆,它应该以预定义的速率扩展;我还想重复这个过程,如果这些不断扩大的圆圈有一个数字 想想导弹司令部: 一般来说,如果我用SDL或其他图形库在C++中写这个,我会: 让一个类代表一个“成长圈” 有一个向量/数组来保存指向我创建的所有“成长圈”的指针 所有圆的直径都会随着刻度的增加而增加,在我的renderloop中,我会迭代列表并在缓冲区中绘制适当的圆 然而,这似乎与我在以前的iPh
UIView
,并覆盖drawRect
?我想我必须通过创建一个视图并将其添加到我的主视图来添加每个圆
最初的调查还让我看到了对该类的引用,不过我猜这可能与实现UIView子类化技术差不多。将以下代码添加到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"];
}
很好的解决方案,我喜欢这样的效果:-)如果我想查询某个特定圆的状态,这不会太有用吗?虽然我想我可以保留对所有现有层的引用,并使用这些来查询层的尺寸(例如,我是否应该处理碰撞)是的,有很多方法可以给猫蒙皮。看看CALayer
s
hitTest:`方法。默认情况下,它使用层的矩形边界,但您可以将CAShapeLayer子类化为扩展圆,并提供一个在透明度/几何体方面非常灵活的hitTest实现。