get hold of UIResponder instance methods on UIScrollView. iPhone - iphone

on the current project I am working on, on one of the views, I have a UIView and on top of it UIScrollView which is constructed from the interface builder.
I have implemented the following UIResponder instance methods:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
but the control comes to this methods only when touch event occur in area where is not covered by the UIScrollView.
how I can get the hold of a touch event on the UIScrollView also?

you could create a subclass of UIScrollView and override:
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
you have to be careful that you don't interfere with the scrolling gestures etc tho.

Related

touchesBegan - Only needs the last touch action - iPhone

In
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
method I am doing some actions and in
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
method I'm doing some other action.
If I'm tapping 3 times the touchesBegan will call 3 times. What I want is if the user continuously taps only the last touch action should perform. Is it possible or not?
You can try a UITapGestureRecognizer instead, and disable it after the first tap.
Otherwise, you can use performSelector:withObject:afterDelay: with a short delay in touchesEnded:withEvent: and cancelPreviousPerformRequestsWithTarget:selector:object: in touchesBegan:withEvent:.
Does that make sense?

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.

Multitouch not working in cocos2D for iPhone

I am quite familiar with cocos2D (been working with it for over 1 year) and I encountered a problem with my multitouch game - the multitouch.
What I have:
[glView setMultipleTouchEnabled:YES] in my delegate.
A playscene that does NOT implement touches.
Many objects (of my own, inherits CCLayer) that are added to playscene.
These objects implement touches and are delegates of the targeted protocol.
[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
The very same architecture is used in the touches test from cocos2D and it's working.
The only difference is that they use CCSprite instead of CCLayer as the parent class for the objects.
Does anyone have any ideas?
I do it this way:
in app delegate:
[glView setMultipleTouchEnabled:YES]
then in you game you see the touches like this:
self.isTouchEnabled = YES;
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {}
With this I sold my problem I hope it helps
Well, you will never guess! Of course I knew XCode is not the best IDE in the world and that sometimes you should clean the project and rebuild...but I never thought to do a new project and import everything in there. That was the problem!!! How f**ked up...I thought I only had to code... After that , everything worked ok.

how to make uiview movable?

I am developing a simple game photo puzzle. I have 9 pieces of an image. The image parts are placed on different UIView. Now I want to know how can I move the uiview parts on a touch ?
Thanks...
check this
you need to handle
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {}
events

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.