首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >触摸坐标/位置失真

触摸坐标/位置失真
EN

Stack Overflow用户
提问于 2016-03-23 00:13:41
回答 1查看 40关注 0票数 0

大家好,我有一个spritekit场景“菜单”,我正在加载一个UIView作为一个子视图(CGScratchViewController.xib)

代码语言:javascript
运行
复制
-(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

代码语言:javascript
运行
复制
//  Created by Olivier Yiptong on 11-01-11.

//

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
-(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];

}

我尝试过转换coordinates...still,但运气不是很好(也许无论如何这都不是正确的做法)。

代码语言:javascript
运行
复制
    //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

EN

Stack Overflow用户

发布于 2016-03-23 09:29:11

看起来你并没有像你想象的那样将你的观点层次化。

现在你可以像这样分层了

屏幕背面

SKView

--SKScene

ScratchView

现在,当这种情况发生时,这里是你的视图可能是如何布局大小明智的。

SKView->UIBuilder默认值(我想是600x600)

--SKScene-> SceneBuilder默认值(600x600)

ScratchView->ScreenSize

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

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36159649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档