大家好我有一個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
我最後做了改變@implementation GameViewController,刪除 - (void)viewdidload,用 - (void)viewWillLayoutSubviews替換,scene.scaleMode = SKSceneScaleModeAspectFill;這是因爲在此時系統調用viewDidLoad方法時,調用層次結構中不會添加任何視圖。因此,視圖的大小和方向無法調整(至少這是我的理解??) 這似乎解決了這個問題。感謝龍的騎士,它讓我走上了正確的解決之道! – StackBuddy
有點相同的方法,但方面填充可能不適合你在各種設備上,我會測試一個4s,5~6s(只有一個需要),和iPad – Knight0fDragon