How can you detect if an image is touched in Xcode? I have looked at Apple documentation and it is really confusing... I saw:
if (CGRectContainsPoint([imageView frame], location))
but my image still won't move. I tried using touchesBegan + touchesMoved, and set the image's userInteractionIsEnabled to YES, but it still won't detect it :(
EDIT: Thank you all so much for your great suggestions! In the end, I really wanted to make it as simple as possible, and I knew that my code should work, so I kept fiddling with it, and after a good night's sleep, I realized it was a fairly simple solution:
In my touchesMoved:
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
CGRect shapeRect = [imageView frame];
CGRect dropSquareRect = [dropSquare frame];
CGPoint touchLocation = CGPointMake(location.x, location.y);
if (CGRectContainsPoint(shapeRect, touchLocation))
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
[imageView setCenter:touchLocation];
[UIView commitAnimations];
}
if (CGRectIntersectsRect(shapeRect, dropSquareRect))
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.imageView.alpha = 0;
self.imageView.center = CGPointMake(dropSquare.center.x, dropSquare.center.y);
self.imageView.transform = CGAffineTransformMakeScale(0.8, 0.8);
[UIView commitAnimations];
you could consider using UITapGestureRecognizer
with UIImageView
to detect the touches.
And also not forget to set userInteractionIsEnabled
to YES
.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
myImageView.userInteractionIsEnabled = YES;
[myImageView addGestureRecognizer:tap];
[tap release];
Implement imageTapped:
method.
- (void )imageTapped:(UITapGestureRecognizer *) gestureRecognizer
{
}
You could add a UIPanGesureRecognizer to the UIImageView that's holding the UIImage. This will allow you to detect when the user is panning on the image and move the images translation accordingly. The reference to the documentation is here.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPanGestureRecognizer_Class/Reference/Reference.html
It's nice using the gesture recognizers since it keeps the consistency with the rest of the OS as far as panning goes.
Hope this helps.
You need the touchesBegan method e.g.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint myPoint = [touch locationInView:self];
if (CGRectContainsPoint([imageView frame], location)){
// code here
}
}
I just read that you tried it, although I am not sure why it doesn't work. Try using the tap gesture as suggested below.
You can try this Suppose you have an
IBOutlet UIImageView *touchImageVIew;
Let touchImageVIew height and width are 50,25;
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
// Retrieve the touch point
CGPoint pt = [[touches anyObject] locationInView:touchImageVIew];
if (pt.x>=0 && pt.x<=50 && pt.y>=0 && pt.y<=25) {
NSLog(@"image touched");
}
else
{
NSLog(@"image not touched");
}
}
Adjust height,width and name according to your requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With