让图像出现...如何?
有人可以告诉我如何在用户点击屏幕时显示图像并使其出现在点击的位置吗? 提前致谢, 泰特美术馆
Could someone please tell me how to make an image appear when the user taps the screen and make it appear at the position of the tap.
Thanks in advance,
Tate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UIView
是UIResponder
,它具有以下可能有帮助的方法:-touchesBegan:withEvent:
,-touchesEnded:withEvent:
、-touchesCancelled:withEvent:
和-touchesMoved:withEvent:
。其中每个参数的第一个参数是
UITouch
对象的NSSet
。UITouch
有一个-locationInView:
实例方法,应生成视图中点击的位置。UIView
is a subclass ofUIResponder
, which has the following methods that might help:-touchesBegan:withEvent:
,-touchesEnded:withEvent:
,-touchesCancelled:withEvent:
and-touchesMoved:withEvent:
.The first parameter of each of those is an
NSSet
ofUITouch
objects.UITouch
has a-locationInView:
instance method which should yield the position of the tap in your view.您可以创建一个初始星星,并在每次触摸视图时移动它。
我不确定你的最终结果会是什么样子。
笔记:
此代码将为您提供 1 颗星,轻按即可移动
这是我的代码:-
You could create an initial star and just move it every time view is touched.
I'm not sure what you're end result will look like.
Note:
This code will give you 1 star that moves with a tap
Here is my code:-
这个问题似乎暗示您希望用户能够点击屏幕上的任何位置并在他们点击的地方绘制图像?与点击指定位置并使图像出现在那里相反?
如果是这样,您可能必须使用自定义视图。在这种情况下,您需要执行如下操作:
UIView
的子类。touchesBegan
方法。调用[[touches anyObject] locationInView:self]
(其中touches
是该方法的第一个参数,是UITouch< 的
NSSet
/code>objects)来获取触摸的位置,并记录下来。touchesEnded
方法。使用与步骤 2 相同的方法确定触摸结束的位置。[self setNeedsDisplay]
来重新绘制自定义视图。drawRect
方法。在这里,如果位置已在步骤 4 中设置,您可以使用UIImage
方法drawAtPoint
在所选位置绘制图像。有关更多详细信息,此链接可能值得一看。希望有帮助!
编辑:我注意到您之前已经问过基本相同的问题。如果您对那里给出的答案不满意,通常认为“推翻”旧问题更好,也许通过编辑它来要求进一步澄清,而不是创建一个新问题。
编辑:根据要求,下面是一些非常简短的示例代码。这可能不是最好的代码,而且我还没有测试过它,所以它可能有点不确定。澄清一下,
THRESHOLD
允许用户在点击时稍微移动手指(最多 3 像素),因为如果手指不移动一点,则很难点击。MyView.h
MyView.m
It seems implied from the question that you want the user to be able to tap anywhere on the screen and have an image drawn where they tap? As opposed to tapping in a designated place and having the image appear there?
If so, you're probably going to have to go with a custom view. In that case, you'd do something like the following:
UIView
.touchesBegan
method. Call[[touches anyObject] locationInView:self]
(wheretouches
is the first argument to the method, anNSSet
ofUITouch
objects) to get the location of the touch, and record it.touchesEnded
method. Determine the location touches ended at using the same method as in step 2.[self setNeedsDisplay]
to cause the custom view to be redrawn.drawRect
method. Here, if the location has been set in step 4, you can use theUIImage
methoddrawAtPoint
to draw your image at the selected location.For further details, this link might be worth a look. Hope that helps!
EDIT: I've notice you've asked essentially the same question before. If you're not happy with the answers given there, it's generally considered better to "bump" the old one, perhaps by editing it to ask for further clarification, rather than create a new question.
EDIT: As requested, some very brief sample code follows. This is probably not the best code around, and I haven't tested it, so it may be a little iffy. Just for clarification, the
THRESHOLD
allows the user to move their finger a little while tapping (up to 3px), because it's very difficult to tap without moving your finger a little.MyView.h
MyView.m