Reputation: 1
Xcode Place an Image where the user touches
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=[[event allTouches]anyObject];
CGPoint point= [touch locationInView:touch.view];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:@"BluePin.png"];
[image drawAtPoint:point];
}
Basically touch screen image should appear where touched but nothing appears...
Upvotes: 0
Views: 3807
Reputation: 11841
To add on to the other answer, here's how you could animate it:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Touches began!");
UITouch *touch= [[event allTouches] anyObject];
CGPoint point= [touch locationInView:touch.view];
UIImage *image = [UIImage imageNamed:@"BluePin.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame: CGRectMake(point.x-(imageView.bounds.size.width/2), point.y-(imageView.bounds.size.width/2), imageView.bounds.size.width, imageView.bounds.size.height)];
[self addSubview: imageView];
//self.currentPins += 1;
[UIView animateWithDuration:2.0 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
[imageView setAlpha:0.0];
} completion:^(BOOL finished) {
[imageView removeFromSuperview];
//self.currentPins -= 1;
}];
// for(;self.currentPins > 10; currentPins -= 1){
// [[[self subviews] objectAtIndex:0] removeFromSuperview];
// }
}
The commented out code is a little extra I wrote to limit the amount of pins on the screen to ten at a time, assuming you have a @property called currentPins. Tested and it works, assuming I didn't mess anything up after copying, pasting and commenting out a few lines.
EDIT: Disregard the commented out code. I actually mixed up two versions (one without animation, one with) so it's broken.
Upvotes: 0
Reputation: 3956
You should init a UIImage like this:
UIImage *image = [UIImage imageNamed:@"BluePin"];
You should use a UIImageView to contain a UIImage, you can not put a UIImage into UIView directly.
update
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=[[event allTouches]anyObject];
CGPoint point= [touch locationInView:touch.view];
UIImage *image = [UIImage imageNamed:@"BluePin"];
CGRect rect=CGRectMake(point.x, point.y, image.size.width, image.size.height);
UIImageView *imageView=[[UIImageView alloc]initWithFrame:rect];
[imageView setImage:image];
[self.view addSubview:imageView];
}
Upvotes: 1