I have a viewcontroller like the following. But the touchsBegan doestnt get detected.
Can anyone plz tell me what is wrong.
- (id)init
{
if (self = [super init])
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
return self;
}
-(void) viewWillAppear:(BOOL)animated
{
overlay = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"overlay.png"]] autorelease];
[self.view addSubview:overlay];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Detect touch anywhere
UITouch *touch = [touches anyObject];
// Where is the point touched
CGPoint point = [touch locationInView:self.view];
NSLog(#"pointx: %f pointy:%f", point.x, point.y);
// Was a tab touched, if so, which one...
if (CGRectContainsPoint(CGRectMake(1, 440, 106, 40), point))
NSLog(#"tab 1 touched");
else if (CGRectContainsPoint(CGRectMake(107, 440, 106, 40), point))
NSLog(#"tab 2 touched");
else if (CGRectContainsPoint(CGRectMake(214, 440, 106, 40), point))
NSLog(#"tab 3 touched");
}
Touches are only called on UIViews. This means you have to subclass UIView and put your touchesBegan:withEvent: code in this subclass. You're using the code inside an UIViewController and this doesn't get any touches itself because it's not an object on screen. A controller is just for the application logic behind a view.
As chriB said touches are detected only on views and as u access it in a controller its not detected.
also FYI, the touches wont be detected on any views you add over the base view.
ie: if u have a view and u add a scroll view or a table view over it, then the touches wont be detected over this added regions. same goes for buttons, textfields etc. Touch is detected only wen its done exactly on the base view.
Related
I have a UIView inside a UIScrollView, and the UIViewControllers for those views are not receiving the touch events. If I take the views out of the scroll view then it works.
UserInteraction is default ON of all views but it's still not working!
This must be possible and I'd be really grateful if someone could point me in the right direction!
Many Thanks,
You can use the following method
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[yourView addGestureRecognizer:panRecognizer];
And to handle it
-(void)move:(id)sender {
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged){
// This will return you location in view
CGPoint currentPoint = [sender locationInView:self.view];
// This will return you location in Scrollview
CGPoint scrollPoint = [sender locationInView:[[sender view] superview]];
}
}
add the following code to implementation file then touches moved
#interface UIScrollView(Custom)
#end
#implementation UIScrollView(Custom)
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
}
#end
I have UIScrollView with many UIImageViews.I need to drag and drop image from UIScrollView to another view. Outside the scrollView touch is worked. But inside scroll view touch is not worked. I used touchesBegan,touchesMoved etc method. Please help me.
-(IBAction)selectBut:(id)sender
{
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(x,y,w,h)];
scrollView.userInteractionEnabled = YES;
int y = 0;
for (int i = 0; i < [myArray count]; i++) {
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, y, 75, 30)];
image.userInteractionEnabled = YES;
y=y+35;
[scrollView addSubview:image];
}
[self.view addSubview:scrollView];
[scrollView setContentSize:CGSizeMake(150, 300)]
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 1) {
NSLog(#"One touch !!");
}
}
you need to customize the UIImageView with your own view inherited from UIImageView. Provide touch methods in that customized subclass and add it on your UIScrollView..
Subviews of UIScrollview will never call touchesBegan method directly. You need to customized with subview to get touchesBegan properties of that added subview/customized view.
I mean to say take subclass of ImageView like
CustomImageView *imageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[imageView setUserInteractionEnabled:YES];
[scrollView addSubview:imageView];
[imageView release];
CustomImageView class is should be inherited from UIImageView like
#interface CustomImageView : UIImageView
{
}
in # .m File
#import "CustomImageView.h"
#implementation CustomImageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"%s", __FUNCTION__);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImageView.image = nil;
return;
}
}
Add a Tap gesture recognizer to the scroll view for enabling touch inside scroll view:
UITapGestureRecognizer *singlTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(methodName:)];
singlTap.cancelsTouchesInView = NO; //Default value for cancelsTouchesInView is YES, which will prevent buttons to be clicked
[scrollViewName addGestureRecognizer:singlTap];
then write your code in specified method.
-(void)methodName:(UITapGestureRecognizer *)gesture
{
//your code here
}
I don't know if this help you. But try to set the exclusiveTouch property of the UIImageView to YES.
Did you add your scrollview as an IBOutlet? If its from xib and as you say touches are available outside the scroll and not within, I guess you might have accidentally unchecked userInteractionEnabled for the scrollview which inhibits touch events. And you have added UIImageViews as subviews to UIScrollView. For UIImageView you have to first set userInteractionEnabled to YES and then add any gestures if necessary to get events or use the touchesBegan, touchesMoved methods. Unless you set interaction enabled you wont receive touch events on UIImageView. If the parent view I mean the UIScrollView's interaction is disabled you wont get touches on UIImageView too. Hope this helps :)
I have added the view programmatically in the main view. And in the parent view i am adding imageview as a subview programmatically. Now I have set the touch event for imageview on click but its not working. It is not detecting the touch event on click. I am using the following code of touch event of imageview.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == originalphoto)
{
[fullview setHidden:NO];
[self.view addSubview:fullview];
[setimage setImage:image];
}
}
Do anyone has idea that how to set touch event of imageview which has been added programmatically?
Thanks alot.
Why dont you use a gesture? it is lot easier. You could use the UITapGestureRecognizer and let it work for you: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITapGestureRecognizer_Class/Reference/Reference.html
//Add gesture to your view
UITapGestureRecognizer *Tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(ResignResponder:)];
[tempV addGestureRecognizer:Tap];
[Tap release];
//method will be called on tapping the view
-(void)ResignResponder:(UIGestureRecognizer*)recognizer {
[LocSearchField resignFirstResponder];
[self.view sendSubviewToBack:tempV];
}
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:UIIMAGEVIEW];// uiimageview object
touchx = location.x;
touchy = location.y;
NSLog(#" x= %i ", touchx);
NSLog(#"y = %i",touchy);
Maybe you need to set property imageView.userInteractionEnabled = YES?
I've got a NavCtrl and on top of that I have a TableView/ctrl, then I have a scrollview.
I've got everything working, except I can't seem to get touches to hit the Cell's on the table view.
I've tried the nextResponder things and everything seems to be doing the right thing. Here's some snippets of code, keep in mind this code has been changed a lot for testing so it isn't "clean" yet :):
ScrollView *myScrollView = [[ScrollView alloc] initWithFrame:CGRectMake(0, 150, 320, 680)];
myScrollView.contentSize = CGSizeMake( 320 , 888);
myScrollView.pagingEnabled = NO;
myScrollView.bounces = NO;
myScrollView.directionalLockEnabled = YES;
myScrollView.canCancelContentTouches = NO;
myScrollView.delaysContentTouches = NO;
myScrollView.scrollEnabled = true;
myScrollView.userInteractionEnabled = YES;
myScrollView.scrollsToTop = NO;
mySettingsTableView = [[SettingsTableView alloc] init];
// Settings View Ctrl
SettingsTableViewCtrl *mySettingsTableViewCtrl = [[SettingsTableViewCtrl alloc] initWithStyle:UITableViewStyleGrouped];
mySettingsTableViewCtrl.tableView.scrollEnabled = NO;
mySettingsTableViewCtrl.tableView.delaysContentTouches = NO;
mySettingsTableViewCtrl.tableView.canCancelContentTouches = NO;
mySettingsTableViewCtrl.view.userInteractionEnabled = YES;
[mySettingsTableViewCtrl.view addSubview:myScrollView];
[mySettingsTableViewCtrl.view addSubview:mySettingsTableView];
* In subclass of scrollView *
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//if (![self yourMethodThatDeterminesInterestingTouches:touches withEvent:event])
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
Here's the NSLog from the touch events:
NSLog(#"Event: %#", event);
NSLog(#"Event NextResponder : %#\n", self.nextResponder);
2011-07-05 13:09:36.063 App[1924:14f03] Event: <UITouchesEvent: 0x802ea30> timestamp: 8503.71 touches: {(
<UITouch: 0x80e6f60> phase: Began tap count: 1 window: <UIWindow: 0x836eb00; frame = (0 0; 320 480); layer = <CALayer: 0x836ebb0>> view: <ScrollView: 0x83770b0; baseClass = UIScrollView; frame = (0 150; 320 680); clipsToBounds = YES; layer = <CALayer: 0x8376e00>; contentOffset: {0, 0}> location in window: {140, 249} previous location in window: {140, 249} location in view: {140, 35} previous location in view: {140, 35}
)}
2011-07-05 13:09:36.065 App[1924:14f03] Event NextResponder : <SettingsTableViewCtrl: 0x8377db0>
I have the same problem with my code, and solved it by adding UITapGestureRecognizer to the view to which I want to handle touches.
In your case you should also add a UITapGestureRecognizer to either scroll view if you want to handle touches on it or table view if you want to handle touches on it as;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tappedScrollView)];
[tapRecognizer setNumberOfTapsRequired:1];
[myScrollView addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
and what you want to do on touch should be done in a method as;
-(void)tappedScrollView
{
//TO-DO what you want after Scroll view touched, DO it here.
}
Similarly you can do it for your table view as;
UITapGestureRecognizer *tapRecognizerForTableView = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tappedTableView)];
[tapRecognizerForTableView setNumberOfTapsRequired:1];
[mySettingsTableView addGestureRecognizer:tapRecognizerForTableView];
[tapRecognizerForTableView release];
and what you want to do on touch should be done in a method as;
-(void)tappedTableView
{
//TO-DO what you want after Table view touched, DO it here.
}
Here's what I ended up, not perfect yet but working, any tuning advice would be much appreciated......
//AppDelegate.m
ScrollView *myScrollView = [[ScrollView alloc] initWithFrame:CGRectMake(0, 140, 320, 680)];
myScrollView.contentSize = CGSizeMake( 320 , 888);
myScrollView.pagingEnabled = NO;
myScrollView.bounces = NO;
myScrollView.directionalLockEnabled = YES;
myScrollView.canCancelContentTouches = NO;
myScrollView.delaysContentTouches = NO;
myScrollView.scrollEnabled = true;
myScrollView.userInteractionEnabled = YES;
myScrollView.scrollsToTop = NO;
mySettingsTableViewCtrl = [[SettingsTableViewCtrl alloc] initWithStyle:UITableViewStyleGrouped];
mySettingsTableViewCtrl.tableView.scrollEnabled = NO;
mySettingsTableViewCtrl.tableView.delaysContentTouches = NO;
mySettingsTableViewCtrl.tableView.canCancelContentTouches = NO;
mySettingsTableViewCtrl.view.userInteractionEnabled = YES;
[mySettingsTableViewCtrl.view addSubview:myScrollView];
[mySettingsTableViewCtrl.view addSubview:mySettingsTableView];
//ScrollView.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
//SettingsTableView.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSUInteger tapCount = [touch tapCount];
CGPoint location = [touch locationInView:self];
NSIndexPath *indexPathAtHitPoint = [self indexPathForRowAtPoint:location];
if ( [self cellForRowAtIndexPath:indexPathAtHitPoint] ) {
[self.delegate tableView:self didSelectRowAtIndexPath:indexPathAtHitPoint];
} else {
[super touchesBegan:touches withEvent:event];
}
}
Now I have a DatePicker on top of the scrollview and its working but this code needs some additional code to manage it, i.e. detect when picker is "touched" and when its not there. But that shouldn't be too difficult (I think ;)
Thanks for help and comments,
D
I think it might be overkill to use a ScrollView for your purpose. You can "scroll" any view. Position it so part or all of it is out of bounds, set clipping mode on the parent view, and when you want to "scroll" it, change its frame to in-bounds, with animation in effect. That is, if you are scrolling the DatePicker in in response to some other event (like adding an item to the table that is associated with a date). If the user scrolls the date picker in by dragging then carry on.
Combining a scroll view and a table view will be tricky, since a table view is a scroll view. It's ambiguous which should handle scrolling gestures. It might be possible but you probably have to write extra code to make the ambiguity explicit.
I have a view controller where there are several uiview object. I need to know on which uiview user have tapped. how is this possible? any guidance will help a lot....
Thanks
Pankaj
Here is what you can do to get what you wanted ..... In this example i have created 7 views
UITapGestureRecognizer* gestureRecognizer;
UIView* myView;
for (int i = 0; i < 8; i++)
{
gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doSomthing:)];
gestureRecognizer.numberOfTapsRequired = 1;//or what ever you want
myView = [[UIView alloc] initWithFrame:CGRectMake(10, i*30, 30, 28)];
myView.backgroundColor = [UIColor redColor];
myView.tag = 100+i;
[self.view addSubview:myView];
[myView addGestureRecognizer:gestureRecognizer];
[myView release];
[gestureRecognizer release];
}
Now you need to implement the method like this
-(void)doSomthing:(id)sender
{
UIView* temp = [(UITapGestureRecognizer*)sender view];
// here you get the view you wanted
NSLog(#"view number :%d",temp.tag);
}
I think this should help you
Set a tag for every view to keep track of them.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// We only support single touches, so anyObject retrieves just that touch from touches
UITouch *touch = [touches anyObject];
NSLog(#"view %i", [touch view].tag);
}
you can add gestures to the uiview objects to find which object has been touched. see the documentation.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html
for specific code just comment.
U can probably add a custom button with a tag on top of each view. Then u can know which view is tapped based on the button tag.
pls take a look at this. It may help.
http://www.iphonedevsdk.com/forum/iphone-sdk-development/13041-touch-event-subview.html