让图像出现...如何?

发布于 2024-09-04 12:53:00 字数 53 浏览 8 评论 0原文

有人可以告诉我如何在用户点击屏幕时显示图像并使其出现在点击的位置吗? 提前致谢, 泰特美术馆

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有你我更好 2024-09-11 12:53:00

UIViewUIResponder,它具有以下可能有帮助的方法:-touchesBegan:withEvent:, -touchesEnded:withEvent:-touchesCancelled:withEvent:-touchesMoved:withEvent:

其中每个参数的第一个参数是 UITouch 对象的 NSSetUITouch 有一个 -locationInView: 实例方法,应生成视图中点击的位置。

UIView is a subclass of UIResponder, 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 of UITouch objects. UITouch has a -locationInView: instance method which should yield the position of the tap in your view.

旧时浪漫 2024-09-11 12:53:00

您可以创建一个初始星星,并在每次触摸视图时移动它。
我不确定你的最终结果会是什么样子。

笔记:
此代码将为您提供 1 颗星,轻按即可移动
这是我的代码:-

(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];
    switch ([allTouches count]) {
        case 1:
        {
            UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
            CGPoint point = [touch locationInView:myView];
            myStar.center = point;  
            break;
        }
        default:
            break;
    }
}

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:-

(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];
    switch ([allTouches count]) {
        case 1:
        {
            UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
            CGPoint point = [touch locationInView:myView];
            myStar.center = point;  
            break;
        }
        default:
            break;
    }
}
狼性发作 2024-09-11 12:53:00

这个问题似乎暗示您希望用户能够点击屏幕上的任何位置并在他们点击的地方绘制图像?与点击指定位置并使图像出现在那里相反?

如果是这样,您可能必须使用自定义视图。在这种情况下,您需要执行如下操作:

  1. 创建 UIView 的子类。
  2. 重写 touchesBegan 方法。调用 [[touches anyObject] locationInView:self] (其中 touches 是该方法的第一个参数,是 UITouch< 的 NSSet /code>objects)来获取触摸的位置,并记录下来。
  3. 重写 touchesEnded 方法。使用与步骤 2 相同的方法确定触摸结束的位置。
  4. 如果第二个位置靠近第一个位置,则您需要将图像放置在该位置。记录该位置并调用 [self setNeedsDisplay] 来重新绘制自定义视图。
  5. 重写drawRect方法。在这里,如果位置已在步骤 4 中设置,您可以使用 UIImage 方法 drawAtPoint 在所选位置绘制图像。

有关更多详细信息,此链接可能值得一看。希望有帮助!

编辑:我注意到您之前已经问过基本相同的问题。如果您对那里给出的答案不满意,通常认为“推翻”旧问题更好,也许通过编辑它来要求进一步澄清,而不是创建一个新问题。

编辑:根据要求,下面是一些非常简短的示例代码。这可能不是最好的代码,而且我还没有测试过它,所以它可能有点不确定。澄清一下,THRESHOLD 允许用户在点击时稍微移动手指(最多 3 像素),因为如果手指不移动一点,则很难点击。

MyView.h

#define THRESHOLD 3*3

@interface MyView : UIView
{
    CGPoint touchPoint;
    CGPoint drawPoint;
    UIImage theImage;
}

@end

MyView.m

@implementation MyView

- (id) initWithFrame:(CGRect) newFrame
{
    if (self = [super initWithFrame:newFrame])
    {
        touchPoint = CGPointZero;
        drawPoint = CGPointMake(-1, -1);
        theImage = [[UIImage imageNamed:@"myImage.png"] retain];
    }

    return self;
}

- (void) dealloc
{
    [theImage release];
    [super dealloc];
}

- (void) drawRect:(CGRect) rect
{
    if (drawPoint.x > -1 && drawPoint.y > -1)
        [theImage drawAtPoint:drawPoint];
}

- (void) touchesBegan:(NSSet*) touches withEvent:(UIEvent*) event
{
    touchPoint = [[touches anyObject] locationInView:self];
}

- (void) touchesEnded:(NSSet*) touches withEvent:(UIEvent*) event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    CGFloat dx = point.x - touchPoint.x, dy = point.y - touchPoint.y;

    if (dx + dy < THRESHOLD)
    {
        drawPoint = point;
        [self setNeedsDisplay];
    }
}

@end

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:

  1. Create a subclass of UIView.
  2. Override the touchesBegan method. Call [[touches anyObject] locationInView:self] (where touches is the first argument to the method, an NSSet of UITouch objects) to get the location of the touch, and record it.
  3. Override the touchesEnded method. Determine the location touches ended at using the same method as in step 2.
  4. If the second location is near the first, you'll want to place your image at that location. Record that location and call [self setNeedsDisplay] to cause the custom view to be redrawn.
  5. Override the drawRect method. Here, if the location has been set in step 4, you can use the UIImage method drawAtPoint 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

#define THRESHOLD 3*3

@interface MyView : UIView
{
    CGPoint touchPoint;
    CGPoint drawPoint;
    UIImage theImage;
}

@end

MyView.m

@implementation MyView

- (id) initWithFrame:(CGRect) newFrame
{
    if (self = [super initWithFrame:newFrame])
    {
        touchPoint = CGPointZero;
        drawPoint = CGPointMake(-1, -1);
        theImage = [[UIImage imageNamed:@"myImage.png"] retain];
    }

    return self;
}

- (void) dealloc
{
    [theImage release];
    [super dealloc];
}

- (void) drawRect:(CGRect) rect
{
    if (drawPoint.x > -1 && drawPoint.y > -1)
        [theImage drawAtPoint:drawPoint];
}

- (void) touchesBegan:(NSSet*) touches withEvent:(UIEvent*) event
{
    touchPoint = [[touches anyObject] locationInView:self];
}

- (void) touchesEnded:(NSSet*) touches withEvent:(UIEvent*) event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    CGFloat dx = point.x - touchPoint.x, dy = point.y - touchPoint.y;

    if (dx + dy < THRESHOLD)
    {
        drawPoint = point;
        [self setNeedsDisplay];
    }
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文