English 中文(简体)
Making images appear... How?
原标题:

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

最佳回答

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.

问题回答

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

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




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签