IOS: touchbegan and touchmoved in two classes - ios

In my class that I call "one" I have two method for touch: touchbegan and touchmoved.
In this class I alloc an imageview in this way:
imageView = [[ImageToDrag alloc] initWithImage:[UIImage imageNamed:#"machine.png"]];
imageView.center = CGPointMake(905, 645);
imageView.userInteractionEnabled = YES;
[self addSubview:imageView];
[imageView release];
in this class (ImageToDrag) in .m I have:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// When a touch starts, get the current location in the view
currentPoint = [[touches anyObject] locationInView:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
// Get active location upon move
CGPoint activePoint = [[touches anyObject] locationInView:self];
// Determine new point based on where the touch is now located
CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x),
self.center.y + (activePoint.y - currentPoint.y));
//--------------------------------------------------------
// Make sure we stay within the bounds of the parent view
//--------------------------------------------------------
float midPointX = CGRectGetMidX(self.bounds);
// If too far right...
if (newPoint.x > self.superview.bounds.size.width - midPointX)
newPoint.x = self.superview.bounds.size.width - midPointX;
else if (newPoint.x < midPointX) // If too far left...
newPoint.x = midPointX;
float midPointY = CGRectGetMidY(self.bounds);
// If too far down...
if (newPoint.y > self.superview.bounds.size.height - midPointY)
newPoint.y = self.superview.bounds.size.height - midPointY;
else if (newPoint.y < midPointY) // If too far up...
newPoint.y = midPointY;
// Set new center location
self.center = newPoint;
}
so my problem is this: touch recognize method inside ImageToDrag class and not in my main class "one", why? is there a way to recognize touch in each class?

From UIResponder touchesBegan method:
The default implementation of this method does nothing. However
immediate UIKit subclasses of UIResponder, particularly UIView,
forward the message up the responder chain. To forward the message to
the next responder, send the message to super (the superclass
implementation); do not send the message directly to the next
responder. For example,
So add [super touchesBegan:touches withEvent:event]; to your touchesBegan method (and the other touches methods) for events you want to pass up the responder chain.
You should also implement the touchesEnded and touchesCanceled methods.

Related

How to send the touch events of UIScrollView to the view behind it?

i have a transparent UIScrollView on top of another view.
the scroll view has content - text and images, used to display info.
the view behind it has some images that the user should be able to tap on.
and the content over them is scrollable using the mentioned scrollview.
i want to be able to normally use the scroll view (no zoom though), but when that scroll view is not actually scrolling to let the tap events through to the view behind it.
using a combination of the touch and scroll events i can determine when to let the taps through.
but the view behind it still it does not receive them.
i have tried using something like this for all touch events:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesBegan %#", (_isScrolling ? #"YES" : #"NO"));
if(!_isScrolling)
{
NSLog(#"sending");
[self.viewBehind touchesBegan:touches withEvent:event];
}
}
but it does not work.
also in my case i cannot really apply the hitTest and pointInside solutions, given the use case that i have.
First off UIScrollViews only inherently recognize UIPanGestureRecognizers and UIPinchGestureRecognizers so you need to add a UITapGestureRecognizer to the UIScrollView so it can recognize any tapping gestures as well:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
// To prevent the pan gesture of the UIScrollView from swallowing up the
// touch event
tap.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:tap];
Then once you receive that tap gesture and the handleTap: action is triggered, you can use locationInView: to detect whether the tap gesture's position is in fact within the frame of one of the images below your scroll view, for example:
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
// First get the tap gesture recognizers's location in the entire
// view's window
CGPoint tapPoint = [recognizer locationInView:self.view];
// Then see if it falls within one of your below images' frames
for (UIImageView* image in relevantImages) {
// If the image's coordinate system isn't already equivalent to
// self.view, convert it so it has the same coordinate system
// as the tap.
CGRect imageFrameInSuperview = [image.superview convertRect:image toView:self.view]
// If the tap in fact lies inside the image bounds,
// perform the appropriate action.
if (CGRectContainsPoint(imageFrameInSuperview, tapPoint)) {
// Perhaps call a method here to react to the image tap
[self reactToImageTap:image];
break;
}
}
}
This way, the above code is only performed if a tap gesture is recognized, your program only reacts to a tap on the scroll view if the tap location falls within an image; otherwise, you can just scroll your UIScrollView as usual.
Here I present my complete solution that:
Forwards touches directly to views instead of calling a control event.
User can specify which classes to forward.
User can specify which views to check if forward is needed.
Here the interface:
/**
* This subclass of UIScrollView allow views in a deeper Z index to react when touched, even if the scrollview instance is in front of them.
**/
#interface MJForwardingTouchesScrollView : UIScrollView
/**
* Set of Class objects. The scrollview will events pass through if the initial tap is not over a view of the specified classes.
**/
#property (nonatomic, strong) NSSet <Class> *forwardsTouchesToClasses;
/**
* Optional array of underlying views to test touches forward. Default is nil.
* #discussion By default the scroll view will attempt to forward to views located in the same self.superview.subviews array. However, optionally by providing specific views inside this property, the scroll view subclass will check als among them.
**/
#property (nonatomic, strong) NSArray <__kindof UIView*> *underlyingViews;
#end
And the Implementation:
#import "MJForwardingTouchesScrollView.h"
#import "UIView+Additions.h"
#implementation MJForwardingTouchesScrollView
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self != nil)
{
_forwardsTouchesToClasses = [NSSet setWithArray:#[UIControl.class]];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self != nil)
{
_forwardsTouchesToClasses = [NSSet setWithArray:#[UIControl.class]];
}
return self;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL pointInside = [self mjz_mustCapturePoint:point withEvent:event];
if (!pointInside)
return NO;
return [super pointInside:point withEvent:event];
}
#pragma mark Private Methods
- (BOOL)mjz_mustCapturePoint:(CGPoint)point withEvent:(UIEvent*)event
{
if (![self mjz_mustCapturePoint:point withEvent:event view:self.superview])
return NO;
__block BOOL mustCapturePoint = YES;
[_underlyingViews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (![self mjz_mustCapturePoint:point withEvent:event view:obj])
{
mustCapturePoint = NO;
*stop = YES;
}
}];
return mustCapturePoint;
}
- (BOOL)mjz_mustCapturePoint:(CGPoint)point withEvent:(UIEvent *)event view:(UIView*)view
{
CGPoint tapPoint = [self convertPoint:point toView:view];
__block BOOL mustCapturePoint = YES;
[view add_enumerateSubviewsPassingTest:^BOOL(UIView * _Nonnull testView) {
BOOL forwardTouches = [self mjz_forwardTouchesToClass:testView.class];
return forwardTouches;
} objects:^(UIView * _Nonnull testView, BOOL * _Nullable stop) {
CGRect imageFrameInSuperview = [testView.superview convertRect:testView.frame toView:view];
if (CGRectContainsPoint(imageFrameInSuperview, tapPoint))
{
mustCapturePoint = NO;
*stop = YES;
}
}];
return mustCapturePoint;
}
- (BOOL)mjz_forwardTouchesToClass:(Class)class
{
while ([class isSubclassOfClass:NSObject.class])
{
if ([_forwardsTouchesToClasses containsObject:class])
return YES;
class = [class superclass];
}
return NO;
}
#end
The only extra code used is inside the UIView+Additions.h category, which contains the following method:
- (void)add_enumerateSubviewsPassingTest:(BOOL (^_Nonnull)(UIView * _Nonnull view))testBlock
objects:(void (^)(id _Nonnull obj, BOOL * _Nullable stop))block
{
if (!block)
return;
NSMutableArray *array = [NSMutableArray array];
[array addObject:self];
while (array.count > 0)
{
UIView *view = [array firstObject];
[array removeObjectAtIndex:0];
if (view != self && testBlock(view))
{
BOOL stop = NO;
block(view, &stop);
if (stop)
return;
}
[array addObjectsFromArray:view.subviews];
}
}
Thanks
The problem is that your UIScrollView is consuming the event. To pass it through, you would have to disable the user's interaction on it, but it wouldn't scroll then. If you have the touches location however, you can calculate where would that fall on the underlying view, using the convertPoint:toView: method, and call a mathod on it by passing on the CGPoint. From there, you can calculate which image was tapped.

Handle disabled Button Touch

I want to handle touches on my disabled button
self.closeButton.enabled = NO;
self.closeButtonDisabledRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(closeButtonDisablePressed)];
[self.closeButton addGestureRecognizer:self.closeButtonDisabledRecognizer];
But seems like it doesn't work. Any good solution ?
Disabling is too much like telling the SDK to ignore touches to be what you want. I suggest instead:
#property(assign, nonatomic) BOOL treatTheCloseButtonAsEnabled;
// replace the synthesized setter
- (void)setTreatTheCloseButtonAsEnabled:(BOOL)enabled {
self.closeButton.alpha = (enabled)? 1.0 : 0.5;
// or some other visible indication of the "disabled" state
}
- (IBAction)pressedCloseButton:(id)sender {
if (self.treatTheCloseButtonAsEnabled) {
// logic for a regular press
} else {
// logic for a disabled press
}
}
Maybe you can also use the touch event in the controller.It will be like this(I assume that your close button is added on controller.view):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
touchPoint = [self.view convertPoint:touchPoint toView:_closeButton.superview];
//to determine wether user touch on you button
if (CGRectContainsPoint(_closeButton.frame, touchPoint) == NO) {
return;
}
//handle your touch on button here
}

Passing a PanGesture to a subview

I am building a iOS app. I have a UIWebView that is added as a subview to self.view, then another view, which is mapView, added as a subview of the web view. But the mapView is send to the back of the webView. The background of the webView is transparent so that one can see the map.
see the code:
[self.webView addSubview: self.mapView];
[self.webView sendSubviewToBack: self.mapView];
Well what I am trying to do is to pass the gestures of the webView to the mapView so that the user can drag the map.
I have marked the cancelsTouchesInView property to NO for both the webView and the mapView.
I have added a gesture recognizer for the webView. The selector does get called. But what am I supposed to do next?
self.webPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action: #selector(handleWebPanGesture:)];
[self.webView addGestureRecognizer: self.webPanGesture];
I called the gestureRecognizerShouldBegin method in the webView selector, but it doesn't work.
- ( void ) handleWebPanGesture: ( UIPanGestureRecognizer *)gesture
{
NSLog(#"WebPanGesture recognizer called!");
[self.mapView gestureRecognizerShouldBegin: gesture];
[self panAction: gesture];
self.mapPanGesture = gesture; // the mapPanGesture property is the Gesture recognizer for the map
}
I also call this function
- ( IBAction )panAction:(UIPanGestureRecognizer *)sender {
NSLog(#"panAction called!");
CGPoint move = [sender translationInView:self.webView];
CGPoint newCenter = subViewCenter;
newCenter.x += move.x; newCenter.y += move.y;
self.myMapView.mapView.center = newCenter;
}
but it doesn't make the map draggable, it just moves it.
self.mapPanGesture = gesture //doesn't work as well.
How can I target the actions to the mapView so that the map gets dragged when drag on the webView?
I sure you should use overlays (MKOverlay) on mapView to show content on map. Because this is much easier way to achieve what you need.
Please read this Adding Annotations to a Map
Here I found a way around so do check out this link might be helpful.
In short, webView doesn't handle touchBegan method so u need to subclass that and in touch began method u could pass the following,
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"%#",event);
[super touchesBegan:touches withEvent:event];
[_mapView touchesBegan:touches withEvent:event];
}
or check out for below method,
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitView = [super hitTest:point withEvent:event];
// If the hitView is THIS view, return the view that you want to receive the touch instead:
if (hitView == self) {
return otherView;
}
// Else return the hitView (as it could be one of this view's buttons):
return hitView;
}
Above hitTest is with reference to this link.
Hope, this much info is useful to u.

Cocos2d - TMX Collision checking during a CCMoveTo action invoked on an instance of CCSprite

The application responds to touches with the following method - invoking movePlayer:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.player stopAllActions];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
CGPoint diff = ccpSub(touchLocation, self.player.position);
self.distanceToMovePlayer = sqrtf((diff.x*diff.x)+(diff.y*diff.y));
self.playerDestination = touchLocation;
[self movePlayer];
}
movePlayer is defined here. It runs the CCAction that moves the sprite to the touch.
- (void)movePlayer{
CCAction *movePlayer = [CCMoveTo actionWithDuration:self.distanceToMovePlayer/100 position:self.playerDestination];
self.playerMovement = movePlayer;
[self.player runAction:self.playerMovement];
}
I have an invisible TMX Layer called meta on the TMXTileMap that indicates a wall or boundary with the following method that runs every frame:
- (void)checkCollisions:(CGPoint)position{
CGPoint tileCoordinate = [self tileCoordForPosition:position];
int tileGID = [self.meta tileGIDAt:tileCoordinate];
if(tileGID == 49){
NSDictionary *properties = [self.meta properties];
if(properties){
NSString *collision = properties[#"Collidable"];
if(collision && [collision isEqualToString:#"True"]){
[self.player stopAction:self.playerMovement];
}
}
Whenever the sprite touches a boundary, the action stops and the sprite is simply stuck there because the action is immediately stopping whenever it starts as the sprite is still in the boundary.
I have tried setting the collision method to return a boolean which is then tested in the CCMoveTo. Is there a way to call a selector each iteration of a CCAction? Something like CCCallBlockN that runs each frame of the action.
Well, i would probably schedule a selector for a CCAction duration, for a per-frame call back. Assuming you also run some kind of animation on the player sprite, and new in cocos2d 2.0+, you could use a CCAnimation, whereby you could register for a notification to be served with some user-data for each frame.
from the CCAnimation.h file :
/** A CCAnimationFrameDisplayedNotification notification will be broadcasted
* when the frame is displayed with this dictionary as UserInfo. If UserInfo is nil,
* then no notification will be broadcasted. */
#property (nonatomic, readwrite, retain) NSDictionary *userInfo;
ob cit. Not tried, ymmv

Drawing line on touches moved in COCOS2D

I'm developing a game for iPhone using COCOS2D.
In that, I need to draw a line when user drag his finger from a point to another. As far as my knowledge is concern I need to do this in Touches Moved method from where I can get the points.
But I don't know how to do this. Can anybody help me on this?
Kia ora. Boredom compels me to provide an answer on this topic.
Layer part (i.e. #interface GetMyTouches : CCLayer):
-(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event
{
UITouch *touchMyMinge = [inappropriateTouches anyObject];
CGPoint currentTouchArea = [touchMyMinge locationInView:[touchMyminge view] ];
CGPoint lastTouchArea = [touchMyMinge previousLocationInView:[touchMyMinge view]];
// flip belly up. no one likes being entered from behind.
currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea];
lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea];
// throw to console my inappropriate touches
NSLog(#"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y);
NSLog(#"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y);
// add my touches to the naughty touch array
naughtyTouchArray addObject:NSStringFromCGPoint(currentTouchArea)];
naughtyTouchArray addObject:NSStringFromCGPoint(lastTouchArea)];
}
Node part (i.e. #interface DrawMyTouch: CCNode) :
#implementation DrawMyTouch
-(id) init
{
if( (self=[super init]))
{ }
return self;
}
-(void)draw
{
glEnable(GL_LINE_SMOOTH);
for(int i = 0; i < [naughtyTouchArray count]; i+=2)
{
start = CGPointFromString([naughtyTouchArray objectAtIndex:i]);
end = CGPointFromString([naughtyTouchArray objectAtIndex:i+1]);
ccDrawLine(start, end);
}
}
#end
Layer part II (i.e. #interface GetMyTouches : CCLayer):
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
DrawMyTouch *line = [DrawMyTouch node];
[self addChild: line];
}
Remember touching is easy. Knowing what you're doing while touching isn't rocket science.
Finally, if you don't understand anything i've posted ... take up baking. The world needs more chocolate cake producers.
Clarification:
No one learns from cut 'n paste ~ this code was never meant to work without caressing
If you fail to see the humour, you're in the wrong profession
Of note, I love a good chocolate cake. The world really does need more fantastic bakers. That's not an insult, that's encouragement.
"Look outside the square, to find the circle filled with the knowledge that makes life worth living" ~ Aenesidemus.
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *theTouch = [touches anyObject];
CGPoint touchLocation = [theTouch locationInView:[theTouch view] ];
cgfloat x = touchLocation.x;
cgfloat y= touchLocation.y;
printf("move x=%f,y=%f",x,y);
}
Try the above code. It will get the coordinate points when touches moved in iphone.
To draw line, use something like this:
-void draw
{
here is the code for line draw.
}
Update this function in update method.

Resources