다른 것들 중에서도 노드와 가장자리가있는 scrollview가 포함 된 ios 응용 프로그램에서 작업하고 있습니다.하위 레이어로 cashapelayer 및 catextlayer
처음에는 시작 노드와 끝 노드를 관찰하는 키 값으로 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();
}
을, 나는 주변 노드를 드래그 할 수 있었고, 가장자리가 뒤에 있지만 응용 프로그램으로 인해 메모리 문제로 추락했다. 그렇다면 캐패 패 레이어를 사용해야한다고 들었습니다. 이것은 또한 더 잘 작동하지만, 이제 가장자리에 레이블을 추가하는 데 문제가 있습니다.
레이블은 모서리의 중심에 맞게 정렬 및 회전해야합니다.
다음 코드는 내가
@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로 가장자리를 그리는 방법을 보여줍니다? cashapelayer의 하위 계층 또는 scrollview의 하위 계층으로?
노드 변경 사항에 대한 옵저버와 함께 catextlayer를 추가하려했지만 cashapelayer와 동시에 업데이트 할 수 없습니다.
나는 당신이 그것을 이해하기를 바란다면, 제발 내 문제를 명확히하려고 노력할 것입니다.
감사합니다.