Add a uiimageview inside a uiview when touching the uivew - ios

What I want to do is when I touch the UIView, I want to add an image at the touched location.
This is my code I have for the moment.
(I have tried to replace x and y with touchLocation in the CGRectMake, but that doesn't work).
Edited
After clean and restart xcode it work.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
if ([touch view] == GrassView) {
NSLog(#"Hit grass");
UIImageView *imageViewFlower =[[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)];
imageViewFlower.center = touchLocation;
imageViewFlower.image= [UIImage imageNamed:#"whiteflower.png"];
[GrassView addSubview:imageViewFlower];
}
}
Thanks for any help!

try to change the imageViewFlower insertion code in the if branch with the following code:
UIImageView *imageViewFlower =[[UIImageView alloc] initWithFrame:CGRectMake(touchLocation.x-25,touchLocation.y-25,50,50)];
imageViewFlower.image = [UIImage imageNamed:#"whiteflower.png"];
[GrassView addSubview:imageViewFlower];

After clean and restart xcode the code works fine.

Related

TouchesMoved only moves a little

I am trying to allow the user to drag a label around the screen, but in the simulator it only moves a little each time I touch somewhere on the screen. It will jump to the location and then drag slightly, but then it will stop dragging and I have to touch a different location to get it to move again. Here is my code in my .m file.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *Drag = [[event allTouches] anyObject];
firstInitial.center = [Drag locationInView: self.view];
}
My ultimate goal is to be able to drag three different labels on the screen, but I'm just trying to tackle this problem first. I would greatly appreciate any help!
Thanks.
Try using a UIGestureRecognizer instead of -touchesMoved:withEvent:. And implementing something similar to the following code.
//Inside viewDidLoad
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(dragonMoved:)];
panGesture.minimumNumberOfTouches = 1;
[self addGestureRecognizer:panGesture];
//**********
- (void)dragonMoved:(UIPanGestureRecognizer *)gesture{
CGPoint touchLocation = [gesture locationInView:self];
static UIView *currentDragObject;
if(UIGestureRecognizerStateBegan == gesture.state){
for(DragObect *dragView in self.dragObjects){
if(CGRectContainsPoint(dragView.frame, touchLocation)){
currentDragObject = dragView;
break;
}
}
}else if(UIGestureRecognizerStateChanged == gesture.state){
currentDragObject.center = touchLocation;
}else if (UIGestureRecognizerStateEnded == gesture.state){
currentDragObject = nil;
}
}

I'm having problems moving an programatically added UIIMAGEVIEW

So , i am having a problem moving some images with touchesBegan and touchesMoved here is my code for the p1 UIImageView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: touch.view];
NSLog(#"incepe");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (touch.view == self.p1) {
CGPoint location = [touch locationInView: self.view];
self.p1.center = location;
NSLog(#"nmk");
}
}
and the way i add the pictures is this
-(void)img{
if (!self.p1) {
self.p1 =[[UIImageView alloc] initWithFrame:CGRectMake(14,20,70,50)];
}
[self.view addSubview:self.p1];
}
the touchesmoved event is not triggred when i press the p1 UIImageview
try setting p1.userInteractionEnabled = YES
Have a I look at this:
-(void)viewDidAppear:(BOOL)animated{
UIPanGestureRecognizer *pgr1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(image1Moved:)];
[yourImageView addGestureRecognizer:pgr1];
}
-(void)image1Moved:(UIPanGestureRecognizer *)pgr{
NSLog(#"Came here");
[yourImageView setCenter:[pgr locationInView:self.view]];
}
P.S yourImageView should have userInteractionEnabled to YES

How to make create image when screen is touched then be able to move it

I am trying to create a dot on the screen when it is touched and then be able to move that dot around and then ending the touch which will bring up an alert view. For the first touch and movement i have this code so far...
{
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(359,487,50,50)];
dot.image=[UIImage imageNamed:#"hockey-puck-stress-toy-superextralarge-175648.png"];
[self.view addSubview:dot];
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
dot.center = location;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesBegan:touches withEvent:event];
}
The problem is that this creates a new object when moving instead of just when I first touch it. I understand why it does this, but i don't know any way to fix it. Could someone please help? Thanks!
Create a property in your .h file:
#property (nonatomic) UIImageView *dot;
And then just create the view if it doesn't exist and then move the property:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (!self.dot) {
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(359,487,50,50)];
dot.image=[UIImage imageNamed:#"hockey-puck-stress-toy-superextralarge-175648.png"];
[self.view addSubview:dot];
}
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
self.dot.center = location;
}

TouchesMoved with UITouch

I'm trying to create a simple application where you can move your UIImageView by touching him and dragging him around.
my UIImageView is called imv
-(void ) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch * touch = [touches anyObject];
if([touch view] == self.imv){
CGPoint location = [touch locationInView:self.view];
self.imv.center = location;
}
}
-(void ) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch * touch = [touches anyObject];
if([touch view] == self.imv){
CGPoint location = [touch locationInView:self.view];
self.imv.center = location;
}
}
i'am trying to solve this like whole day and i don't know what is wrong. If i disable if statement it's working else not. What can i do?
Thanks for the answers
Unless you've subclassed UIImageView (unlikely), your view is receiving the touch events.
These days it's simpler & more usual to use a UIGestureRecognizer for this kind of thing, in this case a UIPanGestureRecognizer.
e.g.
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(dragImageView:)];
[self.imv addGestureRecognizer:pan];
- (void)dragImageView:(UIPanGestureRecognizer *)dragImageView {
if(UIGestureRecognizerStateBegan == state) {
originalCenter = self.imv.center; // add CGPoint originalCenter; member
} else if(UIGestureRecognizerStateChanged == state) {
CGPoint translate = [pan translationInView:self.imv.superview];
self.imv.center = CGPointMake(originalCenter.x + translate.x, originalCenter.y + translate.y);
}
}
From a bit of experimenting, it seems that the [touch view] is returning the main UIView and not your subview, hence the problem with the if statement not working (I added the UIImageView in a storyboard xib). EDIT- it's because UIImageViews don't handle touch events by default - see here . When adding a regular UIView in the viewDidLoad seems to work as you would expect.
Anyway, this adapted version of your code works for me
-(void)moveImageForTouches:(NSSet*)touches
{
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(self.imv.frame, location))
{
self.imv.center = location;
}
}
-(void ) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self moveImageForTouches:touches];
}
-(void ) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self moveImageForTouches:touches];
}

Load image when screen tapped [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm attempting to make an app where you can tape to 'fire' and where you tap it will make an image appear aka a bullet hole. I realise I will have to make a UIImageView appear but I wouldn't have any idea how.
Any help is appreciated
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
NSLog(#"X cord: %f",location.x);
NSLog(#"Y cord: %f",location.y);
}
On above code you will get the current touch location.So place your image on that location.
Hope it will help you.
//
// ViewController.m
//
#import "ViewController.h"
#implementation ViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: self.view];
UIImage *bulletImg = [UIImage imageNamed: #"bullet.png"];
UIImageView *bulletView = [[UIImageView alloc] initWithImage: bulletImg];
int bulletWidth = bulletImg.size.width;
int bulletHeight = bulletImg.size.height;
bulletView.frame = CGRectMake(
location.x - (bulletWidth / 2), // centered x position
location.y - (bulletHeight / 2), // centered y position
bulletWidth, // width
bulletHeight // height
);
[self.view addSubview: bulletView];
}
#end
Make sure your view userInteractionEnabled to YES
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
[self addBulletHoleImageAtLocation:location];
}
-(void) addBulletHoleImageAtLocation:(CGPoint)location {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(location.x, location.y, yourImageWidth, yourImageHeight)];
imgView.image = [UIImage imageNamed:#"bulletHole.png"];
imgView.contentMode = UIViewContentModeCenter;
[self.view addSubview:imgView];
[imgView release]; //if NON ARC
}
You can set different contentMode like, UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, or UIViewContentModeScaleAspectFillas per your need.
Apply a UITapGestureRecognizer to your view controller's view, and set it to perform an action/method where you show the image.

Resources