Touch coordinates/location distorted - uiview

Hi all I have a spritekit scene "Menu"
that I am loading a UIView as a subview (CGScratchViewController.xib)
-(void)addscratchView:(SKView*)view
{
CGRect viewFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
//ScratchableView : UIView
myScratchView = [[ScratchView alloc]initWithFrame:viewFrame ];
self.name = #"menuScene";
[self.view addSubview:myScratchView];
}
ScratchableView class loads a layer that i can erase with my finger an overlay drawing revealing a drawing below
the code seems to be working however the touches are off and seem to be "scaled" somehow, meaning that if I draw in the top left corner the touch is in the correct place, but dragging the finger outward and the touch becomes more and more distorted
- any thoughts on what to look for to correct this
(am a newbie making newbie mistakes)
Scratchableview.h
// Created by Olivier Yiptong on 11-01-11.
//
#import "ScratchableView.h"
#implementation ScratchableView
#synthesize contentScale;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
scratchable = [UIImage imageNamed:#"scratchable.jpg"].CGImage;
width = CGImageGetWidth(scratchable);
height = CGImageGetHeight(scratchable);
self.opaque = NO;
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
CFMutableDataRef pixels = CFDataCreateMutable( NULL , width * height );
alphaPixels = CGBitmapContextCreate( CFDataGetMutableBytePtr( pixels ) , width , height , 8 , width , colorspace , kCGImageAlphaNone );
provider = CGDataProviderCreateWithCFData(pixels);
CGContextSetFillColorWithColor(alphaPixels, [UIColor blackColor].CGColor);
CGContextFillRect(alphaPixels, frame);
CGContextSetStrokeColorWithColor(alphaPixels, [UIColor whiteColor].CGColor);
CGContextSetLineWidth(alphaPixels, 20.0);
CGContextSetLineCap(alphaPixels, kCGLineCapRound);
CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, width, provider, nil, NO);
scratched = CGImageCreateWithMask(scratchable, mask);
CGImageRelease(mask);
CGColorSpaceRelease(colorspace);
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextDrawImage(UIGraphicsGetCurrentContext() , [self bounds] , scratched);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if([[touch view] isKindOfClass:[UIImageView class]]){
CGPoint point= [touch locationInView:touch.view];
NSLog(#"%f%f",point.x,point.y);
location = point;
}
firstTouch = YES;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:self] anyObject];
if (firstTouch) {
firstTouch = NO;
previousLocation = [touch previousLocationInView:self];
} else {
location = [touch locationInView:self];
previousLocation = [touch previousLocationInView:self];
}
// Render the stroke
[self renderLineFromPoint:previousLocation toPoint:location];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:self] anyObject];
if (firstTouch) {
firstTouch = NO;
previousLocation = [touch previousLocationInView:self];
[self renderLineFromPoint:previousLocation toPoint:location];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end {
CGContextMoveToPoint(alphaPixels, start.x, start.y);
CGContextAddLineToPoint(alphaPixels, end.x, end.y);
CGContextStrokePath(alphaPixels);
[self setNeedsDisplay];
}
- (void)dealloc {
CGContextRelease(alphaPixels);
CGImageRelease(scratchable);
CGDataProviderRelease(provider);
}
and Menuscene
-(void)didMoveToView:(SKView *)view {
self.userInteractionEnabled = YES;
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// [self createButtons ];
[self addscratchView:view]; //for testing
}
-(void)addscratchView:(SKView*)view
{
CGRect viewFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
myScratchView = [[ScratchView alloc]initWithFrame:viewFrame ];
self.name = #"menuScene";
[self.view addSubview:myScratchView];
}
I've tried to convert the coordinates...still not much luck (maybe its not the right thing to do anyways ..
//convert between view coordinates and scene coordinates
//coordinate system is not the same in a Sprite Kit scene as they are in a UIView
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchlocation = [touch locationInView:touch.view];
spriteView = (Menu *) spriteView.scene.view;
CGPoint positionInScene = [self convertPoint:touchlocation fromCoordinateSpace:spriteView.scene.view];
Video of the effect/distorted touches
https://www.youtube.com/watch?v=dMrcufcKpao

It looks like you are not layering your views the way you are thinking you are laying them in your head.
Right now you have it layered like this
Back of the screen
SKView
--SKScene
ScratchView
Now when this is happening, here is how your views are probably laid out size wise.
SKView->UIBuilder Default (600x600 I think)
--SKScene-> SceneBuilder Default (600x600)
ScratchView->ScreenSize
Your SKView will then resize to the screen size with AutoConstraints, but your scene will stay at 600x600 because you do not set the scaleMode to .ResizeFill, so now you have 2 different coordinate systems, with scaling all off.
I think the best option for you right now is to just set your scaleMode to .ResizeFill (skView.scaleMode = .ResizeFill) so that all your coordinates line up, and as you learn more about what SpriteKit has to offer, you can refactor your code better to suit your needs.

Related

Objective c - transparent draw on view (UIVisualEffectView)

I want to create a transparent draw on a view (like the red draw inside the yellow view).
I have some view (UIVisualEffectView - the yellow view) and i want to draw with the finger, and it will looks like we "erase" it.
I created a subclass of UIVisualEffectView and tried to draw, but its not working so well..
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
CGPoint location = [touch locationInView:self];
CGFloat drawSize = 45;
CGRect rect = CGRectMake(location.x, location.y, drawSize, drawSize);
[self test:rect];
}
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
CGPoint location = [touch locationInView:self];
CGFloat drawSize = 45;
[self test:CGRectMake(location.x, location.y, drawSize, drawSize)];
}
}
-(void)test:(CGRect)rect
{
if (!self.mask)
{
self.mask = [[UIView alloc]initWithFrame:self.bounds];
self.mask.clipsToBounds = YES;
self.mask.backgroundColor = [UIColor clearColor];
}
if (!self.fillLayer)
{
self.fillLayer = [CAShapeLayer layer];
self.fillLayer.fillRule = kCAFillRuleEvenOdd;
self.fillLayer.fillColor = [UIColor greenColor].CGColor;
}
if (!self.outerbezierPath)
{
self.outerbezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:0];
self.outerbezierPath.usesEvenOddFillRule = NO;
}
self.innerCirclepath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:rect.size.height * 0.5];
[self.outerbezierPath appendPath:self.innerCirclepath];
[self.fillLayer removeFromSuperlayer];
self.fillLayer.path = self.outerbezierPath.CGPath;
[self.mask.layer addSublayer:self.fillLayer];
self.maskView = self.mask;
}

UITouch coordinates and SKSpriteNode coordinates are different

I have an SKSpriteNode that is initialized with an image. I am using the -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method to figure out if the user touch is within the bounds of the sprite. For some reason, even when I tap inside the sprite, the coordinates are not even similar. They seem to be on a different coordinate system.
#import "GameScene.h"
SKSpriteNode *card;
#implementation GameScene
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
self.backgroundColor = [SKColor whiteColor];
card = [SKSpriteNode spriteNodeWithImageNamed:#"card"];
card.position = CGPointMake(500, 500);
[self addChild:card];
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
int cardX = card.frame.origin.x;
int cardwidth = card.frame.size.width;
int cardY = card.frame.origin.y;
int cardHeight = card.frame.size.height;
if(touchLocation.x >= card.frame.origin.x &&
touchLocation.x <= (card.frame.origin.x + card.frame.size.width) &&
touchLocation.y <= card.frame.origin.y &&
touchLocation.y >= (card.frame.origin.y + card.frame.size.height))
{
self.backgroundColor = [SKColor blackColor];
}
else{
self.backgroundColor = [SKColor whiteColor];
}
}
#end
Node and View have different coordinate system.
If you need to know the location of the tapped point in the node coordinate you may want to replace the line :
CGPoint touchLocation = [touch locationInView:self.view];
by :
CGPoint touchLocation = [touch locationInNode:self];
For Swift 4 and above:
if playButton.frame.contains(touch.location(in: scene!)) {
print("Play button touched")
}

How to detect subclass sprite touch

I subclass a sprite called newSprite.h / newSprite.m, and I add a sprite in it
CCSprite *nsprite = [CCSprite spriteWithFile:#"mouse.png"];
[self addChild: nsprite];
and in gamelayer.m, I add the following code
newSprite *newp = [newSprite node];
newp.position = ccp(actualX, actualY);
[self addChild:newp];
[_NSMutableArrayName addObject:newp];
when I use following code to detect which sprite i touched
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [self convertTouchToNodeSpace: touch];
for (CCSprite *target in _NSMutableArrayName) {
if (CGRectContainsPoint(target.boundingBox, location)) {
CCLOG(#"yes i am touched");
}
}
}
but it doesn't work, the sprite can not be detected, so where is the wrong? please help me, thanks
Try using this:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [self convertTouchToNodeSpace:touch];
for (CCSprite *target in _NSMutableArrayName) {
CGSize size = node.contentSize;
CGRect r = CGRectMake(0.f, 0.f,
size.width, size.height);
if (CGRectContainsPoint(r, local)) {
CCLOG(#"yes i am touched");
}
}
}
You are trying to detect touches on sub sprite and giving the bounds of parent sprite.
First, take nsprite as class variable in NewSprite to keep its reference when you call it from GameLayer. Then try changing this method like:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [self convertTouchToNodeSpace: touch];
for (CCSprite *target in _NSMutableArrayName) {
CCSize size = target.nSprite.contentSize;
CCRect rect = CCRectMake(target.position.x - size.width/2, target.position.y - size.height/2, width, height);
if (CGRectContainsPoint(rect, location)) {
CCLOG(#"yes i am touched");
}
}
}

UILabel does not register touch

I have a problem, I want to drag a label with finger, and I can't even register a touch on it. What can be a problem? self is my viewcontroller and self.label is my label.
EDIT:
My label is made programmatically over an uiimageview. UserInteractionEnables is set to YES.
I am not sure what is wrong, here is code in its entirety:
Init:
UIImage *myImage = [UIImage imageNamed:#"fish.jpg"];
UIImageView *mainImageView = [[UIImageView alloc] initWithImage:myImage];
self.view.frame = CGRectMake(0, 0, 320, 480);
self.view = mainImageView;
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
self.label.userInteractionEnabled = YES;
self.label.text = #"Hello, World";
[self.view addSubview:self.label];
Dragging:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([[touch view] isEqual:self.label])
{
NSLog(#"touch on label");
self.startLocation = [[touches anyObject] locationInView:self.label];
// startLocation is a CGPoint declare globaly in .h..and lblName is your UILabel
}
}
- (void) touchesMoved:(NSSet *)touches withEvent: (UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == self.label)
{
CGPoint pt = [[touches anyObject] previousLocationInView:self.label];
CGFloat dx = pt.x - self.startLocation.x;
CGFloat dy = pt.y - self.startLocation.y;
CGPoint newCenter = CGPointMake(self.label.center.x + dx, self.label.center.y + dy);
self.label.center = newCenter;
}
}
EDIT 2:
I have also tried this code, but it doesn't work too. I think the problem is not in these methods but with registering touches.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.startLocation = [[touches anyObject] locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint point = [[touches anyObject] locationInView:self.label];
self.label.center = CGPointMake(self.label.center.x + point.x - self.startLocation.x, self.label.center.y + point.y - self.startLocation.y);
}
Make sure you check "User Interaction Enabled" on the label in interface builder, or if you you doing it in code, userInteractionEnabled=YES
By default, UILAbel does not respond to touches. You need to enable the userInteractionEnabled property.
label.userInteractionEnabled = YES;

Detect objects, touchesmoved and UITouch

I have a UIView on where i add balls with a ID. When i touch the ball with the ID 1 i draw a dashed line follows my finger.
The problem if when i move the finger to the other balls, i don't know how to detect these balls and check the ID they have. The what i need to happen is when i touch the next ball with the ID 2, the line finish draw and stay from ball 1 to ball 2, and create new one from 2 to finger, next.. 3 etc...
The code:
#import "DrawView.h"
#implementation DrawView
- (id)init
{
self = [super initWithFrame:CGRectMake(0, 0, 1024, 768)];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.opaque = YES;
checkPointCircle = [[CheckPointCircle alloc] initWithPosX:315 posY:138 andNumber:1];
checkPointCircle.userInteractionEnabled = YES;
[self addSubview:checkPointCircle];
checkPointCircle = [[CheckPointCircle alloc] initWithPosX:706 posY:138 andNumber:2];
checkPointCircle.userInteractionEnabled = YES;
[self addSubview:checkPointCircle];
checkPointCircle = [[CheckPointCircle alloc] initWithPosX:315 posY:526 andNumber:3];
checkPointCircle.userInteractionEnabled = YES;
[self addSubview:checkPointCircle];
checkPointCircle = [[CheckPointCircle alloc] initWithPosX:706 posY:526 andNumber:4];
checkPointCircle.userInteractionEnabled = YES;
[self addSubview:checkPointCircle];
}
return self;
}
- (Line *)drawLine {
return drawLine;
}
- (void)setDrawLine:(Line *)line
{
drawLine = line;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CheckPointCircle * checkTouch = (CheckPointCircle *)touch.view;
if (touch.view.class == checkPointCircle.class) {
if ([checkTouch getObjectID] == 1) {
startTouchPoint = CGPointMake([checkTouch center].x, [checkTouch center].y);
endTouchPoint = [touch locationInView:self];
}
}
self.drawLine = [[Line alloc] initWithPoint:[touch locationInView:self]];
[self setNeedsDisplay];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Cancelado");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
UITouch *secondaryTouch = (UITouch *)[[[event touchesForView:checkPointCircle] allObjects] objectAtIndex: 0];
NSLog(#"Que toco: %# ", secondaryTouch.view);
if (touch.view.class == checkPointCircle.class) {
CheckPointCircle * checkTouch = (CheckPointCircle *)touch.view;
if ([checkTouch getObjectID] == 1) {
endTouchPoint = [touch locationInView:self];
}
}
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CheckPointCircle * checkTouch = (CheckPointCircle *)touch.view;
if (touch.view.class == checkPointCircle.class) {
if ([checkTouch getObjectID] == 1) {
endTouchPoint = [touch locationInView:self];
}
}
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[drawLine drawLineFrom:startTouchPoint to:endTouchPoint];
}
#end
How to detect the other balls to get the ID.
Can anybody help me?
Thanks!
The Apple documentation of class UIView (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/hitTest:withEvent:) lists two methods which will determine which view contains a hit/point:
– hitTest:withEvent:
– pointInside:withEvent:
Probably hitTest will help most: Its description reads "Returns the farthest descendant of the receiver in the view hierarchy (including itself) that contains a specified point.", so it even converts coordinates as needed.
If you check [self hitTest: [touch.locationInView self] withEvent: event] (or similar, no code completion here ;-)= in your touchesMoved: method, you should be able to detect when the finger is over any of the other circles.
Hope this helps, nobi

Resources