How to enable touch began in UIScrollView? - iphone

Am using UIScrollview, when i am trying to capture touch event in UIscrollview is not responding. how should i achieve it? i am using UILable for showing the text.

Yes I was also facing the same problem.But finally I got remedy on it.
Solution is that you will have to create a class inherited from UIScrollview as follows:
#import <Foundation/Foundation.h>
#interface CustomScrollView : UIScrollView
{
}
#end
And override the touches method in its implementation file as follows:
#import "CustomScrollView.h"
#implementation CustomScrollView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if(!self.dragging){
[self.nextResponder touchesMoved:touches withEvent:event];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self.nextResponder touchesEnded:touches withEvent:event];
}
#end
You will have to import this class in the required file and use its object instead of UIScrollview"s object.
Now you can see that control reaches to touches methods in the class in which you have added scrollview whenever you touch on it.
Thanks.

As of iOS 3.2 UIScrollView does not listen to touchesBegan:, touchesMoved:, touchesEnded:, etc. anymore, afaik.
You should add Gesture Recognizers instead to handle touch events in your UIScrollView.
http://developer.apple.com/iphone/library/documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html
But if you want to handle the touches in one of the UIScrollView's subviews, it would be best to read the Overview section in the UIScrollView documentation. See touchesShouldBegin:withEvent:inContentView:, pagingEnabled, and delaysContentTouches, among others.
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html

"split" writes about a solution here that I used in my project to be able to access touches in a scroll view.

No need to go anywhere simple solution is here. Really as simple as you think.

Related

why does this touchesBegan method code get trigger in my UITableView?

Why does the touchesBegan method code get trigger in my UITableView?
Background: I have a UITableView within a UINavigationController. In the UITableView I have custom cells based on subclassing UITableViewCell. I current don't have a selectRowAtIndexPath method in my UITableView.
Some aspects of the answer that would be good to cover off would include:
why it doesn't work obviously
in terms of getting it to work, how does get one ensure that the correct touch (single-tap) detection will not prevent the other scenarios to work like: selecting a table row, scrolling the tableview contents up and down etc.
should the simulator be able to pick up touches etc, or are there cases here where you need the physical device
Code:
#interface AppointmentListController : UITableViewController <UIActionSheetDelegate>
.
.
.
#implementation AppointmentListController
.
.
.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
timeStampStart = event.timestamp;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSTimeInterval timeStampEnd = event.timestamp;
NSTimeInterval touchDuration = timeStampEnd - timeStampStart;
if(touchDuration > 0.2)
[super touchesEnded:touches withEvent:event];
else
DLog(#" - TOUCH EVENT OCCURS");
[super touchesEnded:touches withEvent:event];
}
Wanted to put up this as a possible answer, the answer being there is no relative straight way to achieve what I've asked for this. That is a way for a tap or double-tap to be detected in a UITableView page, which is already picking up row touches and scroll up/down etc.
Haven't verified whether this is the case or not, but people could up-vote this answer if they believe this to be true.

how to "bypass" touch events in Cocoa-touch?

I want to make a transparent mask-view over the current window, which just tracks touch events and passing them to the visible views below. However if I set userInteractionEnabled=YES to this mask, this blocks the events and won't be passed below.
Is there any way that I can prevent this view from blocking the events, or manually passing the events below?
Thanks,
I just recently did this for one of my apps and it turned out to be quite simple.
Get ready to subclass UIView:
I called my Mask View the catcher view and this is how the protocol looks:
#interface CatcherView : UIView {
UIView *viewBelow;
}
#property(nonatomic,retain)UIView *viewBelow;
#end
Here you are just subclassing UIView AND keeping a reference to the view bellow.
On the implementation you need to fully implement at least 4 methods to pass the touches to the view, or views bellow, this is how the methods look:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Began");
[self.viewBelow touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Moved");
[self.viewBelow touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Ended");
[self.viewBelow touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Cancelled");
//Not necessary for my app but you just need to forward it to your view bellow.
}
Just remember to set the view or views that are bellow when you create the view; it is also very important to set the background color to clear, so it acts as a mask. THis is how that looks:
CatcherView *catchView=[[CatcherView alloc] initWithFrame:[self.view bounds]];
catchView.backgroundColor=[UIColor clearColor];
catchView.viewBelow=myViewBellow;
[self.view addSubview:catchView];
Hope it helps and comment if you need more info.
UIKit determines the target view for an event by sending -hitTest:withEvent: messages down the responder chain
Once the target has been found, the event is sent up the responder chain until a responder that handles it is found (often the view that was touched, but not always)
Thus, if you override -[NSView hitTest:withEvent:] in a suitably high up view (perhaps by using a custom window!) you can note all incoming events and call super to have them behave as normal.

iPhone - how to track touches and allow button taps at the same time?

I'm wondering how to track touches anywhere on the iPhone screen and still have UIButtons respond to taps.
I subclassed a UIView, made it full screen and the highest view in the hierarchy, and overrode its pointInside:withEvent method. If I return YES, I'm able to track touches anywhere on the screen but the buttons don't respond (likely because the view is instructed to handle and terminate the touch). If I return NO, the touch passes through the view and the buttons respond, but I'm not able to track touches.
Do I need to subclass UIButton or is this possible through the responder chain? What am I doing wrong?
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
return NO;
}
//only works if pointInside:withEvent: returns YES.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"began");
[self.nextResponder touchesBegan:touches withEvent:event];
}
//only works if pointInside:withEvent: returns YES.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"end");
[self.nextResponder touchesEnded:touches withEvent:event];
}
Rather then having extra view you can subclass your applications main window and track touches (and other events in it)
#interface MyAppWindow : UIWindow
...
#implementation MyAppWindow
- (void)sendEvent:(UIEvent*)event {
[super sendEvent:event];
if (event.type == UIEventTypeTouches){
// Do something here
}
return;
}
Then set your application window type to MyAppWindow (I did that in MainWindow.xib in IB)

UIScrollview+UItextview touch event

i am using a scroll view and that scroll view i have UIIMageview and UITextview.My Question is -i want the Touchevent of Textview.
I think UIScrollView becomes the first responder of touch events, and it doesn't pass the touch events to other view as a default.So, why don't you create a subclass of the UIScrollView? Then, pass the all touch events which are received by the subclass to the UIIMageview or UITextview like as follows.
#implementation SubclassOfScrollView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.nextResponder touchesBegan:touches withEvent:event];
}
#end

Make a UIScrollView get touch events with a UITextView as a subview

I have a UIScrollView that contains a UITextView (not editable).
I can't make the UIScrollView gets the touch events, UITextView seems to get them and keep them . Any idea how to let UIScrollView gets the touch events?
I want UITextView to still be scrollable vertically (my UIScrollView is scrollable only horizontally).
In your UITextView subclass, do this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.nextResponder touchesBegan:touches withEvent:event];
}
If you want UITextView to handle the touches too, then I believe you can do this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.nextResponder touchesBegan:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
}
but it might result in really weird behavior.