iPhone UIScrollView, calling the pan and pinch gesture selectors programmatically [duplicate] - iphone

This question already has answers here:
UIScrollView and cancel a zooming pinch gesture
(3 answers)
Closed 3 years ago.
My needs are to override the UIScrollView pan and pinch gestures with a simple condition, so they would work only if a certain condition is true.
Overriding the default gesture was ok, disabling them and calling my own gesture which prior logic was ok. But in case the condition is true I want to call the default selectors of the pan and pinch to obtain the normal slick scrollview behavior with bouncing and insets and such...
So how can I call the default gesture selectors programmatically.

You should look into subclassing UIScrollView and override the following methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
If the condition is true, you call the corresponding method on super.
To always enable scrolling you can check if the touches set contains exactly one object.

Related

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.

Semi-piano app touch troubles with UIButtons

I am working on a semi-piano app with a diffrent keyboard-layout then a usual one.
I created the view manually with UIButtons,
My problem was I that I didn't know how to slide from a UIButton to another,
I figured that out with addTarget with the option of withEvent, which gave me the access to the touches.
Now, after I added the target like this:
[C addTarget:self action:#selector(outsideOfKey: forEvent:) forControlEvents:UIControlEventTouchDragOutside|UIControlEventTouchDragInside];
[C addTarget:self action:#selector(keyGetsLeft: forEvent:) forControlEvents:UIControlEventTouchUpOutside | UIControlEventTouchUpInside];
(also for all of the other keys),
I maneged to make them slideable,
outsideOfKey:forEvent: is as follows:
-(void) outsideOfKey:(id)sender forEvent:(UIEvent *)event
{
for(UITouch *t in [event allTouches])
{
CGPoint touchPoint = [t locationInView:window];
if(CGRectContainsPoint(C.frame, touchPoint))
{
C.highlighted = YES;
}
else{
C.highlighted = NO;
}
(Done for all the other keys as well)
I can slide from and into other keys, and when I leave them in keyGetsLeft:forEvent: I have just used the same syntx without the else, and the highlighted became NO.
Up to here it's easy,
But then when I try to do multi-touch, I can slide only one of the touches all around and the others must stay in the same position.
And even more, If I take one of the fingers away all of them are becoming non-highlighted,
I know the reasons to all of that, but I don't know how to fix it and make it to work.
I would probably move away from UIButtons altogether and implement my own custom touch tracking code. See Handling Multitouch Events in the docs. Namely, you will be implementing the following methods:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
I would probably just implement hit areas, which you could setup in IB, with perhaps a custom UIView touch overlay (just a UIView with a custom subclass). This would let you setup the view in IB with images, titles, etc., but do all of your touch tracking in your custom subclass.
I am afraid, bensnider is right. But I'd implement it via GestureRecognizer:
https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_uikit_gestures
https://developer.apple.com/documentation/uikit/uigesturerecognizer
Each key has one TapRecognizer, while a parent view has a SwipeRecognizer to detect slides form one key to another.
Very useful, on Apple Developer Video Archivelogin with development account required:
WWDC2010: Session 120 — Simplifying Touch Event Handling with Gesture Recognizers
WWDC2010: Session 121 — Advanced Gesture Recognition
I'd use one view for all buttons and manually implement touch tracking as bensnider said. Manually drawing backgrounds/titles also is not so difficult.
Heres a link to an open source iOS piano app I made https://github.com/meech-ward/iOSPiano
I used CALayers for the piano keys and I used
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
to detect if the user is currently touching a piano key.
It works really well even with multiple touches.

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

iPhone Dev: Making a tableHeaderView respond to touches

Ok, I'm a objective c noob. I have a table view with a tableHeaderView and I want it to respond to touches?
Here are some steps that I do when creating the view:
1. create a view
2. add a bunch of labels and imageviews to it
3. set my tableHeaderView to the view I created
Any help will be appreciated!
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(tapTableHeader:)];
[self.tableView.tableHeaderView addGestureRecognizer:recognizer];
- (void)tapTableHeader:(UITapGestureRecognizer *)recognizer {
NSLog(#"table header taped");
}
Is the desired touch behavior especially complex? If not, just make the part of the header that needs to respond to touches a button, slider, switch or other control.
The header view can respond to touches the same way any other view can. You can put buttons on it or you can override the UIResponder methods that handle touch events, specifically:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
add a bunch of labels and imageviews to it
Might want to be careful about that from a user interface design perspective. The effective touch area of a table header is rather small. You don't want it to have more than one or at most two components or possible actions. Otherwise, your users will need a stylus to do anything.
If you just need an image/text and to respond to taps, consider using a UIButton. It will provide you with all that functionality without subclassing.