我正在开发一个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的子层或作为scrollview的子层?
我曾尝试使用观察者添加catextlayer以更改节点,但我无法与cashapelayer同时更新。
我希望你能理解,如果没有请告诉我,我会尽力澄清我的问题。
感谢。
答案 0 :(得分: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];
}