Drag UIButton from one view to another by using touch method - ios

Hello all,
I am using "Touch methods" first time so facing certain issue.
In my app, there are 2 subviews in mainview.I am trying to implement drag and drop of uibutton from one view to another view.
I'm trying lots of things but not able to do drag and drop.
please help me.
Thanks in advance.
My Code :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];
for (UIButton *iView in self.view.subviews) {
if ([iView isMemberOfClass:[UIButton class]])
{
if (touchPoint.x > iView.frame.origin.x &&
touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
touchPoint.y > iView.frame.origin.y &&
touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
{
self.btnalpha = iView;
touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
touchPoint.y - iView.frame.origin.y);
homePosition = CGPointMake(iView.frame.origin.x,
iView.frame.origin.y);
[self.viewblue bringSubviewToFront:self.btnalpha];
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];
CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
touchPoint.y - touchOffset.y,
self.btnalpha.frame.size.width,
self.btnalpha.frame.size.height);
self.btnalpha.frame = newDragObjectFrame;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];
if (touchPoint.x > self.vieworange.frame.origin.x &&
touchPoint.x < self.vieworange.frame.origin.x + self.vieworange.frame.size.width &&
touchPoint.y > self.vieworange.frame.origin.y &&
touchPoint.y < self.vieworange.frame.origin.y + self.vieworange.frame.size.height )
self.btnalpha.frame = CGRectMake(self.homePosition.x, self.homePosition.y, self.btnalpha.frame.size.width, self.btnalpha.frame.size.height);
}
- (void) drag
{
str_scramble =#"IH";
for (int i = 0; i < 2; i++)
{
self.btnalpha = [[UIButton alloc] init];
[self.btnalpha setFrame:CGRectMake(i * 50 + 10, 5, 42, 42)];
char letter = [str_scramble characterAtIndex:i];
self.btnalpha.backgroundColor = [UIColor grayColor];
self.btnalpha.titleLabel.font = [UIFont boldSystemFontOfSize:20];
[self.btnalpha setTitle:[NSString stringWithFormat:#"%c", letter] forState:UIControlStateNormal];
[self.btnalpha addTarget:self action:#selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];
[self.btnalpha addTarget:self action:#selector(touchesMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[viewblue addSubview:self.btnalpha];
NSLog(#"string : --> %#", self.btnalpha.titleLabel.text);
}
}

you should use touchDrag methodes like touchDragEnter, touchDragExit, touchDragInside or touchDragOutside to drag a UIButton.
These all events are UIControlEvents

I am using PanGestureRecognizer and it works great.
UIPanGestureRecognizer *drag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(dragButton:)];
drag.delegate = self;
drag.view.tag = position;
[button addGestureRecognizer:drag];
- (void)dragButton:(UIPanGestureRecognizer *)gesture
{
buttonPicked = [allObjects objectAtIndex:gesture.view.tag -1];
if (gesture.state == UIGestureRecognizerStateBegan)
{
originalPoint = buttonPicked.center;
}
else
{
buttonPicked.center = [gesture locationInView:self.view];
}
}
If you want some kind of special treatment for the end gesture (such as only to drop if in a certain location) - you can use the UIGestureRecognizedStateEnd.
if it's still not working - in the UIGestureRecognizerStateBegan, try to remove the button from the subview and add it to the main view, and in the UIGestureRecognizerStateEnd check which subview the button is in and add it instead of the main view. Should do the trick.
Good luck.

According to apple Documentation on Event Handling Guide,
when you first touch on button, it becomes first responder.SO, touchesBegan:withEvent in your ViewController is not called.
Disable the userInteraction of UIbutton and now viewcontroller will be first responder and starts receiving touch events.
button.userInteractionEnabled=NO;
Eventhough you addGestures to ViewController Or any of those Views, they wont respond when your touches are on button.

Related

How to move array of UIImageViews smoothly?

Am working on kind of seating arrangement application. Where the app allows the admin to arrange the seatings by click and move the each seat and then save the seating arrangement with frame sizes of each seat. I tried by loading 5 UIImageView on custom view and move the imageview but, only one imageview is getting moved. Rest of the imageviews not getting moved. Can you please help me on this?
- (void) addSeatsToTheView {
for (int i=0; i<5; i++) {
self.hotelImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
self.hotelImage.image = [UIImage imageNamed:#"DiningIcon"];
self.hotelImage.userInteractionEnabled = YES;
[self.hotelImage setUserInteractionEnabled:YES];
self.hotelImage.tag = i;
[self.hotelView addSubview:self.hotelImage];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view == self.hotelImage) {
CGPoint location = [touch locationInView:self.hotelView];
NSLog(#"Begen Touch Location: %#", NSStringFromCGPoint(location));
self.hotelImage.frame=CGRectMake(location.x, location.y, 50, 50);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view == self.hotelImage) {
CGPoint location = [touch locationInView:self.hotelView];
NSLog(#"Begen Touch Location: %#", NSStringFromCGPoint(location));
self.hotelImage.frame=CGRectMake(location.x, location.y, 50, 50);
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view == self.hotelImage) {
CGPoint location = [touch locationInView:self.hotelView];
NSLog(#"Begen Touch Location: %#", NSStringFromCGPoint(location));
self.hotelImage.frame=CGRectMake(location.x, location.y, 50, 50);
}
}
Once the admin arranged the seatings we have to save all the visible uiimageview framesizes. Thanks.
Edited:
I tried the using UIPanGestureRecognizer but, only one imageview getting moved. Don't know where am doing wrong? Can you please help me? Thanks.
- (void) addSeatsToTheView {
for (int i=0; i<5; i++) {
self.hotelImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
self.hotelImage.image = [UIImage imageNamed:#"DiningIcon"];
self.hotelImage.userInteractionEnabled = YES;
[self.hotelImage setUserInteractionEnabled:YES];
self.hotelImage.tag = i;
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(gestureRecognizerMethod:)];
[self.hotelImage addGestureRecognizer:panGestureRecognizer];
[self.hotelView addSubview:self.hotelImage];
}
}
- (void)gestureRecognizerMethod:(UIPanGestureRecognizer *)recogniser
{
if (recogniser.state == UIGestureRecognizerStateBegan || recogniser.state == UIGestureRecognizerStateChanged)
{
CGPoint touchLocation = [recogniser locationInView:self.hotelView];
self.hotelImage.frame = CGRectMake(touchLocation.x, touchLocation.y, 50, 50);
}
}
Don't use touches methods for this; you'll go insane. Make each image view user-interactive and give it a UIPanGestureRecognizer. There is standard code for the pan gesture recognizer's action handler for following a finger, i.e. making the view draggable.
Once you've done that, everything stems from your misuse of self.hotelImage. Get rid of it entirely. In your for loop, just use a local variable, UIImageView* hotelImage = .... In the gesture recognizer method, use [recogniser view] to refer to the image view to which this gesture recognizer is attached. Everything will sort itself out after that!

touchEvent for subview in UIScrollView

All,
I have an issue and I have looked at all possible answers on StackOverflow (and other internet) but all solutions I try, nothing works....
I have a UIScrollView with a UIImageView as subview to create a board. I can zoom in and out, works fine. Also scrolling works fine!
Now, I want to drag & drop other UIImageViews (tiles) into the ScrollView and also OUT of the ScrollView. But when I drop the UIImageView in the ScrollView, I can not drag/drop the subview.
I have set the setCanCancelContentTouches:NO of the UIScrollView in the viewDidLoad.
I have created a subclass TileImageView and in the init I set the exclusiveTouch to YES.
NB: the boardImage is a 'normal' UIImageView', the tiles are with the subclass!
Addition: when I set the UserInteraction setting of the boardImage to NO, it the ScrollView zooms but touchBegan is not recorded. If I set it to YES, the ScrollView does not zoom, but the touchBegan is recorded..
So I leave it so I can zoom, but the Tiles are not draggable anymore once they are in the ScrollView.
What am I doing wrong? Or what am I missing?
PS: setting the delaysContentTouches property makes scrolling inactive, so that is also not a solution...
Code:
RootViewController_iphone.m
#implementation RootViewController_iphone
#synthesize boardScrollView;
#synthesize dragObject;
#synthesize boardImage;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImage *image = [UIImage imageNamed:#"greyblue_numbered_15x15_900x900.png"];
self.boardImage = [[UIImageView alloc] initWithImage:image];
self.boardImage.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.boardScrollView addSubview:self.boardImage];
self.boardImage.userInteractionEnabled = YES;
self.boardScrollView.contentSize = image.size;
self.boardScrollView.userInteractionEnabled = YES;
self.boardScrollView.canCancelContentTouches = NO;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 1) {
// one finger
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
for (UIImageView *iView in self.view.subviews) {
if ([iView isMemberOfClass:[TileImageView class]]) {
if (touchPoint.x > iView.frame.origin.x &&
touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
touchPoint.y > iView.frame.origin.y &&
touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
{
self.dragObject = iView;
self.touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
touchPoint.y - iView.frame.origin.y);
self.homePosition = CGPointMake(iView.frame.origin.x,
iView.frame.origin.y);
[self.view bringSubviewToFront:self.dragObject];
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
touchPoint.y - touchOffset.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
self.dragObject.frame = newDragObjectFrame;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
NSString *tmp = touch.view.description;
CGPoint touchPointScreen = [[touches anyObject] locationInView:self.view];
CGPoint touchPointImage = [[touches anyObject] locationInView:self.boardImage];
CGPoint touchPointBoard = [[touches anyObject] locationInView:self.boardScrollView];
if (touchPointScreen.x > self.boardScrollView.frame.origin.x &&
touchPointScreen.x < self.boardScrollView.frame.origin.x + self.boardScrollView.frame.size.width &&
touchPointScreen.y > self.boardScrollView.frame.origin.y &&
touchPointScreen.y < self.boardScrollView.frame.origin.y + self.boardScrollView.frame.size.height)
{
self.dragObject.frame = CGRectMake(touchPointImage.x - touchOffset.x,
touchPointImage.y - touchOffset.y,
self.dragObject.frame.size.width,
self.dragObject.frame.size.height);
[self.boardImage addSubview:self.dragObject]; // add to image
}
}
Code:
TileImageView.m
#implementation TileImageView:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.exclusiveTouch = YES;
}
return self;
}
You already have subclassed UIImageView, so you can do the following inside your subclass:
1- Override touchsBegan, didEnd, and didMove
2- inside these methods pass them to the superview which is UIScrollView: [self.superview touchsBegan .... ]
3- If your UIImageView not receiving these messages then in init add self.userInteractionEnabled = YES;
Hopefully this going to help you

Cancel multiple touches on the View

I am adding UIPanGestureRecogniser to drag and drop images in a view.I want to drag only one image at a time.but I am able to drag two images at a time.which should not happen.
I am stuck with this bug since morning.I tried all the ways i found on google.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setMultipleTouchEnabled:NO];
for(UIImageView *iView in self.movableArray){
if ([iView isMemberOfClass:[UIImageView class]]){
UIPanGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[iView addGestureRecognizer:recognizer];
[iView setUserInteractionEnabled:YES];
recognizer.delegate = self;
}
}
}
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
[gameView bringSubviewToFront:[(UIPanGestureRecognizer *)recognizer view]];
CGPoint translation = [recognizer translationInView:gameView];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:gameView];
self.dragObjectImageView = (UIImageView*)recognizer.view;
if(recognizer.state == UIGestureRecognizerStateBegan){
int ind = [self.movableArray indexOfObject:self.dragObjectImageView];
for(int i = 0 ; i < [self.movableArray count] ; i ++){
if( i != ind ){
[[self.movableArray objectAtIndex:i] removeGestureRecognizer:recognizer];
}
}
self.homePosition = self.dragObjectImageView.frame;
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint touchPoint = [recognizer locationInView:gameView];
for (UIImageView *iView in self.staticArray) {
if ([iView isMemberOfClass:[UIImageView class]]) {
if (touchPoint.x > iView.frame.origin.x &&
touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
touchPoint.y > iView.frame.origin.y &&
touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
{
self.dropTargetImageView = iView;
}
}
}
if(self.dragObjectImageView.tag == self.dropTargetImageView.tag){
self.dragObjectImageView.frame = CGRectMake(self.dropTargetImageView.frame.origin.x, self.dropTargetImageView.frame.origin.y + self.dropTargetImageView.frame.size.height/2 - 15, self.dragObjectImageView.frame.size.width, self.dragObjectImageView.frame.size.height);
[self.dragObjectImageView removeGestureRecognizer:recognizer];
}
}else{
self.dragObjectImageView.frame = self.homePosition;
}
}
}
This happens because you are adding one UIPanGestureRecognizer for each of your imageViews. Try adding only one to your self.view (and have your imageViews to setUserInteractionEnabled:NO, otherwise they will trap the touch). Also put
recognizer.maximumNumberOfTouches = 1;
Before you add it to your view. All you have to do now is to test which image view should be dragged in your handlePan method. You should check the recognizer state and when it turns to UIGestureRecognizerStateBegan you should save which imageView is being dragged. Then as the state is UIGestureRecognizerStateChanged just drag the view around. The most general way you can find out which view was touched (as i don't know your full view hierarchy) would be to do something like:
NSUInteger index = [self.movableArray indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop)){
UIView* hitTest = (UIView*)obj;
return [hitTest pointInside:firstTouch withEvent:nil];
}];
if ( index != NSNotFound )
self.draggingView = self.moveableArray[index];
else
self.draggingView = nil;
Then of course if self.draggingView is nil you would do nothing when the user is panning around.
You should create a mechanism to setUserInteractionEnable = NO when your
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer;
Is called on the other images. You can either do that, or disable the others UIPanGestureRecognizer, like: myPanGestureRecognizer.enabled = NO;
A quick example:
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer
{
UIImageView *currentDraggedImageView = recognizer.view;
// Based on this, you can iterate again on your UIImageViews and disable them.
// Once your work is done with gesture recogniser, you can re-enable them.
}

How to move UIView by longPress on button and then dragging?

I want to move some UIView by button inside this view.
I can it, in that way:
- (void)viewDidLoad
{
[button addTarget:self action:#selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown];
[button addTarget:self action:#selector(dragMoving:withEvent:) forControlEvents: UIControlEventTouchDragInside];
[button addTarget:self action:#selector(dragEnded:withEvent:) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
}
.
- (void)dragBegan:(UIControl *)c withEvent:ev {
UITouch *touch = [[ev allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
}
- (void)dragMoving:(UIControl *)c withEvent:ev {
UITouch *touch = [[ev allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
//This is moving view to touchPoint
SimpleView.center = touchPoint;
}
- (void)dragEnded:(UIControl *)c withEvent:ev {
}
How can i move it only then, if I do longPress on that button?
Try using this code. I've used this in a card game i developed. moving cards around using long press gestures. Hope i helps.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(addLongpressGesture:)];
[longPress setDelegate:self];
[YOUR_VIEW addGestureRecognizer:longPress];
- (void)addLongpressGesture:(UILongPressGestureRecognizer *)sender {
UIView *view = sender.view;
CGPoint point = [sender locationInView:view.superview];
if (sender.state == UIGestureRecognizerStateBegan){
// GESTURE STATE BEGAN
}
else if (sender.state == UIGestureRecognizerStateChanged){
//GESTURE STATE CHANGED/ MOVED
CGPoint center = view.center;
center.x += point.x - _priorPoint.x;
center.y += point.y - _priorPoint.y;
view.center = center;
// This is how i drag my views
}
else if (sender.state == UIGestureRecognizerStateEnded){
//GESTURE ENDED
}
I would use this link provided by #Coder404 to detect whether or not the player used a long touch. Then, add an #property BOOL performedLongTouch and set that to YES in the selector passed into the UILongPressGestureRecognizer
Then, in your dragBegan and dragMoving functions, add a check for performedLongTouch and in your dragEnded function, set its value to NO
I know that seem pretty straight forward, but is that what you were looking for?

Detect diagonal swipe gestures in a UIView

I want to detect a two-finger diagonal swipe that starts from the bottom right of the screen to the middle. I tried adding UISwipeGestureRecognizer with direction set as "UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionLeft" but to no vail as the handler is getting invoked even if I start the swipe from the middle of the screen to the top left.
Do I need to sub-class UIGestureRecognizer or can I handle this using touchesBegan and touchesMoved ?
You need to subclass it to use touchesBegan and touchesMoved anyway. I'd do UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionLeft like you said, then set up two CGRects, one for acceptable starting points, one for acceptable end points. Then use the UIGestureRecognizerDelegate method gestureRecognizer:shouldReceiveTouch:, and check if the touch point is in the acceptable start rect by using CGRectContainsPoint(). Then (I think) in your UISwipeGestureRecognizer subclass, override touchesEnded:withEvent:, and check if the end touch is in the acceptable rect. If it's not, set the state to UIGestureRecognizerStateCancelled (or however you're supposed to cancel a gesture). Also make sure the view it's attached to has its multipleTouchEnabled property set. I haven't actually tried this, but something of the sort should do it. Good luck!
EDIT
Actually, if you didn't want to have to worry about specific rect values for the acceptable start/end points, and make it device independent, you could do this:
//swap in whatever percentage of the screen's width/height you want in place of ".75f"
//set the origin for acceptable start points
CGFloat startRect_x = self.view.frame.size.width * .75f;
CGFloat startRect_y = self.view.frame.size.height * .75f;
//set the size
CGFloat rectWidth = self.view.frame.size.width - startRect_x;
CGFloat rectHeight = self.view.frame.size.height - startRect_y;
//make the acceptable start point rect
CGRect startRect = CGRectMake(startRect_x, startRect_y, rectWidth, rectHeight);
//set the origin for the accepable end points
CGFloat endRect_x = self.view.center.x - rectWidth/2;
CGFloat endRect_y = self.view.center.y - rectHeight/2;
//make the acceptable end point rect
CGRect endRect = CGRectMake(endRect_x, endRect_y, rectWidth, rectHeight);
the touches method of cause can do every Gesture, the Gesture is supported since ios3.2.
i can give you a simple code , you maybe modify by yourself.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
isTwoFingersBegin = NO;
isTwoFingersEnd = NO;
UITouch *touch = [touches anyObject];
UIView *touchView = [touch view];
if ([touches count] == 2)
{
isTwoFingersBegin = YES;
}
touchStartPoint = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
isSwip = YES;
}
#define TOUCH_MOVE_EFFECT_DIST 30
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
UIView *touchView = [touch view];
touchEndPoint = [touch locationInView:self.view];
if ([touches count] == 2)
{
isTwoFingersEnd = YES;
}
CGPoint deltaVector = CGPointMake(touchEndPoint.x - touchStartPoint.x, touchEndPoint.y - touchStartPoint.y);
if (fabsf(deltaVector.x) > TOUCH_MOVE_EFFECT_DIST
&&fabsf(deltaVector.y) > TOUCH_MOVE_EFFECT_DIST
&& isSwip &&isTwoFingersBegin&&isTwoFingersEnd) {
theSwipGesture=RightUpGesture;
}
else if (fabsf(deltaVector.x) <- TOUCH_MOVE_EFFECT_DIST
&& fabsf(deltaVector.y) <- TOUCH_MOVE_EFFECT_DIST
&& isSwip&&isTwoFingersBegin&&isTwoFingersEnd) {
theSwipGesture=LeftDownGesture;
}
isSwip = NO;
}
You can use a UIPanGestureRecognizer
- (void)viewPanned:(UIPanGestureRecognizer *)panGR
{
CGPoint translation = [panGR translationInView:self];
CGFloat threshold = self.bounds.size.width/2;
// Detect
Direction direction = DirectionUnknown;
if (translation.x > threshold) {
if (translation.y > threshold) {
direction = DirectionBottomRight;
} else if (translation.y < -threshold) {
direction = DirectionTopRight;
} else {
direction = DirectionRight;
}
} else if (translation.x < -threshold) {
if (translation.y > threshold) {
direction = DirectionBottomLeft;
} else if (translation.y < -threshold) {
direction = DirectionTopLeft;
} else {
direction = DirectionLeft;
}
} else {
if (translation.y > threshold) {
direction = DirectionBottom;
} else if (translation.y < -threshold) {
direction = DirectionTop;
}
}
}
One possible solution would be to map off a group of coordinates on the iPhone's screen where the swipe could potentially start, and another where it could potentially end. Then in the touchesBegan: and touchesMoved: methods, you could compare the starting and ending points of the swipe and determine whether it qualified as diagonal.
Thanks Guys!
I ended up writing custom code in touchesBegan, touchesMoved and touchesEnded and ot works like charm.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 2) {
CGPoint nowPoint = [[touches anyObject] locationInView:self];
if( nowPoint.x >= ALLOWED_X)
swipeUp = YES;
}
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if ([touches count] == 2 && swipeUp) {
CGPoint nowPoint = [[touches anyObject] locationInView:self];
CGPoint prevPoint = [[touches anyObject] previousLocationInView:self];
if( nowPoint.x <= prevPoint.x && nowPoint.y <= prevPoint.y){
}
else {
swipeUp = NO;
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
CGPoint nowPoint = [[touches anyObject] locationInView:self];
if ([touches count] == 2 && swipeUp && nowPoint.x <= DELTA_X && nowPoint.y <= DELTA_Y) {
NSLog(#"Invoke Method");
}
swipeUp = NO;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
swipeUp = NO;
}

Resources