Lets say I wan\'t to add 1 to an integer. This will only be done when I push down on a UIButton
and then release my finger on another UIButton
.
Do the following for the first UIButton in the ViewDidLoad
method
[button1 addTarget:self action:@selector(dragMoving:withEvent: )
forControlEvents: UIControlEventTouchDragInside | UIControlEventTouchDragOutside];
[button1 addTarget:self action:@selector(dragEnded:withEvent: )
forControlEvents: UIControlEventTouchUpInside |
UIControlEventTouchUpOutside];
In the dragMoving
method
- (void)dragMoving:(UIControl *)c withEvent:event{
NSSet* touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:[button2 superview]];
if ( CGRectContainsPoint(button2.frame, currentPoint) ) {
NSLog(@"over second button);
}
}
As for the dragEnded
method:
- (void) dragEnded:(UIControl *)c withEvent:event{
NSSet* touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:[self.button1 superview]];
if ( CGRectContainsPoint(button2.frame, currentPoint) ) {
NSLog(@"ended on button 2");
//add 1 to the integer
}
else {
NSLog(@"not on button 2");
}
}