iOS: UITableView Selective Touches - objective-c

I have a UITableView that has certain cells that are a different height. I was hoping to have a way, without adding a button to each cell, to not receive responses to didSelectedRowAtIndexPath, if the touches in the cell are below a certain point.
As an example, say I have two cells, one that has a height of 100 and another with a height of 150. Is there a way to not receive responses to didSelectRowAtIndexPath: for touches below 100, but still receive the touch input on the cell?
I thought of overriding the touches methods in the cell and returning the point of contact in a delegate to the controller/table and using that input to determine whether or not I will ignore the didSelectRowAtIndexPath: response, but I'm worried about the highlighting and other calls and whether it may not be fast enough to block those as well. It just seems really iffy to me.

I think without subclassing the cell, it isn't possible.
My suggestion would be to do this:
Create a Subclass of the cells
Add an extra boolean iVar to the cells, like didTapOutside
Set the boolean to True or False, based ont he Y position tap in the touchesBegan method
In the didSelectRowAtIndexPath read the didTapOutside boolean variable and according to it's setting do your actions
Example follows:
#interface MyCustomCell : UITableViewCell {
BOOL didTapOutside;
}
#property (atomic, readwrite) BOOL didTapOutside;
And the implementation:
#implementation MyCustomCell
#synthesize didTapOutside;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:touch.view];
didTapOutside = (location.y>100);
[super touchesBegan:touches withEvent:event];
}

You can create a custom UITableViewCell for example:
CustomCell *cell = [[CustomCell alloc] initWithFrame:rect reuseIdentifier:identifier];
You could override the touchesBegan method in the CustomCell and do something like the following to get the position of the touch:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
CGPoint point = [aTouch locationInView:self];
// point.x and point.y have the coordinates of the touch
// based on where the touch is... your custom code
[super touchesBegan:touches withEvent:event];
}

Sorry i read the question wrong at first. There is no easy solution for this.
If you do not want to use a sub class, the only thing you can do is add a transparent view or button on the area you want to detect touch and create an event on that instead of using the table view cell methods.

Related

UISlider change values by touching its track

I am beginner in ios and I have done basic functionality with UISlider , changing its value on dragging thumb. But now in my app I want to move the thumb of slider gradually in same direction where I am touching slider's track without dragging the thumb of slider. Thumb must reach to exact touched position on the track in 2 or more steps.
I know it requires involvement of UITouch but unable to understand HOW?
You need to subclass UISlider to add this functionality. After subclassing, you need to implement touchesBegan and touchedMoved in your subclass. Well, here how i did
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGFloat maxX = CGRectGetMaxX(self.frame);
CGFloat minX = CGRectGetMinX(self.frame);
int value = (maxX - minX) /(self.maximumValue - self.minimumValue);
[self setValue:([touch locationInView:self].x - minX) / value animated:YES];
[self beginTrackingWithTouch:touch withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self continueTrackingWithTouch:[touches anyObject] withEvent:event];
}
You need more work to stabilise a little bit more when user first tap, but i hope you get the idea.

How to identify a button touch event from other touch events

I have an app in which the user can interact with many objects. These are several UIButtons, a few UILabels and a lot of UIImageViews.
The focus of all interaction centers around touching the UIImageView objects. With a touch I can move the images around, tell them to do this or that. However, my current obstacle is in knowing how to properly have the app distinguish touches that occur when I touch a UIButton.
Why? Well the logic within the Touches Began event is meant to be only for the UIImageViews, however the moment that I touch a button, or any other object, the app is interpreting the touch as if it occurred for a UIImageView object.
So, my approach boils down to: is there a good way of identifying if a touch occurred for a UIButton, UIImageView, UILabel object? That way I can filter out the irrelevant touches in my app from the relevant ones.
EDIT:
The code below outlines how I capture the touch event, however I do not know how to know from the touch event whether it is a button or a view that I touched.
touch = [touches anyObject];
touchLocation = [touch locationInView:[self view]];
To know whether UIButton is pressed follow this:
-(void) touchBegan : (NSSet *) touches withEvent : (UIEvent *) even
{
UITouch *touch = [touched anyObject];
UIView *touchedView = [touch view];
if([touchedView isMemberofClass : [UIButton class])
{
//do something when button is touched
}
}
Call hitTest:withEvent: on the view with the touch event to get the view that is actually being touched. Then you can use isKindOfClass: to check what type of view it is and respond accordingly.
you can use this method :-
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {
//You clicked inside the object
}
return [super hitTest:point withEvent:event]
}
and wain has already given you the explanation for it..
https://stackoverflow.com/a/18051856/1865424

Dragging UIImages to the selected area

Newbie obj-c question.
My task is to make visually represented test for iPad. In view I have three UIImages (with images of answers to test question) what will dragged to area for answer. If selected right image, then it needs to stay in this area, if not - it goes back into the start position. If user stop dragging not in area for answer it also needs to goes back to the start position.
I tried to implement this: http://www.cocoacontrols.com/platforms/ios/controls/tkdragview but it's too hard for me because I am a super newbie.
I implemented simple dragging of images by PanRecognizer, so every from three images dragging
-(IBAction)controlPan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
If I think in right way, I need to set CGPoint's with start and destination coordinates and then to customize Pan Gestures method? If it's not right, in what way can I do this?
You can consider this three method, maybe these are easier for you:
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
Detailed demo for – touchesBegan:withEvent: is:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
UITouch *touched = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touched.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
UITouch *touched = [[event allTouches] anyObject];
// Here I suppose self.imageView contains the image user is dragging.
CGPoint location = [touch locationInView:touched.view];
// Here the check condition is the image user dragging is absolutely in the answer area.
if (CGRectContainsRect(self.answerArea.frame, self.imageView.frame)) {
// set transition to another UIViewController
} else {
self.imageView.center = location; // Here I suppose you just move the image with user's finger.
}
}
Here is the Apple's doc for this UIResponder Class Reference

Can I detect if user has touched anywhere except from Annotation, on the map?

I am building an app and I would like to know when user has touched an annotation or anywhere else on the map. I have a button that I would like to display only if an Annotation has been selected. So, if user after annotation try to touch anywhere on the map (if is not another annotation) then make button invisible.
At the moment, I have tried the touchesEnded method but the problem is that it does not recognises annotations and land.
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if([touches isMemberOfClass:[BuildingViewController class]])
printf("Building");
else
printf("Land!");
}
Thanks in advance.
touches is object with class NSSet, so you can get object from NSSet and check it`s class membership. For example:
UITouch *touch = [touches anyObject];
You can get UIView from touch
UIView *touchedView = touch.view;
Then check this UIView class and compare it with yours
[touchedView isMemberOfClass:[BuildingView class]]
Also I advice you to check all touches in that NSSet.
BOOL isBuilding = NO;
for(UITouch *touch in touches){
if([touch.view isMemberOfClass:[BuildingView class]]){
isBuilding = YES;
break;
}
}
if(isBuilding){
printf("Building");
}else{
printf("Land!");
}

touches in touch event handling in UIView vs UIViewControllers

So, I'm capturing multiple touches and determining the number and where each of the touches occurred and I'm seeing different behavior between a UIView UIViewController.
Basically, the event handler is coded below: (in this case the touchesEnded, but it really doesn't matter). What happens in the View, is that I get the entire collection of touches, but in the controller, I get only one touch at a time, that is there is never a collection of touches, but the TouchesEnded gets executed for each touch that occurred in the same moment.
I add the subviews as identically as possible...
Can someone 'spain to me what's going on? Why doesn't the controller version give me the collection?
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
int total = 0;
for (UITouch *touch in touches)
{
for(UIView *myView in [contentView subviews])
{
if (CGRectContainsPoint([myView frame], [touch locationInView:self.view]))
total++; // this is always 1 for the UIViewController
// but I get the full number within the UIView version
}
}
}
I suspect you are getting the touches but that the views are different. Check that contentView is the same and has the same subviews.
I am not sure that I am right but this thing always work for me.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
If(touch.view==your view)
{
NSLog("do your task here");
}
}