how to call image on touch event in iphone - iphone

i have created an application in which there are 2 imageviews and i have added image on image view.i wnt that when i click on my image the image should get selected and the value should be saved in sqlite database.So for that i have created touches method,and added flags for both the images so when particular image is selected it is identified by its flag.
this is my code:
-(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
select=1;
}
else if(CGRectContainsPoint(secImage.frame, location))
{
select=2;
}
[mComment resignFirstResponder];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
if(select==1) {
var=1;
}
else if(CGRectContainsPoint(secImage.frame, location)) {
if(select==2) {
vars=2;
}
select=0;
}
}
}
But i am having a problem,when i select my first image it properly gets into the if part and stores value 1 into var 1 but when i click secimage it does not enter into elseif part,it just comes out of the loop.What may be the problem .Please help me in solving this problem.thanks

The following change should fix the problem:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
if(select==1) {
var=1;
}}
if(CGRectContainsPoint(secImage.frame, location)) {
if(select==2) {
vars=2;
}}
select=0;
}
Your above code wasn't working because if(CGRectContainsPoint(firstImage.frame, location) == YES, it will never reach the line if(CGRectContainsPoint(secImage.frame, location)
The code could also be cleaned up a bit. Not sure this is the ideal way of implementing these two methods, but it could work anyway.

May be your code has a mistake? Try this
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
if(select==1)
{
var=1;
}
}
else if(CGRectContainsPoint(secImage.frame, location))
{
if(select==2)
{
vars=2;
}
select=0;
}
}

Related

Multi object drag

While dragin multi objects using the following code the the object is shaking when i touch the object,
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == img1)
{
CGPoint location = [touch locationInView:touch.view];
img1.center = location;
return;
}
if (touch.view == img2)
{
CGPoint location = [touch locationInView:touch.view];
img2.center = location;
return;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
What is problem in my code, is there any other method to drag and if the view contain one more object?please help me to solve this issue
I think if you are talking about more object to drag all at once then you should remove the return from you touch begain method

touchesMoved not working fast enough

I want when user slides from left to right label counter updates from 1 to 5. If I swipe slowly, very slowly it works, faster then that and it doesn't work right? Is there another way to implement this?
I have this code:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
if ((int)(currentPosition.x)%40 == 0 && counter < 5) {
counter++;
}
[label setText:[NSString stringWithFormat:#"%d", counter]];
}
You can use UIPanGestureRecognizer... it has begin state ,change state and end state like as touchEvents...
- (void)onDraggingLetter:(UIPanGestureRecognizer *)panGR {
if (panGR.state == UIGestureRecognizerStateBegan) {
} else if (panGR.state == UIGestureRecognizerStateChanged) {
} else if (panGR.state == UIGestureRecognizerStateEnded) {
}
}
it may helps you....

Drag image from carousal to imageview

I want to drag image from carousel of image and put it in another imageview.After drag of it should delete from carousel.I have done code for this that will also remove from carousel.
I used touch events.
But problem is that after draging one image when i touch anywhere else in the screen it will select next image automatically.How can i stop it?I want to drag image when i click on it.
My Code.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
btnBackImg.center = location;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint pointMoved = [touch locationInView:self.view];
btnBackImg.frame = CGRectMake(pointMoved.x, pointMoved.y, btnBackImg.frame.size.width,btnBackImg.frame.size.height);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touch end");
if (btnBackImg != nil)
{
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint(basket_image.frame, [touch locationInView:self.view]))
{
basket_image.image=[UIImage imageNamed:[NSString stringWithFormat:#"wit_bas.png"]];
[self.product_categories removeObjectAtIndex:imgIndex+1];
[carousel reloadData];
}
}
}
In this btnCackImg want to move in to basketImg.
If anyone konw then pls help me or if any link then also useful.
Thanks in Advance.
You have used this code in your touches.began CGPoint location = [touch locationInView:touch.view]; where touch.View means touching all the objects in the view namely self.view. What you really need is to add the name of the image to be put instead of touch.View. This will select the image at one time.
CGPoint location = [touch locationInView:imageName];

UIScrollview touchesbegan,touchesmoved,touchesended actions

I did this touch actions in UIView, ie,
in a uiview there are two or three subviews v1,v2,v3. I used the code below to place the image i1,i2,i3 in corresponding views and if I moves the touch the images move to that point in the view.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == v1)
{
CGPoint location = [touch locationInView:v1];
i1.center = location;
}
else if([touch view] == v2)
{
CGPoint location = [touch locationInView:v2];
i2.center = location;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == v1)
{
CGPoint location = [touch locationInView:v1];
i1.center = location;
}
else if([touch view] == v2)
{
CGPoint location = [touch locationInView:v1];
i2.center = location;
}
}
Now I have to do this for a sequence of 28 images so I went for Uiscrollview but I cant get that please explain me clearly with code. thanks a lot in advance for your help.
You need to subclass UIScrollView to get touch events of UIScrollView.

how to highlight the image on touch event in iphone

hi friend i created this method for image select it working proper but i am facing problem on image when i touch the image i cant see image click or not i want highlight the image when i touch on it how can i 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))
{
// flag like
select=1;
}
else if(CGRectContainsPoint(secImage.frame, location))
{
select=2;
}
[mComment resignFirstResponder];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
if(select==1) {
var=1;
}}
else if(CGRectContainsPoint(secImage.frame, location)) {
if(select==2) {
vars=1;
}}
select=0;
}
One thing you can do for highlight the image when you touch on it. When you touch on image you should change the alpha of selected image in touch began method and reset the image alpha in touch ended method. So its look like button.
For Ex.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location= [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location))
{
// flag like
select=1;
firstImage.alpha = 0.5;
}
else if(CGRectContainsPoint(secImage.frame, location))
{
select=2;
secImage.alpha = 0.5;
}
[mComment resignFirstResponder];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
if(select==1) {
firstImage.alpha = 1.0;
var=1;
}}
else if(CGRectContainsPoint(secImage.frame, location)) {
if(select==2) {
secImage.alpha = 1.0;
vars=1;
}}
select=0;
}