0

I am having issues adding an image across a view while moving finger across the screen.

Currently it is adding the image multiple times but squeezing them too close together and not really following my touch.

EDIT:

What I want:

After taking or choosing an image, the user can then select another image from a list. I want the user to touch and move their finger across the view and the selected image will appear where they drag their finger, without overlapping, in each location.

This works:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
     UITouch *touch = [touches anyObject];
     currentTouch = [touch locationInView:self.view];

     CGRect myImageRect = CGRectMake(currentTouch.x, currentTouch.y, 80.0f, 80.0f);
     myImage = [[UIImageView alloc] initWithFrame:myImageRect];
     [myImage setImage:[UIImage imageNamed:@"dot.png"]];
     [self.view addSubview:myImage];
     [myImage release];
}

New Question: How do I add spaces in this so that the image isn't so snug when drawing it on the view?

Luke Irvin
  • 1,179
  • 1
  • 20
  • 39

3 Answers3

0

You might want to explain your question more, what exactly are you trying to achieve! If you dont want the images to overlap than you can try this!

UITouch * touch = [touches anyObject];
touchPoint = [touch locationInView:imageView];
prev_touchPoint = [touch previousLocationInView:imageView];

if (ABS(touchPoint.x - prev_touchPoint.x) > 80 
    || ABS(touchPoint.y - prev_touchPoint.y) > 80) {

   _aImageView = [[UIImageView alloc] initWithImage:aImage];
   _aImageView.multipleTouchEnabled = YES;
   _aImageView.userInteractionEnabled = YES;
  [_aImageView setFrame:CGRectMake(touchPoint.x, touchPoint.y, 80.0, 80.0)];
  [imageView addSubview:_aImageView];
  [_aImageView release];
}
Asif Mujteba
  • 4,596
  • 2
  • 22
  • 38
0

I'm sorry because I'm in company and I can't post too big data(the code). The squeezing is because you have not checked the touch point's distance with last touch point. Check a point whether it's in a view:bool CGRectContainsPoint (CGRect rect,CGPoint point);I mean remember a touch point in touchesBegan:. Update it if the new touches in touchesMomved: is bigger than image's width or left. And put the add image view in a method and call it use - (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg.

sunkehappy
  • 8,970
  • 5
  • 44
  • 65
0

you can also use UISwipeGestureRecognizer as below instead of touchesMoved method,while swiping across the screen.in viewDidload:method,

UISwipeGestureRecognizer *swipeup = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipedone:)];
swipeup.direction = UISwipeGestureRecognizerDirectionUp;
swipeup.numberOfTouchesRequired=1;
[self.view addGestureRecognizer:swipeup];

method definition:

-(IBAction)swipedone:(UISwipeGestureRecognizer*)recognizer
{
    NSLog(@"swiped");
    UIImageView* _aImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    _aImageView.frame = CGRectMake(10, 10, 100, 100);
    _aImageView.multipleTouchEnabled = YES;
    _aImageView.userInteractionEnabled = YES;
    CGPoint point = [recognizer locationInView:recognizer.view];
    [_aImageView setFrame:CGRectMake(point.x, point.y, 80.0, 80.0)];
    [self.view addSubview:_aImageView];
    [_aImageView release];
}

currently i am using this code for swipe up.i think it will works fine.once try it.

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
iSpark
  • 952
  • 7
  • 18
  • This has gotten me closer to what I want! How can I improve this to keep track of my finger and add the image at each new point it is at? – Luke Irvin Nov 09 '12 at 07:37