将图像添加到触摸位置iOS
我有一个应用程序可以将数组中的点与线连接起来,接下来我要做的是在每条线的端点上添加一个标记,我为此选择了一个红点。但我不知道怎么开始。每次用户点击屏幕时,图像都会出现,因为此时会创建一个新点。谢谢 最简单的解决方案是在视图中添加一个将图像添加到触摸位置iOS,ios,objective-c,uiimage,Ios,Objective C,Uiimage,我有一个应用程序可以将数组中的点与线连接起来,接下来我要做的是在每条线的端点上添加一个标记,我为此选择了一个红点。但我不知道怎么开始。每次用户点击屏幕时,图像都会出现,因为此时会创建一个新点。谢谢 最简单的解决方案是在视图中添加一个UITapGestureRecognizer,使用以下方法作为GR的操作。假设您正在为您的圆点使用图像资源,例如: -(void)screenTapped:(UITapGestureRecognizer*)tap { CGPoint tapLocation =
UITapGestureRecognizer
,使用以下方法作为GR的操作。假设您正在为您的圆点使用图像资源,例如:
-(void)screenTapped:(UITapGestureRecognizer*)tap {
CGPoint tapLocation = [tap locationInView:self.view];
UIImageView *redDotImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redDot"]];
redDotImageView.center = tapLocation;
[self.view addSubview:redDotImageView];
}
最简单的解决方案是使用以下方法将
uitappesturerecognizer
添加到视图中,作为GR的操作。假设您正在为您的圆点使用图像资源,例如:
-(void)screenTapped:(UITapGestureRecognizer*)tap {
CGPoint tapLocation = [tap locationInView:self.view];
UIImageView *redDotImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redDot"]];
redDotImageView.center = tapLocation;
[self.view addSubview:redDotImageView];
}
例如,您可以在视图中添加可识别的手势:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addDotToTapPoint:)];
[view addGestureRecognizer:tapGesture];
然后实现方法adddottospecify:查找抽头点:
- (void)addDotToTapPoint:(UITapGestureRecognizer *)sender {
CGPoint pointOfTouch = [sender locationInView:sender.view];
//... draw circle or something
}
第二种方法。
您可以创建自己的UIView子类,并实现方法touchesBegind:withEvent:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pointOfTouch = [(UITouch *)[touches anyObject] locationInView:self];
//... draw circle or something
}
在这两种情况下,您都可以获得触摸的位置,并可以在该点上绘制一些东西。例如,您可以在视图中添加可识别的手势:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addDotToTapPoint:)];
[view addGestureRecognizer:tapGesture];
然后实现方法adddottospecify:查找抽头点:
- (void)addDotToTapPoint:(UITapGestureRecognizer *)sender {
CGPoint pointOfTouch = [sender locationInView:sender.view];
//... draw circle or something
}
第二种方法。
您可以创建自己的UIView子类,并实现方法touchesBegind:withEvent:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pointOfTouch = [(UITouch *)[touches anyObject] locationInView:self];
//... draw circle or something
}
在这两种情况下,您都将获得触摸的位置,并可以在该点上绘制一些内容谢谢您的快速回答。这似乎是最好的方法,但是由于某些原因,当我点击屏幕时,该方法没有被调用。我在一个UIView的子类中绘制,你知道这是否会导致问题,我不明白为什么。我还将代码放置在touchesbeent方法中,并放置了一个断点,但从未调用该部分。我不得不修改代码的最后一行来添加子视图,因为我在一个子类中,所以它看起来像
[self addSubview:reddoimageview]代码>任何想法,再次感谢你,伙计!尝试添加self.userInteractionEnabled=YES
您是否在以下位置错误地错过了alloc:[UIImageView initWithImage:[uiImageImageName:@“image.png”]代码>好的,我终于可以使用你的代码了,非常感谢。我将代码放在touchesEnded中,现在它可以正确显示图像。我编辑了你的答案以添加你错过的alloc,以防以后有人使用该代码。谢谢你,我可以帮你:)谢谢你注意到丢失的allocThanks谢谢你的快速回答。这似乎是最好的方法,但是由于某些原因,当我点击屏幕时,该方法没有被调用。我在一个UIView的子类中绘制,你知道这是否会导致问题,我不明白为什么。我还将代码放置在touchesbeent方法中,并放置了一个断点,但从未调用该部分。我不得不修改代码的最后一行来添加子视图,因为我在一个子类中,所以它看起来像[self addSubview:reddoimageview]代码>任何想法,再次感谢你,伙计!尝试添加self.userInteractionEnabled=YES
您是否在以下位置错误地错过了alloc:[UIImageView initWithImage:[uiImageImageName:@“image.png”]代码>好的,我终于可以使用你的代码了,非常感谢。我将代码放在touchesEnded中,现在它可以正确显示图像。我编辑了你的答案以添加你错过的alloc,以防以后有人使用该代码。谢谢我能帮忙的小伙子:)谢谢你注意到丢失的alloc