简体   繁体   English

用圆形图像层标记球-在iPhone上触摸缩放或移动

[英]Mark a ball with a circular image layer- in iphone touch zoom or move

I have two images a tennis ball and a red marker image. 我有两个图像,一个网球和一个红色标记图像。 I need to make the tennis ball as background and make the red marker as foreground like a layer over the tennis ball and I need to adjust the red marker according to the shape of tennis ball. 我需要将网球作为背景,并将红色标记作为前景,就像在网球上的图层一样,并且需要根据网球的形状调整红色标记。 I have to achieve this with pinch and touch actions. 我必须通过捏和触摸动作来实现这一目标。

The below are the two images. 以下是两个图像。

Image 1: 图片1

在此处输入图片说明

Image 2 ( layer ): 图片2(图层):

在此处输入图片说明

Final Image By adjusting the circle: 最终图像通过调整圆圈:

在此处输入图片说明

Any starters on this would be good!! 任何初学者都很好!!

There are two ways to do this (handling user touches): 有两种方法(处理用户触摸):

  1. touchesBegan:withEvent: , touchesMoved:withEvent: and touchesEnded:withEvent: methods. touchesBegan:withEvent:touchesMoved:withEvent:touchesEnded:withEvent:方法。 Reference is here: UIResponder - Responding to Touch Events . 参考在这里: UIResponder-响应触摸事件

  2. Gesture recognizers (use standart gesture recognizers or create your own custom one). 手势识别器(使用标准手势识别器或创建自己的自定义手势识别器)。 Reference is here: Gesture Recognizers . 参考在这里: 手势识别器 Good tutorial is here . 好的教程在这里

If you need just to move red circle, but not adjusting it size - to use touches... methods will be good for you. 如果您只需要移动红色圆圈,而不需要调整它的大小-使用touches...方法将很适合您。

But if you want to adjust size of the circle by using pinch gesture - you might want to create custom gesture recognizer (and implement same touches... methods there) to handle touches and moves; 但是,如果您想使用捏手势来调整圆的大小,则可能需要创建自定义手势识别器(并在其中实现相同的touches...方法)来处理触摸和移动。 and use standart UIPinchGestureRecognizer to handle pinch gestures. 并使用标准的UIPinchGestureRecognizer处理捏手势。

You need to create custom gesture recognizer, because standart UITapGestureRecognizer and UIPanGestureRecognizer have some issues, as it mentioned here . 您需要创建自定义的手势识别,因为非标准UITapGestureRecognizerUIPanGestureRecognizer有一些问题,因为它提到这里

UPD: UPD:

For example, you have UIViewController . 例如,您有UIViewController You're adding UIImageView to viewController's view . 您正在将UIImageView添加到viewController的view And you're creating UIView with clear background and drawing red circle inside of it or you're adding another UIImageView with red circle (if you have an image of it). 您正在创建具有清晰背景的UIView并在其中绘制红色圆圈,或者添加另一个带有红色圆圈的UIImageView (如果有图像的话)。 Let's call it redCircleView . 我们称之为redCircleView

You're making redCircleView transparent ( redCircleView.alpha = 0 ) or you can initialize it when user touches the screen. 您可以使redCircleView透明( redCircleView.alpha = 0 ),也可以在用户触摸屏幕时对其进行初始化。 I think second option is better, but I'm not sure. 我认为第二种选择更好,但是我不确定。

In your UIViewController you're implementing touchesBegan:withEvent: method, where you're either set redCircleView.alpha = 1 or initialize this redCircleView . 在您的UIViewController您正在实现touchesBegan:withEvent:方法,您可以在其中设置redCircleView.alpha = 1或初始化此redCircleView

In touchesMoved:withEvent: implementation you can get location of touch in any view using UITouch:locationInView: touchesMoved:withEvent:实现中,您可以使用UITouch:locationInView:在任何视图中获取触摸UITouch:locationInView:

 (void) touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
      UITouch *touch = [touches anyObject];
      CGPoint point = [touch locationInView:self.view];
      .
      .
      .
 }

Then you can either change redCircleView.center (then center of red circle will follow finger on the screen) or you can store location of previous point, compare it to current location on then move redCircleView . 然后,您可以更改redCircleView.center (然后红色圆圈的中心将在屏幕上跟随手指),也可以存储上一点的位置,将其与当前位置进行比较,然后移动redCircleView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM