I have a UIButton which sends the draginside event to:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
It does this through the following code in viewDidLoad:
[colourButton1 addTarget:self action:#selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];
Within the method it sends it to, there is the following line:
UITouch *myTouch = [touches anyObject];
And for some reason, when dragging inside the UIButton this crashes the app. Any ideas why?
EDIT: The solution..
-(IBAction)buttonDragged:(id)button withEvent:(UIEvent*)event {
NSSet *touches = [event touchesForView:colourButton1];
[self touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event];
}
When you're adding target to control you can pass 3 types of selectors for action -
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender withEvent:(UIEvent*)event
Names don't matter, it's number of parameter which is important. If control sends it's target a message with 2 params then first parameter will be control itself (instance of UIButton in your case), second one - instance of UIEvent. But you expects instance of NSSet as first parameter and send it a message anyObject which UIButton doesn't understand. This is the cause of crash.
Why are you trying to send event from UI control to touch handling method touchesMoved:withEvent: in the first place? It will probably do something different from what you meant.
UPDATE:
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event {
UITouch *t = [touches anyObject];
CGPoint touchLocation = [t locationInView:self.view];
NSLog(#"%#", NSStringFromCGPoint(touchLocation));
}
- (IBAction)buttongDragged:(id)button withEvent:(UIEvent*)event {
NSSet *touches = [event touchesForView:button];
[self touchesMoved:touches withEvent:event];
}
Notice that since touchesMoved:withEvent: is a UIResponder's method and controller's view is UIResponder this method will be also called on touch events for this view.
Related
I cannot figure out what is wrong. Below is my code, and it calls the delegate methods once then stops.
What should I do? I haven;t been able to find the sample code that uses these delegate methods. All I've found were gesture recognizers for swipes and taps, using different delegates.
Code so far:
-(void)initTouchesRecognizer{
DLog(#"");
recognizer = [[UIGestureRecognizer alloc] init];
[self addGestureRecognizer:recognizer];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
DLog(#"");
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches)
{
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
DLog(#"");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
I call initTouchesRecognizer from initwithrect for my image view.
What am i doing fundamentally wrong?
UIGestureRecognizer is an abstract class, you're not supposed to add it directly to your view. You need to use a concrete subclass that inherits from UIGestureRecognizer, like UITapGestureRecognizer or UIPanGestureRecognizer for example. You could also make your own concrete subclass but that usually isn't necessary.
Here is an example of adding a UIPanGestureRecognizer to your view (in your view class code, often the gesture is added to the view from the controller):
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(mySelector:)];
[self addGestureRecognizer:panGesture];
In this case, the selector will be called when ever the user pans in this view. If you added a UITapGestureRecognizer, the selector would be called when the user tapped.
You can check out the apple docs for more info:
http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html#//apple_ref/doc/uid/TP40009541-CH2-SW2
Also, I find Paul Hagerty's Stanford lectures to be great, here's one on gesture recognizers:
https://itunes.apple.com/ca/course/6.-views-gestures-january/id593208016?i=132123597&mt=2
You should also understand that none of the methods that you posted are delegate methods, and none of them have anything to do with the UIGestureRecognizer that you added in your code. Those are instance methods of UIResponder (a class that UIView inherits from) that you're overriding. The abstract UIGestureRecognizer also has instance methods with those same names, but it is not the UIGestureRecognizer methods that are getting called in your class.
There was no need to add the gesture recognizer. By overriding the touchesMoved, touchesEnded and touchesBegan methods, I was able to track the user's finger across the screen.
simply do not call the:
-(void)initTouchesRecognizer
code, and the code I originally posted will work.
I've found some code that helps me resign the keyboard when a user touches the screen off of the UITextView element.
Here's how it looks:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if([self.speechBubble.speechText isFirstResponder] && [touch view] != self.speechBubble.speechText){
[self.speechBubble.speechText resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
This works perfectly so far, and will remove the keyboard if a user touches anywhere outside of the text view. However, it only works for the particular object that I'm running it for, so if I have two speechBubbles, it won't work.
How can I change this so that ANY speechBubble will have the same effect? (I could move this code from my ViewController to my SpeechBubble class, but I'd have a little issue with how to use [touch view] to get touches outside of the speechBubble's view. ) Thanks
Something I just discovered recently that may be of use to you is:
[self.view endEditing:YES];
It will resign first responder from any element that currently has it without you having to manually keep track of it yourself.
In reference to your example code, something like this might work, depending on how your speechBubbles work:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
// Note the '!':
if(![[touch view] class] isKindOfClass [speechBubble class]]){
// It's not a bubble they touched, dismiss the keyboard:
[self.view endEditing:YES];
}
[super touchesBegan:touches withEvent:event];
}
I'm posting this message because I've been reading the forum and I haven't been able to find a similar problem. I need to be able to discriminate taps and double taps (this is a standard thing) BUT my problem is that for whatever reasons I have a Scroll View inside another ScrollView. So, I had to sub-class my ScrollView in order to get touchedBegin method called.
I have a class called PhotoViewController (a sub-class of BaseViewController) this class contains another class called CustomScrollView (a subclass of ScrollView). I needed to sub-class this CustomScrollView from ScrollView in order to override the touchesBegin method, and to be able to capture the touches made by the user.
I tried calling the touchesBegin method from CustomScrollView using something like return [super touchesBegan:touches withEvent:event] inside the touchesBegin method, but when the touchesBegin method inside PhotoViewController gets called it's parameters are empty (and I can't discriminate if the user made a single or double tap, which is exactly what I need)
I have a class, called PhotoViewController:
#class PhotoViewController
#interface PhotoViewController : BaseViewController <UIScrollViewDelegate> {
CustomScrollView* myScrollView;
}
#implementation PhotoViewController
...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSUInteger tapCount = [touch tapCount];
switch (tapCount) {
case 1:
[self performSelector:#selector(singleTapMethod) withObject:nil afterDelay:.4];
break;
case 2:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(singleTapMethod) object:nil];
[self performSelector:#selector(doubleTapMethod) withObject:nil afterDelay:.4];
break;
default:
break;
}
}
the class CustomScrollView is (CustomScrollView.h):
#interface CustomScrollViewPhoto : UIScrollView {
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
#end
and it's implementation is this(CustomScrollView.m):
#implementation CustomScrollViewPhoto
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.superview touchesBegan:[NSSet set] withEvent:event];
return [super touchesBegan:touches withEvent:event];
}
Am I going in the wrong direction with what I want to do? Maybe, I should capture the taps/double taps inside the CustomScrollView class(this works fine!), and from there using a #selector or something call the appropiate methods in PhotoViewController?
Thanks for reading!
I think you're going the wrong route (only slightly!). I do a very similar thing in a photo viewer and I capture the touches in the CustomScrollView. You shouldn't need to do anything in PhotoViewController.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
UITouch *touch = [touches anyObject];
if(touch.tapCount == 2)
{
if (self.zoomScale == self.minimumZoomScale)
{
//Zoom where the user has clicked from
CGPoint pos = [touch locationInView:self];
[self zoomToRect:CGRectMake((pos.x - 5.0)/self.zoomScale, (pos.y-5.0)/self.zoomScale, 10.0, 10.0) animated:YES];
}
else
{
//Zoom back out to full size
[self setZoomScale:self.minimumZoomScale animated:YES];
}
}
}
I wish to drag from one subview to another within my application (and connect them with a line), and so I need to keep track of the current view being touched by the user.
I thought that I could achieve this by simply calling [UITouch view] in the touchesMoved method, but after a quick look at the docs I've found out that [UITouch view] only returns the view in which the initial touch occurred.
Does anyone know how I can detect the view being touched while the drag is in progress?
And my way:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([self pointInside:[touch locationInView:self] withEvent:event]) {
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
} else {
[self sendActionsForControlEvents:UIControlEventTouchUpOutside];
}
}
After a bit more research I found the answer.
Originally, I was checking for the view like so:
if([[touch view] isKindOfClass:[MyView* class]])
{
//hurray.
}
But, as explained in my question, the [touch view] returns the view in which the original touch occurred. This can be solved by replacing the code above with the following:
if([[self hitTest:[touch locationInView:self] withEvent:event] isKindOfClass:[MyView class]])
{
//hurrraaay! :)
}
Hope this helps
UIView is subclass of UIResponder. So you can override touches ended/ began methods in your custom class, that inherits from UIView. Than you should add delegate to that class and define protocol for it. In the methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
or whatever interactions you need just send appropriate message to object's delegate also passing this object. E.g.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[delegate touchesBeganInView: self withEvent: event];
}
HI . i am trying to move an UIImageView object with UITouch and have problem with the code how can i implement UITouch to detect only my UIImageView object ?
**imageA it my UIImageView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *toucheA = [[event allTouches] anyObject];
if ([toucheA view] == imageA) {
CGPoint location = [toucheA locationInView:self];
imageA.center = location;
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
CGPoint postion = imageA.center;
imageA.center = postion;
postion.y = 230;
imageA.center = postion;
}
but doesn't work !
I think you can make a custom UIImageView by subclassing and receive touch event you want inside the custom view.
Generally, the top most view will receiver the touch event. If the view process the touch event and doesn't pass the event to another view, then the others view behind it are impossible to receive any touch event. If the view cannot deal with the touch event, it will pass that by calling super's method.