Making images appear… How?

我的未来我决定 提交于 2019-12-31 04:27:11

问题


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


回答1:


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.




回答2:


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;
    }
}



回答3:


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


来源:https://stackoverflow.com/questions/2970293/making-images-appear-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!