UIScrollView - detect second touch while scrolling - iphone

I have a subclass of UIScrollView that implements this method:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touches: %d", [[event allTouches] count]);
[self.nextResponder touchesBegan:touches withEvent:event];
}
This gets called when I first touch the scroll view. However, if I begin dragging the scroll view, then place a second finger down, touchesBegan does not get called. I need to detect when a second touch has occured in the scrollview, even if it is currently scrolling. Any ideas?
EDIT: Two touches are registered if I start with two, but if I start with one, begin scrolling, then put a 2nd finger down it is not registered.

Make sure multipleTouchEnabled is set to YES on your scroll view.
You can also set this in Interface Builder. Look for 'Multiple Touch' checkbox.

Related

how to find out when a person has left his finger off the screen?

thats pretty much all i was wondering. Anybody have a quick answer?
i want my program to do some code when the user stops scrolling and has lifted his finger up
you can implement the following method:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
This will get called whenever you are lifting your finger up.
if you use scrollView , it is better use the delegate:
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

Stop Scrolling UIScrollView When Drawing its SubViews

I have a task in that i am able to draw in subviews of UIScrollView. UIScrollView contains 3 Pages. Each Page contains many UIView as subviews in theses subviews i want to draw like writting but when i tocuh the view and start drawing i can do that because UIScrollView gets scrolled.How to stop scroll when writting time only.
I searched lot for solution. I also used UIView's hitTest method but I am not succeed.Can you provide some suggestions to overcome this issue.
Thanks.
Here's some touchEvent handlers that you need to implement
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
In touchBegan event, you can set a logic to starting drawing after set
[yourScrollView setScrollEnabled:NO];
once you done with drawing in touchEnded event you need to reset your scrollview.
[yourScrollView setScrollEnabled:YES];
P.S. I've not idea of working of my solution but you can try for this.

UIGestureRecognizer that cancels when touchup outside of targetView

I have a UIView of which I want to know when the user is doing:
touchDownInside (to highlight the view)
touchUpInside (to confirm the action)
touchUpOutside (to cancel and reset the hightlight)
what gestureRecognizer can do this for me?
Please go though these four methods also which your view can override to handle the four distinct touch events:
1) finger or fingers touches the screen
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
2)finger or fingers move across the screens(this message repeatedly as a finger moves.)
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event;
3)finger or fingers is removed from the screen
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event;
4) a system event,interrupts a touch before it ends
-(void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event;
You can do this implementing the touches methods itself, why do you need gesture recognizer?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
The above function for touch down.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
The above function for touch up. And the combination of both for cancel.

How are objects made draggable in iphone app?

I would like to have objects in my view which can be dragged and dropped.
When they are dropped in a certain area they should disappear and some code should execute.
How are objects made draggable in an iphone app? Is it just a button which can be drag enabled?
How would one detect the position of the draggable object? Would it be as simple as (Psuedo)
if (draggableButton1.xPosition > e.g. && draggableButton1.xPosition < e,g
&& draggableButton1.yPosition > e.g. && draggableButton1.yPosition < e,g) {
do something
}
?
When user touches the object that time following method is called.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
When moved with touched, following method will be called.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
When user end with touch, the following method will be used
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
The NSSet object contain the UITouch object which contain all information for touches on view, which view, position on screen , previous position and so more. You can check documentation for that.
Start your logic from touchesBegan and change the position of your object in touchesMoved event. then when user drop object, touchesEnded event will be called. Check for position in touchesEnded event and excute your code as you have said.
May this three methods will help...
If said object is an UIView you can get its frame origin and size.
Check Apples MoveMe example: http://developer.apple.com/iphone/library/samplecode/MoveMe/Introduction/Intro.html

How can an underlying view know if its rectangle got touched, no matter if directly or indirectly?

I have an UIView which is transparent and covers almost the whole screen. I left 50 pixels at the top. It is a child of the View Controller's view.
Underneeth the UIView there's MyView that inherits from UIView, which matches the screen size. And inside this MyView class, I ask for a touch on it very simple with this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == self) {
NSLog(#"MyView touched");
}
}
Now the funny thing is, of course, that if the user touches on the transparent UIView which covers that MyView, I don't get "MyView touched" in the console. But when the user touches the little uncovered area of MyView at the top of the screen, then the touch arrives there.
That's logical to me, because I ask for [touch view] == self. But what if I wanted to know that the rectangular area of that MyView got touched (no matter if indirect or direct)?
Is there a way to catch any touch that appears on the screen/window and then just check if it matches the rectangular area of the view?
You should study the iPhone Application Programming Guide's section on Touch Events for the background you're looking for. The concept you want to master is the Responder Chain, so also look through the reference on UIResponder to understand what it's doing. You can definitely do everything you're talking about, and the full discussion is in the link above.