How can we check that which image in imageview is being called - iphone

I am having 2 imageviews with image in it. I want that when I click on the first image the image should get selected and if it is selected it should return me value TRUE or 1 that should be saved in sqlite database. How is this possible?

By using UITouch class methods you will get which image view is touched, or you can put imageview inside button then you will get click event.

You go with Touch events.Capture the touch points and perform your actions.Here I will give u a sample structure to do this,
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location= [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
//set some flag like
selectionFlag=1; }
else if(CGRectContainsPoint(secImage.frame, location)){
selectionFlag=2; }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
if(selectionflag==1) {
//do ur db actions }
}
else if(CGRectContainsPoint(secImage.frame, location)) {
if(selectionflag==2) {
//do ur db actions }
}
selectionflag=0;
}

first do
[self.*yourimageViewname* setUserEnteractionEnabled:YES];
BOOL select1,secelect2;
select1=NO;
select2=NO;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
//give the beginning and ending x and y points in condition to check which imageView is taped
if(touchLocation.x>1 && touchLocation.x<116 && touchLocation.y>133 && touchLocation.y<233)
{
select1=YES;
}
else if(touchLocation.x>120 && touchLocation.x<300 && touchLocation.y>133 && touchLocation.y<233)
{
select2=YES;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
On the condition which boolean variable is true you can save or whatever you want to do further coding your application.

Using the touches is one way as explained in the answers below. If you have just two imageview you can also try to have two custom transparent buttons on top of that imageview and therefore you can easily know which image is touched based on the tag you give for the buttons.

What you could also do is create two buttons (instead of UIImageViews) that have images. They should display roughly the same (you could even disable the touch states, etc). And you get UIResponder events for free (as in; you can target the action to a selector).
UPDATE: here's roughly how (didn't bother configuring the buttons though).
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:yourImage forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
// .. now create a second button .. //
And your button will go to the following method when touched:
- (void)buttonTouched:(id)sender
{
// .. add your stuff to your database .. //
// .. you can identify your button by sender, or give the button a tag (number) to identify it .. /
}

Related

How to make two specific images, when a user places one on top of the other, create a third specified image (all of them pre-created images)

I would like the user to be able to press and drag one image (ex: image1.png) and place it over another (ex: image2.png). When the user releases, a third image (ex: image3.png) is added to the screen. How could I go about doing that in xcode?
It's not so easy to do what you want, you have to start to see this touches tutorial from Apple Developer program
here is a code sample for the image moving:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if (CGRectContainsPoint([image1 frame], location)) {
images.center = location;
if (CGRectContainsPoint([image1 frame], [image2 frame]) {
//place your code when image1 = images2
[self.view addSubview:image3];
}
}
}
i think something like this could work

To find what object is underneath our touch when touch moved

How to determine the property or perhaps the object underneath the object i am hovering or dragging?
To put my question clearly, lets take I am hovering a uiview, I want to find out what (object or view) is underneath the view I am hovering.
in custom view you can override touchesEnded method.This sample code may help your custom view hit test problem.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] == 1) {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:custom_view];
if (CGRectContainsPoint(custom_view.bounds, point)) {
//if touch hit to custom_view
};
}
[super touchesEnded:touches withEvent:event];
}
For one, if you know the frames of both objects you can use CGRectIntersectsRect.
if (CGRectIntersectsRect(topObjectsRect, bottomObjectsRect)) {
//
}
Additionally, you could get the point that was touched and then use the following to check if that point is in a certain rectangle.
if (CGRectContainsPoint(CGRectMake(someX, someY, someWidth, someHeight), pointOfTouch))
{
//
}

Continuous Touch detection in imageView using Touch event..?

I have a method which call the another view. i want to call this method after the image is continuously for a definite time.
This type of method found in UIButton = touchDown(),touchup().
Is there any method in touch event to find continuous touch in image view.
Pls help me.
thanks in advance
You can use these events for UIIMAGEVIEW:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
You can use the tap gesture which you can see the answer i wrote in this thread.In this you can set the number of taps which needs to fire the event.Hope this helps you.
assuming you want to count the touch (continous like double tap)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
int index =[touch view].tag;
if (touch.tapCount == number && index == imageTag) {
}
}
tapCount will be the count of tap in continues time with a very short time interval (double tap). you cannot use it if the count of tap you want to watch has longer delay (say 5 single taps). alternatively, you can remember the touches for your imageview, something like:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
int index =[touch view].tag;
if(index == imagetag){
if([tempMutableArray count] < definiteTime){
[tempMutableArray addObject:#"any"]
}else{
[tempMutablArray removeAllObjects];
//you can call the method now
}
}
}

Custom UISlider: avoid updating when dragging outside

I'm quite new to iPhone development and I'm building my first app :)
In one of my view controllers I built a customSlider that should behave as the native "slide to unlock" slider.
My doubt right now is how to implement the "drag outside" behaviour. As said, I would like to have it exactly as the native slider, that means that when the finger is dragging the slider if it moves out the slider, the slider should be going to zero.
My doubt is not on the animation part (I'm already using animation block successfully), but on control events part. Which control event should I use?
I'm using:
[customSlider addTarget:self action:#selector(sliderMoved:) forControlEvents:UIControlEventValueChanged];
to handle the sliding part (finger sliding the cursor), and
[customSlider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
to handle the release part, but the problem is that if I release the finger outside, the sliderAction function it's not called.
EDIT:
I've tried to implement the solution #Bruno Domingues gave me, but I'm realizing that the issue is that by default UISlider keep getting updated even if the finger is dragged outside of it (try to open for example the Brightness section in System Preferences and you'll see that the slider will keep on updating even if you drag outside of it). So my question could be redefined: How to avoid this default behaviour and have the slider updating only when the finger is moving on it?
Simply interrupt the touches methods in your custom subclass and only forward the touches you want acted on to the superclass, like so:
in .h:
#interface CustomSlider : UISlider
#end
in .m:
#import "CustomSlider.h"
#implementation CustomSlider
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchLocation = [[touches anyObject] locationInView:self];
if (touchLocation.x < 0 || touchLocation.y<0)return;
if (touchLocation.x > self.bounds.size.width || touchLocation.y > self.bounds.size.height)return;
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchLocation = [[touches anyObject] locationInView:self];
if (touchLocation.x < 0 || touchLocation.y<0)return;
if (touchLocation.x > self.bounds.size.width || touchLocation.y > self.bounds.size.height)return;
[super touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchLocation = [[touches anyObject] locationInView:self];
if (touchLocation.x < 0 || touchLocation.y<0)return;
if (touchLocation.x > self.bounds.size.width || touchLocation.y > self.bounds.size.height)return;
[super touchesEnded:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchLocation = [[touches anyObject] locationInView:self];
if (touchLocation.x < 0 || touchLocation.y<0)return;
if (touchLocation.x > self.bounds.size.width || touchLocation.y > self.bounds.size.height)return;
[super touchesCancelled:touches withEvent:event];
}
#end
Please note that this implementation will start updating the control if your finger moves back to the control. To eliminate this, simply set a flag if a touch is received outside of the view then check that flag in subsequent touches methods.
You need to set a [slider addTarget:self action:#selector(eventDragOutside:) forControlEvents:UIControlEventTouchDragOutside];
And add a function
- (IBAction)eventDragOutside:(UISlider *)sender {
sender.value = 0.0f;
}

Handling touches on Iphone

Im having a little problem on handling touches in my apps.
I set my touchesBegan like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *currentTouch = [[event allTouches] anyObject];
touchPoint = [currentTouch locationInView:self.view];
if (CGRectContainsPoint(image1.frame, touchPoint)) {
image1IsTouched = YES;
}
}
Then i set my touch move like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *currentTouch = [[event allTouches] anyObject];
currentPoint = [currentTouch locationInView:currentTouch.view];
if(image1IsTouched == YES) {
image1.center = CGPointMake(currentPoint.x,currentPoint.y);
.....
}
}
Now i tried my app on actual unit and thats where i notice my problem. While im touching the image1 with 1 finger the app is doing ok and its checking for collision everytime i drag my finger. The problem occurs when i touch the screen with another finger while touching/dragging the image. The image im currently touching will jump to the other finger. I've tried [myView setMultipleTouchEnable:NO]; & using NSArray on touches and comparing the [touches count] with the touch but its not working. Can someone show me how to set a uiimageview to act on single touch only. Thanks.
First, you should use UITouch *currentTouch = [touches anyObject]; to get the current touch.
Second, you should check that touches.count == 1 to make sure there's only one finger on the screen, and ignore touch input if there's more than one, unless you wanted to support multitouch.