UILabel not triggering touchesBegan - ios

i'm currently struggling to make touchesBegan work.
I currently have this setup
[UIView(UIViewController) -> UIScrollView -> UIView[holderView] -> UILabels[]]
I add programmatically my UILabels this way
//UIViewController method
UILabel *etiquetaCantidad = [[UILabel alloc] initWithFrame:CGRectMake(350, idx * 35, 50, 30)];
[etiquetaCantidad setTextAlignment:NSTextAlignmentCenter];
[etiquetaCantidad setBackgroundColor:[UIColor azulBase]];
[etiquetaCantidad setTextColor:[UIColor whiteColor]];
[etiquetaCantidad.layer setCornerRadius:5];
[etiquetaCantidad setText:#"0"];
[etiquetaCantidad setUserInteractionEnabled:YES];
[etiquetaCantidad setTag:idx + 100];
[holderView addSubview:etiquetaCantidad];
But when i try
// UIViewController
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"Touch realizado: %#", touches);
}
It's not being triggered, what i've missed here???

Well, after struggling with my problem, the issue was caused by the UIScrollView that lies between my UILabels and my UIViewController
So i've implemented a category for UIScrollView that passes touches to it's super or nextResponder
#import "UIScrollView+TouchesBeganPropagable.h"
#implementation UIScrollView (TouchesBeganPropagable)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if(!self.dragging){
[self.nextResponder touchesBegan:touches withEvent:event];
}else{
[super touchesBegan:touches withEvent:event];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if (!self.dragging){
[self.nextResponder touchesMoved: touches withEvent:event];
}
else{
[super touchesMoved: touches withEvent: event];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if (!self.dragging){
[self.nextResponder touchesEnded: touches withEvent:event];
}
else{
[super touchesEnded: touches withEvent: event];
}
}
#end
That way, i can use touchesXXXX methods on my UIViewController that interact with any UIView no matter if an UIScrollView is in the middle

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

Can I call - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event on subview

I recognize touches in my view - if certain conditions happen I would like to call my subview with this method but it isn't working - (if I don't override hittestWithEvent - this view would have been returned)
How can I do it?
Edit:
I want the original SUBVIEW to get the "tap" so I tried to do this:
-(void)didTap:(MyTapGesture *)tap
{
// the original view that would have been returned by hit test. could be any arbitrary view
UIView *theView = [super hitTest:[tap locationInView:self] withEvent:nil];
NSSet *touchesBegan = tap.touchesBegan;
NSSet *touchesEnded = tap.touchesEnded;
UIEvent *eventBegan = tap.eventBegan;
UIEvent *eventEnd = tap.eventEnd;
NSSet *tbegan = [eventBegan touchesForView:theView];
NSSet *tend = [eventEnd touchesForView:theView];
// try to simulate the touch in the subview - not working
[theView touchesBegan:tbegan withEvent:eventBegan];
[theView touchesEnded:tend withEvent:eventEnd];
}
#interface MyTapGesture : UITapGestureRecognizer
#property (nonatomic,strong) NSSet *touchesBegan;
#property (nonatomic,strong) NSSet *touchesEnded;
#property (nonatomic,strong) UIEvent *eventBegan;
#property (nonatomic,strong) UIEvent *eventEnd;
#end
#implementation MyTapGesture
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchesBegan = touches;
self.eventBegan = event;
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchesEnded = touches;
self.eventEnd = event;
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
}
#end
You can overwrite touchesBegan:withEvent on your main view and it the condition happen call it on your subview:
//In your view
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (CONDITION)
{
//call your subview
[yourSubview touchesBegan:touches withEvent:event];
}
}
Hope this is what you are after.
//Extended
You can try add a method to your subview which is called when the condition is meet:
//In your subview
-(void)myMethod:(NSSet *)touches
{
//Your code
}
And call it in your view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (CONDITION)
{
//call your subview
[yourSubview myMethod:touches];
}
}

UIButton is not receiving actions behind UIScrollView

I put UIButton inside UITableViewCell in UITableView that is behind UIScrollView. I subclassed UIScrollView to forward touches to UITableView.
So method from UITableViewDelegate didSelectRow is calling properly. The problem is that UIButton inside table cell is not receiving TouchUpInside actions.
How can I solve this problem without deleting ScrollView over TableView?
EDIT:
I resolved this issue by detecting which view will receive touch. Here's the code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView *hitView = [self hitTest:[(UITouch*)[[touches allObjects] objectAtIndex:0] locationInView:self] withEvent:event];
if ([hitView isKindOfClass:[UIButton class]]) {
[(UIButton*)hitView sendActionsForControlEvents:UIControlEventTouchUpInside];
}
[super touchesBegan:touches withEvent:event];
}
If you want to enable actions for bouth objects - UIScrollView and UIButton you should to implement custom hit test mechanism for ScrollView.
In your UIScrollView subclass override - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event method to make views behind ScrollView available for getting events.
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return __touchesEnabled;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
_touchesEnabled = NO;
UIWindow *window = [UIApplication sharedApplication].delegate.window;
for(UITouch *touch in touches) {
CGPoint point = [touch locationInView:self];
point = [window convertPoint:point fromView:self];
UIView *view = [window hitTest:point withEvent:event];
[view touchesBegan:touches withEvent:event];
}
_touchesEnabled = YES;
}
It works for me
Since you have added your scroll view over the UIButton, all the touch actions will not be passed to the button.
[yourScrollView setUserInteractionEnabled:NO];
This may solve your problem.

Forwarding Touches From UIScrollView - UIButton Issues

I have been trying to figure out how to forward the touches from a UIScrollView to its superview. The reason is for sort of a cursor to follow the touch. Anyway, the scrollview is populated with UIButtons. My code for forwarding the touch is to use delegation in a scrollview subclass:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
if ([[self tfDelegate]respondsToSelector:#selector(tfDelegateBegan:)]) {
[[self tfDelegate]tfDelegateBegan:[touches anyObject]];
}
NSLog(#"touches began");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesMoved:touches withEvent:event];
if ([[self tfDelegate]respondsToSelector:#selector(tfDelegateMoved:)]) {
[[self tfDelegate]tfDelegateMoved:[touches anyObject]];
}
NSLog(#"touches moved");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesEnded:touches withEvent:event];
if ([[self tfDelegate]respondsToSelector:#selector(tfDelegateEnded:)]) {
[[self tfDelegate]tfDelegateEnded:[touches anyObject]];
}
NSLog(#"touches ended");
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesCancelled:touches withEvent:event];
if ([[self tfDelegate]respondsToSelector:#selector(tfDelegateCancelled:)]) {
[[self tfDelegate]tfDelegateCancelled:[touches anyObject]];
}
NSLog(#"touches cancelled");
}
However, I have learned that UIScrollviews operate via UIGestureRecognizers, so these methods aren't even called by default. I realize that the gesture recognizers are exposed in iOS 5 but I need to support 4.0 as well. I did this instead:
NSArray *a = [[theScrollview gestureRecognizers]retain];
for (UIGestureRecognizer *rec in a) {
if ([rec isKindOfClass:[UIPanGestureRecognizer class]]) {
NSLog(#"this is the pan gesture");
rec.cancelsTouchesInView = NO;
}
}
This allows the gesture to work and the touches methods to be called simultaneously. The issue is, now if you try to scroll while touching a button, the button can be pressed while scrolling. Normally, the scroll cancels the button and the only time a button can be pressed is if the scrollview is not scrolling.
This is the desired functionality. Any suggestions on how I might achieve this?
Maybe try controlling the button action using a flag that would prevent the event from firing if the scroll view is scrolling.
BOOL isScrolling = NO;
- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
isScrolling = YES;
}
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
isScrolling = NO;
}
- (void) didTapButton {
if(isScrolling)
return;
//Do Button Stuff
}

Detecting Tap & Hold in UITableView cells

How do we detect a tap & hold on a UITableViewCell?
In iOS 3.2 or later you can use UILongPressGestureRecognizer
Here's the code lifted straight from my app. You should add these methods (and a boolean _cancelTouches member) to a class you derive from UITableViewCell.
-(void) tapNHoldFired {
self->_cancelTouches = YES;
// DO WHATEVER YOU LIKE HERE!!!
}
-(void) cancelTapNHold {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(tapNHoldFired) object:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self->_cancelTouches = NO;
[super touchesBegan:touches withEvent:event];
[self performSelector:#selector(tapNHoldFired) withObject:nil afterDelay:.7];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self cancelTapNHold];
if (self->_cancelTouches)
return;
[super touchesEnded:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self cancelTapNHold];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self cancelTapNHold];
[super touchesCancelled:touches withEvent:event];
}
//Add gesture to a method where the view is being created. In this example long tap is added to tile (a subclass of UIView):
// Add long tap for the main tiles
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longTap:)];
[tile addGestureRecognizer:longPressGesture];
[longPressGesture release];
-(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer{
NSLog(#"gestureRecognizer= %#",gestureRecognizer);
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
NSLog(#"longTap began");
}
}
You should probably handle the UIControlTouchDown event and depending on what you mean by "hold", fire a NSTimer that will count an interval since you initiated the touch and invalidate upon firing or releasing the touch (UIControlTouchUpInside and UIControlTouchUpOutside events). When the timer fires, you have your "tap & hold" detected.

Resources