我试图了解如何使用Core Graphics在我的CALayer上绘图。到目前为止,我想我知道我应该将控制器对象设置为图层的委托,然后在控制器中调用我的CG命令。
我的问题是我真的无法弄清楚如何使用我当前的代码执行此操作。
理想情况下,我需要有许多不同的图层,所有图层都有不同的CG图形,因此我可以使用CAAnimation为它们设置动画。截至目前,我有一些带动画的工作层,但没有CG内容。我一直在尝试将所有这些图层设置为同一个委托,然后使用if语句调用每个图层并绘制它们。这甚至可能吗?或者我想有更简单的方法吗?我对这些概念还很陌生,所以任何帮助都会非常感激。
以下是我的相关代码:
在我的TapView.h中
@interface TapView : UIView
{
CALayer *circleGlowLayer;
}
-(void)glowCircleTap;
@property (nonatomic) float opacity;
@end
在我的TapView.m
中@implementation TapView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self glowCircleTap];
}
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
circleGlowLayer = [[CALayer alloc]init];
[circleGlowLayer setBounds:CGRectMake(0.0,0.0,470.0,470.0)];
[circleGlowLayer setPosition:self.center];
UIColor *transparent = [UIColor colorWithRed:0.53 green:0.85 blue:0.89 alpha:1.0];
CGColorRef cgTransparent = [transparent CGColor];
[circleGlowLayer setBackgroundColor:cgTransparent];
[[self layer] addSublayer:circleGlowLayer];
[circleGlowLayer setOpacity:0.0];
}
return self;
}
-(void)glowCircleTap
{
CAKeyframeAnimation *glowCircleFader = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
[glowCircleFader setValues:[NSArray arrayWithObjects:
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:0.0], nil]];
[glowCircleFader setDuration:0.5];
[circleGlowLayer addAnimation:glowCircleFader forKey:@"fadeAnimation"];
}
@end
到目前为止,当我点击屏幕时,此代码会绘制一个正方形,然后将其淡出半秒钟。我希望能够在这一层上绘制。
为了尝试创建一个委托,我创建了一个TapViewDelegate类,然后在我的-init中使用TapView我会[circleGlowLayer setDelegate:TapViewDelegate];
然后这就是我有点迷失的地方。另外,如果这很接近,我可以将所有图层设置为这个代理吗?我认为问题在于代表的想法仍然在我的脑海中。在此先感谢您的帮助!