Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 绘制和更新两个图像之间的直线_Ios_Objective C - Fatal编程技术网

Ios 绘制和更新两个图像之间的直线

Ios 绘制和更新两个图像之间的直线,ios,objective-c,Ios,Objective C,我目前有两个UIImageView,可以通过拖动在屏幕上移动它们: - (void)panWasRecognized:(UIPanGestureRecognizer *)panner { UIView *draggedView = panner.view; CGPoint offset = [panner translationInView:draggedView.superview]; CGPoint center = draggedView.center; d

我目前有两个UIImageView,可以通过拖动在屏幕上移动它们:

- (void)panWasRecognized:(UIPanGestureRecognizer *)panner {
    UIView *draggedView = panner.view;
    CGPoint offset = [panner translationInView:draggedView.superview];
    CGPoint center = draggedView.center;
    draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
    [panner setTranslation:CGPointZero inView:draggedView.superview];
}

- (void)panWasRecognized2:(UIPanGestureRecognizer *)panner2 {
    UIView *draggedView = panner2.view;
    CGPoint offset = [panner2 translationInView:draggedView.superview];
    CGPoint center = draggedView.center;
    draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
    [panner2 setTranslation:CGPointZero inView:draggedView.superview];
}

然而,我不确定下一步该怎么做,即在用户移动图像时不断更新的UIImageView的两个中心之间画一条线。我大致知道如何从一点到另一点画一条简单的线,但有人能建议如何保持这条线的更新和两幅图像的移动同步吗?提前感谢:)

例如,您可以通过子类化
UIView
和重写
drawRect
方法来实现这一点 示例代码,在子类
UIView
u中,您可以执行如下操作

#import "MyView.h"

@implementation MyView

- (id)initWithFrame:(CGRect)frame
 {
    self = [super initWithFrame:frame];
    if (self) {
     // Initialization code
     UIImageView *imgViewOne = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
     imgViewOne.backgroundColor = [UIColor greenColor]; //imageview 1

     UIImageView *imgViewTwo = [[UIImageView alloc]initWithFrame:CGRectMake(200, 0, 100, 100)];
     imgViewTwo.backgroundColor = [UIColor redColor]; //imageview 2

     UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panWasRecognized:)];
     [imgViewOne addGestureRecognizer:panGesture];

     UIPanGestureRecognizer *panGesture2 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panWasRecognized2:)];
     [imgViewTwo addGestureRecognizer:panGesture2];

     imgViewOne.tag = 100;
     imgViewTwo.tag = 200;

     imgViewTwo.userInteractionEnabled = YES;
     imgViewOne.userInteractionEnabled = YES;

     [self addSubview:imgViewOne];
     [self addSubview:imgViewTwo];

  }
  return self;
}


- (void)panWasRecognized:(UIPanGestureRecognizer *)panner
{
   UIView *draggedView = panner.view;
   CGPoint offset = [panner translationInView:draggedView.superview];
   CGPoint center = draggedView.center;
   draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
   [panner setTranslation:CGPointZero inView:draggedView.superview];
   [self setNeedsDisplay];//update the drawing
}

- (void)panWasRecognized2:(UIPanGestureRecognizer *)panner2
{
   UIView *draggedView = panner2.view;
   CGPoint offset = [panner2 translationInView:draggedView.superview];
   CGPoint center = draggedView.center;
   draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
   [panner2 setTranslation:CGPointZero inView:draggedView.superview];
   [self setNeedsDisplay]; //update the drawing

}

// Only override drawRect: if you perform custom drawing.  
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
 {
   CGContextRef context = UIGraphicsGetCurrentContext();
   CGContextClearRect(context, self.bounds);//clear drawing

   UIImageView *imgView1 = (UIImageView *)[self viewWithTag:100];
   UIImageView *imgView2 = (UIImageView *)[self viewWithTag:200];

   CGPoint center1 = imgView1.center;
   CGPoint center2 = imgView2.center;

   CGContextSetFillColorWithColor(context,[UIColor whiteColor].CGColor);//set background white color
   CGContextFillRect(context, self.bounds);


   CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); //set the color of the line
   CGContextSetLineWidth(context, 0.5); //set width of line

   CGContextMoveToPoint(context, center1.x,center1.y); //start at this point
   CGContextAddLineToPoint(context, center2.x,center2.y); //draw to this point

   CGContextClosePath(context);
   CGContextStrokePath(context);
 }


 @end
- (void)panWasRecognized:(UIPanGestureRecognizer *)panner {
     //YOUR OLD CODE

     [self refreshLine];
}

- (void)panWasRecognized2:(UIPanGestureRecognizer *)panner {

     //YOUR OLD CODE

     [self refreshLine];
}
在视图控制器中,只需将

 #import "MyView.h" //import this custom view


 - (void)viewDidLoad
   {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    MyView *view = [[MyView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:view]; //add it to view controller's view
   }

通过对父级
UIView
进行子类化,您可以很容易地做到这一点。只需创建一个类并复制粘贴代码即可

DrawView.h

#import <UIKit/UIKit.h>

@interface DrawView : UIView
-(void)refreshWithPointA:(CGPoint)pointA andPointB:(CGPoint)pointB;
@end
#import "DrawView.h"

@interface DrawView()
@property(nonatomic, strong) UIBezierPath *linePath;
@end

@implementation DrawView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



-(void)refreshWithPointA:(CGPoint)pointA andPointB:(CGPoint)pointB{
    _linePath = [UIBezierPath bezierPath];
    [_linePath setLineWidth:2.0];
    [_linePath moveToPoint:pointA];
    [_linePath addLineToPoint:pointB];
    [self setNeedsDisplay];
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{

    // Drawing code
    [[UIColor blackColor] setStroke];
    [_linePath stroke];
}


@end
现在只需更改添加了
UIImageView
的父UIView类。并将标签
1
2
分配给两个图像视图。并在
pangesture选择器中使用以下方法

-(void)refreshLine{
    CGPoint centreA=[self.view viewWithTag:1].center;
    CGPoint centreB=[self.view viewWithTag:2].center;
    [self.drawView refreshWithPointA:centreA andPointB:centreB];
}
将该方法放入手势识别器的最后一行,如下所示

#import "MyView.h"

@implementation MyView

- (id)initWithFrame:(CGRect)frame
 {
    self = [super initWithFrame:frame];
    if (self) {
     // Initialization code
     UIImageView *imgViewOne = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
     imgViewOne.backgroundColor = [UIColor greenColor]; //imageview 1

     UIImageView *imgViewTwo = [[UIImageView alloc]initWithFrame:CGRectMake(200, 0, 100, 100)];
     imgViewTwo.backgroundColor = [UIColor redColor]; //imageview 2

     UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panWasRecognized:)];
     [imgViewOne addGestureRecognizer:panGesture];

     UIPanGestureRecognizer *panGesture2 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panWasRecognized2:)];
     [imgViewTwo addGestureRecognizer:panGesture2];

     imgViewOne.tag = 100;
     imgViewTwo.tag = 200;

     imgViewTwo.userInteractionEnabled = YES;
     imgViewOne.userInteractionEnabled = YES;

     [self addSubview:imgViewOne];
     [self addSubview:imgViewTwo];

  }
  return self;
}


- (void)panWasRecognized:(UIPanGestureRecognizer *)panner
{
   UIView *draggedView = panner.view;
   CGPoint offset = [panner translationInView:draggedView.superview];
   CGPoint center = draggedView.center;
   draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
   [panner setTranslation:CGPointZero inView:draggedView.superview];
   [self setNeedsDisplay];//update the drawing
}

- (void)panWasRecognized2:(UIPanGestureRecognizer *)panner2
{
   UIView *draggedView = panner2.view;
   CGPoint offset = [panner2 translationInView:draggedView.superview];
   CGPoint center = draggedView.center;
   draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
   [panner2 setTranslation:CGPointZero inView:draggedView.superview];
   [self setNeedsDisplay]; //update the drawing

}

// Only override drawRect: if you perform custom drawing.  
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
 {
   CGContextRef context = UIGraphicsGetCurrentContext();
   CGContextClearRect(context, self.bounds);//clear drawing

   UIImageView *imgView1 = (UIImageView *)[self viewWithTag:100];
   UIImageView *imgView2 = (UIImageView *)[self viewWithTag:200];

   CGPoint center1 = imgView1.center;
   CGPoint center2 = imgView2.center;

   CGContextSetFillColorWithColor(context,[UIColor whiteColor].CGColor);//set background white color
   CGContextFillRect(context, self.bounds);


   CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); //set the color of the line
   CGContextSetLineWidth(context, 0.5); //set width of line

   CGContextMoveToPoint(context, center1.x,center1.y); //start at this point
   CGContextAddLineToPoint(context, center2.x,center2.y); //draw to this point

   CGContextClosePath(context);
   CGContextStrokePath(context);
 }


 @end
- (void)panWasRecognized:(UIPanGestureRecognizer *)panner {
     //YOUR OLD CODE

     [self refreshLine];
}

- (void)panWasRecognized2:(UIPanGestureRecognizer *)panner {

     //YOUR OLD CODE

     [self refreshLine];
}
在添加UIImageView的地方,它会立即绘制线条。为
UIView
创建一个
IBOutlet
,并分配新类

就这样


干杯。

这取决于你在哪里画画;我假设是
UIImageViews
的父视图?在这种情况下,您可以告诉视图使用
setNeedsDisplay
(或
setNeedsDisplayInRect:
)重新绘制,然后在
drawRect:
中实现绘图。是否需要从一个图像中心到另一个图像中心的连接线?是,所以简单地说,一条线从第一个图像的中心延伸到第二个图像,它可以根据任何一个图像的位置移动,非常感谢你的答案。我理解您的大部分代码,但我有几个问题-首先,在将代码放入.h和.m文件后,我得到了initWithFrame和setNeedsDisplay的错误“no visible@interface for…声明选择器…”。另外,我不知道您将标记分配给ImageView和更改父UIView类是什么意思。如果这些问题看起来很简单,我很抱歉,我对编程相当陌生:)事实上,我似乎已经解决了大部分错误。目前唯一不起作用的是drawView,我不确定IBOutlet部分。您需要为已更改类的视图创建一个outlet,以访问drawView的方法。看这里我的意思啊,我明白了,这就是我现在拥有的:。我假设我需要对UIView进行子类化,那么我该怎么做呢?您只需要复制粘贴上面的代码,用于
DrawView
,并更改故事板中视图的类。这里描述的方法是,有没有一种方法可以在已建立的UIViewController上执行此操作,而不是在其上创建新视图(例如,在控制器中使用UIImageView,而不是以编程方式初始化它们)。这就是我现在所拥有的:但我需要它在一个层上使用