0

我正在开发一个 ios 应用程序,其中包含带有节点和边缘的滚动视图。

起初我使用 calayer 并使用键值观察开始和结束节点。使用以下代码绘制边缘和边缘标签:

- (void)drawInContext:(CGContextRef)ctx
{
CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
CGContextSetLineWidth(ctx, 2);
CGContextMoveToPoint(ctx, _startNode.center.x, _startNode.center.y);
CGContextAddLineToPoint(ctx, _endNode.center.x, _endNode.center.y);

CGContextStrokePath(ctx);
CGContextSetFillColorWithColor(ctx, [[UIColor darkTextColor] CGColor]);

UIGraphicsPushContext(ctx);

CGContextSaveGState(ctx);
CGRect rect = CGRectMake(((_startNode.center.x + _endNode.center.x)/2)-50, ((_startNode.center.y + _endNode.center.y)/2)-15, 100, 30);

CGFloat angle = atan2f(_endNode.center.y - _startNode.center.y, _endNode.center.x - _startNode.center.x) * 180 / PI;
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, CGRectGetMidX(rect), CGRectGetMidY(rect));
transform = CGAffineTransformRotate(transform, angle * M_PI/180.0);
transform = CGAffineTransformTranslate(transform, -CGRectGetMidX(rect), -CGRectGetMidY(rect));
CGContextConcatCTM(ctx, transform);
NSMutableParagraphStyle* p = [NSMutableParagraphStyle new];
p.alignment = NSTextAlignmentCenter;
UIFont *font = [UIFont fontWithName:@"Helvetica" size:12];
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:font forKey:NSFontAttributeName];
[attributes setObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
[attributes setObject:p forKey:NSParagraphStyleAttributeName];
[_edgeName drawInRect:rect withAttributes:attributes];
CGContextRestoreGState(ctx);


UIGraphicsPopContext();
}

这行得通,我能够拖动节点并跟随边缘,但由于内存问题,应用程序崩溃了。然后我被告知我应该使用 cashapelayer。这也工作得更好,但现在我在边缘添加标签时遇到问题。

标签应对齐并相应地旋转到边缘的中心。

以下代码显示了我如何将边缘绘制为 cashapelayer

@implementation EdgeShape

#define PI 3.14159265
- (id)init {
self = [super init];
if (self) {

}
return self;
}

- (void)setStartNode:(UIView *)startNode
{
[startNode addObserver:self forKeyPath:@"center" options:NSKeyValueObservingOptionNew context:nil];
[startNode addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionNew context:nil];
_startNode = startNode;
[self drawPath];
}

- (void)setEndNode:(UIView *)endNode
{
[endNode addObserver:self forKeyPath:@"center" options:NSKeyValueObservingOptionNew context:nil];
[endNode addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionNew context:nil];
_endNode = endNode;
[self drawPath];
}


-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"center"] )
{
    [self drawPath];
}
if([keyPath isEqualToString:@"hidden"]){

    [self removeFromSuperlayer];

}
}

- (void) drawPath{
CGMutablePathRef cgPath = CGPathCreateMutable();
CGPathMoveToPoint(cgPath , NULL, _startNode.center.x, _startNode.center.y);
CGPathAddLineToPoint(cgPath , NULL, _endNode.center.x, _endNode.center.y);
NSLog(@"shape x : %f", _startNode.center.x);
self.lineWidth = 2.0f;
self.strokeColor = [UIColor blackColor].CGColor;
self.fillColor = [UIColor clearColor].CGColor;
self.path = cgPath;
}

@end

我应该如何添加 catextlayer?作为cashapelayer 的子层还是滚动视图的子层?

我试图添加带有观察者的 cattextlayer 以观察节点的变化,但我无法与 cashapelayer 同时更新。

我希望你能理解,如果不明白,请告诉我,我会尽力澄清我的问题。

谢谢。

4

1 回答 1

0

通过在observeValueForKeyPath 中添加以下几行,解决了这个问题。据我了解,这会在更新 catextlayers 位置时删除动画,至少 catextlayer 会毫不拖延地跟随 cashapelayer。

if ([keyPath isEqualToString:@"center"] )
{
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
    _textLayer.position = CGPointMake((_startNode.center.x + _endNode.center.x)/2, (_startNode.center.y + _endNode.center.y)/2);
    CGFloat angle = atan2f(_endNode.center.y - _startNode.center.y, _endNode.center.x - _startNode.center.x) * 180 / PI;
    _textLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation(angle * M_PI/180.0));
    [CATransaction commit];
    [self drawPath];
}
于 2014-06-20T00:47:50.657 回答