OGeek|极客世界-中国程序员成长平台

标题: ios - 在两个可移动的 uiview 之间画线 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:57
标题: ios - 在两个可移动的 uiview 之间画线

我有一个带有节点(UIViews)的“ ScrollView ”,可以四处拖动。我正在尝试使用“calayer”在选定的 UIViews 之间绘制边缘,但我无法弄清楚当 View 位置发生变化时如何重绘线。

在我的 viewController 类中,我将节点数组中第一个和第二个之间的边添加为:

EdgeLayer *edge = [[EdgeLayer alloc]init];
edge.delegate = self;
edge.strokeColor = [UIColor colorWithWhite:0.25 alpha:1.0];
edge.strokeWidth = 0.5;
edge.startNode = [nodes objectAtIndex:0];
edge.endNode = [nodes objectAtIndex:1];
edge.frame = scrollView.bounds;
[scrollView.layer addSublayer:edge];
[edge setNeedsDisplay];

EdgeLayer.h:

#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>

@interface EdgeLayer : CALayer
@property (nonatomic, strong) UIColor *fillColor;
@property (nonatomic) CGFloat strokeWidth;
@property (nonatomic, strong) UIColor *strokeColor;
@property (nonatomic) UIView *startNode;
@property (nonatomic) UIView *endNode;
@end

EdgeLayer.m:

#import "EdgeLayer.h"
#import "NodeView.h"

@implementation EdgeLayer

@synthesize fillColor, strokeColor, strokeWidth;
- (id)init {
    self = [super init];
    if (self) {
    self.fillColor = [UIColor grayColor];
    self.strokeColor = [UIColor blackColor];
    self.strokeWidth = 1.0;
    [self setNeedsDisplay];
}

return self;
}

- (void) setStartNodeNodeView*)startNode
{
     _startNode = startNode;
     [self setNeedsDisplay];
}

- (void) setEndNodeNodeView*)endNode
{
    _endNode = endNode;
    [endNode addObserver:self forKeyPath"endNode" options:NSKeyValueObservingOptionNew context:nil];
    [self setNeedsDisplay];
 }

-(id)initWithLayerid)layer
{
    self = [super initWithLayer:layer];
    return self;
}

-(void) observeValueForKeyPathNSString *)keyPath ofObjectid)object change:    (NSDictionary *)change contextvoid *)context
{
    if ([keyPath isEqualToString"endNode"] )
    {
    // process here
    }
    NSLog(@"View changed its geometry");
}

- (void)drawInContextCGContextRef)ctx
{
    NSLog(@"DRAW");

    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);
}
@end

这从第一个和第二个节点的中心位置添加一条线。我试图为端节点添加一个观察者,但我认为我做得不对。我无法触发 observeValueForKeyPath 方法。我的猜测是我在错误的地方添加了观察者。



Best Answer-推荐答案


问题不在于您添加观察者的位置,而在于您要观察的内容——您使用 endNode 作为 endNode 上的 keyPath,这将不起作用(无论如何,当您拖动时,endNode 不会改变)。您要观察的 endNode 的属性是中心。

您只需将您正在观察的 keyPath 更改为“中心”,并更改您的 observeValueForKeyPathfObject:change:context: 的实现:就像这样,

-(void) observeValueForKeyPathNSString *)keyPath ofObjectid)object changeNSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString"center"] ) [self setNeedsDisplay];
}

我通过将平移手势识别器添加到其中一个 nodeView 来对此进行测试,并且在我拖动时,连接两个节点的线会相应更新。

关于ios - 在两个可移动的 uiview 之间画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23748451/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4