How To Add One UIGestureRecognizer to All UITextView without blocking textView touches - ios

How To Add One UIGestureRecognizer to All UITextView without blocking textView touches..
i want to call a method when any one touch any UITextView. pls any one give me ans.

<UITextFieldDelegate>
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

Just use UITextFieldDelegate methods or add a UIView under the UITextField and add gesture recognizer to UIView

first u must add uitextfield delegate
<UITextFieldDelegate>
then use that method
(BOOL)textFieldShouldBeginEditing:(UITextField *) textField

Try like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"touchesBegan");
}
Then you Handle following method in above action
[super touchesBegan:touches withEvent:event];
Your total solution is Like this this way:
#import "YourTextView.h"
#implementation YourTextView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
NSLog(#"touchesBegan");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"touchesEnded");
[self.nextResponder touchesEnded: touches withEvent:event];
NSLog(#"touchesEnded");
[super touchesEnded:touches withEvent:event];
NSLog(#"touchesEnded");
}
I hope this is helpful for you.. :)

use this code this is delegate
<UITextFieldDelegate>
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

Related

duplicate/mirror touch events from uiview to uiscrollview

i have a uiwebview and an uiview next to each other everone taking 50% of the screen width.
i can smoothly scroll the uiwebview - and i want to be able that if i scroll up/down on the uiview - that the uiwebview scrolls in the given direction.
already subclassed uiview - and trie'd to send the touchesEnded... events to the webview.scrollview.
.h
#interface UIViewScrollDispatcher : UIView
#property(nonatomic, assign) id scroller;
.m
#implementation UIViewScrollDispatcher
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self.scroller touchesCancelled:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.scroller touchesEnded:touches withEvent:event];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.scroller touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"TOUCHHHH %#", self.scroller);
[self.scroller touchesMoved:touches withEvent:event];
}
using it like this:
UIViewScrollDispatcher * rightarea = [[UIViewScrollDispatcher alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
rightarea.backgroundColor=[UIColor redColor];
rightarea.scroller=self.webView.scrollView;
tried it with self.webView and self.webView.scrollView
doesn't work - any help?
btw: the NSLog inside the touchesMoved gets called
regards
ok - did it with uiscrollview subclass and sync the .contentOffset and .contentSize

How to detect touchesBegan, touchesMoved, touchesEnded events from another class

I need to use touch events but i am trying to detect it from another class, so i will call back to my main class if touched or moved etc.
I am calling init method in "classTouchEvents" firstly from my main class like that
objectTouchEvents=[[classTouchEvents alloc]initWith:self.view];
here is initWith method of "classTouchEvents"
- (id)initWith:(UIView *)selfView
{
self = [super init];
if (self) {
NSLog(#"here");
view=[[UIView alloc]init];
view=selfView;
// Custom initialization
}
return self;
}
and i get "here" log so i guess it is working but i cant detect if view get touch or anything. Here is my touch events in classTouchEvents
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchBegan");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchMoved");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchEnded");
}
But i cant get any log about touches. I need some help.
Thanks in advance.
I'd use delegation:
TouchDelegate.h:
#import <UIKit/UIKit.h>
#protocol TouchDelegate <NSObject>
#optional
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
#end
And then in your view (that actually receives the touch event), create a touchDelegate property (or just delegate if there is unlikely to be others):
#import "TouchDelegate.h"
#interface MyView : UIView
#property (weak, nonatomic) id<TouchDelegate> touchDelegate;
...
#end
and then delegate as normal:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([_touchDelegate respondsToSelector:#selector(touchesBegan:withEvent:)]) {
[_touchDelegate touchesBegain:touches withEvent:event];
}
}
// etc.
I would implement a custom subclass of UIView with a delegate.
#protocol TouchEventsDelegate <NSObject>
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event inView:(UIView *)view;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event inView:(UIView *)view;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event inView:(UIView *)view;
#end
#property (weak, nonatomic) id<TouchEventsDelegate> delegate;
Implement these delegate methods in your ClassTouchEvents.
Implement the touch events method in your custom view and call the delegate.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.delegate && [self.delegate respondsToSelector:#selector(touchesBegan:withEventinView)]) {
[self.delegate touchesBegan:touches withEvent:event inView:self];
}
}

How to know which view the user touches on screen

If there is a uiscrollview and and multiple sub view on the uiscrollview. how to know where the user touches i.e on specific view or scrollview(blank space)
Use this approach:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSLog(#"View touched: %#", touch.view);
}
This method is called every time your finger touches the screen, and the touch knows which view it touched.
Edit: It won't work on a UIScrollView because the scroll view gets the touch itself, check this:
touchesBegan method not called when scrolling in UIScrollView
How to enable touch began in UIScrollView?
You have 2 possibilities for detect the touch with - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; in a scrollView:
first, implement this method in your viewController, and add the scrollview on this viewController.
second, which I recommend: make a custom class which is inherited from UIScrollView like this:
.h:
#interface CustomScrollView : UIScrollView
#end
.m:
#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
in your vc make:
...
CustomScrollView* customScrollView = [[CustomScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSLog(#"View touched: %#", touch.view);
}

UIbutton taking time to receive down event in a UISCrollView on iOS5

I have the following hierarchy:
- UIView 1
- UIScrollView 2
- UIView 3
- UIView 4
- UIButton 5
My problem is that the touch down on the UIButton requires me to press for what seems a long time (like a second) to be registered by the UIButton.
The way this hierarchy is created : UIView1 is loaded from a nib file with 2 and 3 embedded but 4 is created from another nib file, first placed in one view not depicted here and then brought in 3 using addsubview. (I don't know if this is relevant).
Does somebody have any idea on how to fix this delay ? The problem appears on my 4s with ios5.1 and not in the simulator with iOS 6.
Try to set UIScrollView delaysContentTouches property to NO.
OR
Try using [button performSelector:#selector(buttonClickMethod:) afterDelay:0.0];
#interface CustomScrollView : UIScrollView {
}
#end
#implementation CustomScrollView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
return self;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self superview] touchesEnded:touches withEvent:event];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self superview] touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[[self superview] touchesMoved:touches withEvent:event];
}
- (void)dealloc {
[super dealloc];
}
#end
Use customscrollview Instead of uiscrollview

Detect multitouch in a custom UIViewController contains a UITableView subview

I have a UITableView as a subview in the UIViewController's view. I understand that I can detect the touch event by overwritting
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
}
But it never detected these events in the ViewController if I touched the tableview. As suggested in this forum by others, I should had a custom UITableView and added the above lines in it. I did that, but still the viewController did not detect the touch event. Any suggestions? Thanks.
After a few days of Googling, I finally found the solution that works for me. The key is to pass the responder chain to the next responder. For instance, in the touch began case, add the following line.
[self.nextResponder touchesBegan:touches withEvent:event]

Resources