- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 开发者_开发百科{
}
I want to call two methods in this above method,
-(void) movingUp{
}
-(void) movingDown {
}
I want to call moveUp Method when user will move his touch upward direction, and moveDown when user will touch downward direction, How can I do this, I used gestures, but it is sensing one touch moving up or down, but I want call these methods again and again, as per user movement of touch, either upside or downwards
Help!
The touchesMoved: is called with a touches set ; you can use it :
CGFloat diff = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y;
if (diff > 0) {
// moving down
} else {
// moving up
}
CGPoint beginPoint, tappoint;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
beginPoint = [touch locationInView:self];//or self.view
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
switch ([allTouches count]) {
case 1:
{
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
switch ([touch tapCount])
{
case 1: //Single Tap.
{
tappoint = [touch locationInView:self];
if(tappoint.y-beginPoint.y>0)
{
[self performSelector:@selector(movingUp)];
} else
{
[self performSelector:@selector(movingDown)];
}
}
}
}
}
}
Declare initialPoint and endPoint as Global variables in .h file :
CGPoint initialPoint,endPoint;
And in .m file write below code :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
initialPoint = [[touches anyObject]locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
endPoint = [[touches anyObject]locationInView:self.view];
if ((endPoint.y - initialPoint.y) > 100.f) {
UIAlertView *obj = [[UIAlertView alloc]initWithTitle:@"Touches and Events"
message:@"Swipe Down" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok",nil];
[obj show];
[obj release];
}
else if((endPoint.y - initialPoint.y) < -100.f) {
UIAlertView *obj1 = [[UIAlertView alloc]initWithTitle:@"Touches and Events"
message:@"Swipe Up" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok",nil];
[obj1 show];
[obj1 release];
endPoint = [[touches anyObject]locationInView:self.view];
if ((endPoint.x - initialPoint.x) > 100.f) {
UIAlertView *obj = [[UIAlertView alloc]initWithTitle:@"Touches and Events"
message:@"Swipe right" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok",nil];
[obj show];
[obj release];
}
else if((endPoint.x - initialPoint.x) < -100.f) {
UIAlertView *obj1 = [[UIAlertView alloc]initWithTitle:@"Touches and Events"
message:@"Swipe left" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok",nil];
[obj1 show];
[obj1 release];
}
}
}
精彩评论