Method didn't work in UIScrollView - iphone

hello
i put some uitextfield in uiscrollview,and connect them in interface builder to event "touch down" to a method,the problem his that when the uitexttfield is inside uiscrollview the method doesn't called and when the textfield is not in the uiscroll view the method doesn't called.

UIScrollView is special because of all the touch events it needs to process itself. Try sub-classing UIScrollView and add:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"you should see this for touch events on your scroll view");
}
set the class name in IB to your UIScrollView subclass.

Related

Passing scroll gesture to UIScrollView from another view

I do have a view that has a UIScrollView and over it there is a view that display some text.
When the user swipes over this view that contains text the UIScrollView won't scroll. How to make this view transparent in a way it relays the swipe gesture to UIScrollView.
Thanks
You can just set
myTextView.userInteractionEnabled = NO;
Or, if you're creating your view with Interface Builder, there's a check box there called 'User interaction enabled', just uncheck that.
Check out the UIView hitTest Method
Returns the farthest descendant of the receiver in the view hierarchy (including itself) that contains a specified point.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
Inside the -touchesXXXX:withEvent: methods of your custom view, call their super methods to forward touch events.
For example:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// forward touchesBegan to super class
[super touchesBegan:touches withEvent:event];
...
// process touchesBegan for this view
}
Do the same things for touchesMoved, touchesEnded, and touchesCancelled.

pass scroll event from uibutton to uiscrollview

i have horizontal UIScrollView which is extended from UIScrollView and i added UIButtons horizontally. i can only scroll out of the buttons area, but if i want to scroll over any buttons is fires UIControlEventTouchUpInside event. I don't want this. i want to fire UIControlEventTouchUpInside action if i click and i want to scroll if i scroll.
so how can i pass scroll event from UIButton to UIScrollView?
Subclass UIScrollView, return YES in the - touchesShouldCancelInContentView: method.
that functionality is already built in. When you have a UIControl element as a subview of a scroll view and a touch event is detected, it is initially passed to the UIScrollView. IF, after a moment or two there hasn't been sufficient movement in the touch event, it gets passed on to the button.
If you subclass NSButton and make your button of that type and override the following in your subclass you can pass the events back to the parent view, in your case the scroll view
The following would make the button never trigger an event and instead all would be dealt with by the parent view:
- (void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event {
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
[self.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event
{
[self.nextResponder touchesEnded:touches withEvent:event];
}
If you want to have the button deal with one of those events instead use
[super touchesEnded:touches withEvent:event];
Try this:
self.button.exclusiveTouch = YES
Worked like a charm for me!

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

Parent responds to child's touch

I have a UIScrollView with subclassed UIImageViews in it. When one of the imageviews are clicked, I'd like the parent UIView (contains the scrollview) to receive the event in its
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
I can tell if the imageview was clicked by
if(CGRectContainsPoint(myScrollView.frame, location) == YES){...}
Assuming the imageview and scrollview are the same size. Or is there a better way to to know a particular imageview within the scrollview was tapped? The imageviews are created dynamically.
The approach I've used for this in the past is to fire off a notification when a subview has been touched, with the subview as the object posted with the notification. You can then have your container view listen for the appropriate notification, and perform whatever action is needed based on the UIView instance that was selected.
You'd post the notification within the subview's -touchesEnded:withEvent: method as follows:
[[NSNotificationCenter defaultCenter] postNotificationName:#"MySubviewWasTouched" object:self];
To set up listening for this notification, you'd place code like the following somewhere in the initialization of your view or view controller:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleSubviewTouch:) name:#"MySubviewWasTouched" object:nil];
remembering to remove the observer in your teardown code for the the view or controller.
Finally, you'd need to implement the method that processes the receipt of the notification (-handleSubviewTouch: in the example above):
- (void)handleSubviewTouch:(NSNotification *)note;
{
UIView *subviewThatWasTouched = [note object];
// Your touch-handling logic here
}
Note that UIImageView ignores touches (or rather, lets them pass through to its superview) by default. Make sure you set your image views' userInteractionEnabled property to YES.
If the image views don't have any children, you can use the UIView method hitTest:withEvent:. It returns the farthest descendent of a UIView that contains a given touched point. So for example:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint location = [[touches anyObject] locationInView:myScrollView];
UIView *touchedView = [myScrollView hitTest:location withEvent:event];
// ...
}

How to catch touchBegan by other subviews such us UIScrollView

I have a UIScrollView in my view control but my UIScrollView can't catch the touchBegan. touchBegan only execute when touching outside of the UIScrollView. How can catch the touchBegan when touching UIScrollView?
Thanks.
You must subclass UIScrollView (or an other view) and re-implement the methods you want to catch. Don't forget to call super in you implementation!
In order to subclass UIScrollView, your MyUIScrollView.h file should look similar to this:
#import <UIKit/UIKit.h>
#interface MyUIScrollView : UIScrollView {
}
#end
Then, in your MyUIScrollView.m, you place your touchesBegan method:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
After you do this, anywhere in your code where you now use an UIScrollView, you switch to using a MyUIScrollView.