触摸坐标/位置失真

时间:2016-03-22 16:13:41

标签: uiview sprite-kit

大家好我有一个spritekit场景“菜单” 我正在加载UIView作为子视图(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类加载一个图层,我可以用手指擦除一个覆盖图,显示下面的图纸

代码似乎正在工作但触摸关闭并且似乎以某种方式“缩放”,这意味着如果我在左上角绘制触摸在正确的位置,但是向外拖动手指并且触摸变为越来越扭曲了   - 任何想要纠正这个问题的想法  (我是新手犯新手的错误)

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);
}

和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];

}

我试图转换坐标...仍然没有太多运气(也许它不是正确的事情做..

    //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];

效果/扭曲触摸的视频 https://www.youtube.com/watch?v=dMrcufcKpao

1 个答案:

答案 0 :(得分:1)

看起来你没有按照你认为将它们放在脑中的方式将你的观点分层。

现在你有这样的分层

屏幕背面
SKView
--SKScene
ScratchView

现在,当这种情况发生时,您的观点可能是大小明智的。

SKView-> UIBuilder默认(我认为600x600)
--SKScene-> SceneBuilder默认值(600x600)
ScratchView-> ScreenSize

然后您的SKView将使用AutoConstraints调整屏幕大小,但您的场景将保持在600x600,因为您没有将scaleMode设置为.ResizeFill,所以现在您有2个不同的坐标系,并且全部缩放。

我认为现在最好的选择就是将scaleMode设置为.ResizeFill(skView.scaleMode = .ResizeFill),以便所有坐标都排成一行,当您了解SpriteKit提供的内容时,您可以更好地重构您的代码以满足您的需求。