将图像放在用户触摸的位置

时间:2012-09-10 20:27:59

标签: ios uiimage uitouch

触摸屏图像基本上应该出现在触摸屏上但不起作用的地方。

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    UIImage *image = [[UIImage alloc]initWithContentsOfFile:@"BluePin.png"];
    [image drawAtPoint:CGPointMake(location.x, location.y)];
}

1 个答案:

答案 0 :(得分:0)

您无法调用UIImage,因为它只保存图像数据,您必须使用UIImageView然后才能工作。

所以请改用:

-(void) touchBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
UImageView *imageView = [[UIImageView alloc] init];
UIImage *image = [[UIImage imageNamed:@"BluePin.png"];
imageView.image = image;
[imageView drawAtPoint:CGPointMake(location.x, location.y)];
}

希望这会有所帮助:D