2016-03-22 122 views
0

大家好我有一個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

看起來你是不是分層你的意見,你在想你的方式是在你的腦袋解僱他們。

現在你擁有了它分層這樣

回到屏幕
SKView
--SKScene
ScratchView

現在,當發生這種情況,這是你的看法是怎樣大概佈局尺寸明智。

SKView-> UIBuilder默認(600×600我認爲)
--SKScene-> SceneBuilder默認(600×600)
ScratchView->屏幕尺寸

然後你SKView將調整到屏幕尺寸與AutoConstraints,但你的場景將停留在600x600,因爲您沒有將scaleMode設置爲.ResizeFill,所以現在您有2個不同的座標系,縮放全部關閉。

我認爲你現在的最佳選擇是將scaleMode設置爲.ResizeFill(skView.scaleMode = .ResizeFill),以便所有座標排成一列,當你瞭解更多關於SpriteKit所提供的內容時,可以重構代碼更好地滿足您的需求。

+0

我最後做了改變@implementation GameViewController,刪除 - (void)viewdidload,用 - (void)viewWillLayoutSubviews替換,scene.scaleMode = SKSceneScaleModeAspectFill;這是因爲在此時系統調用viewDidLoad方法時,調用層次結構中不會添加任何視圖。因此,視圖的大小和方向無法調整(至少這是我的理解??) 這似乎解決了這個問題。感謝龍的騎士,它讓我走上了正確的解決之道! – StackBuddy

+0

有點相同的方法,但方面填充可能不適合你在各種設備上,我會測試一個4s,5~6s(只有一個需要),和iPad – Knight0fDragon