touchesMoved not working fast enough - iphone

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....

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

How to get which object began touch in iphone

I have one view in iPhone application. In that view i added two UIImageView lets say img_view1, img_view2. Now i put touch event on that view as following.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Some code are here
}
how do i can get which image view began to touch ?
UITouch *touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];
if (CGRectContainsPoint(img_view1.frame, pt)
{
NSLog(#"Image View 1 touched");
}
etc.
Make your image view user interaction enabled in the xib (or through the code if you are adding it programmatically) and use the UIResponder methods touchesBegan, touchesMoved, touchesEnded etc to detect the touch on the image view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == yourImageView)
{
//add your code for image touch here
}
}
Use this code
UITouch *touch = [touches anyObject];
if (touch.view==imageView1) {
// do something
}
else if (touch.view==imageView2) {
// do something else
}
Hope this is what you are looking for.
Enjoy coding :)
use this code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if(touch.view == img_view1)
{
}
else if(touch.view == img_view2)
{
}
}

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;
}

how to call image on touch event in 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;
}
}