我正在尝试使用UIImage
作为大理石游戏的路径。我正在使用CoreMotion
和QuartzCore
处理在屏幕上移动大理石。 UIImage
是一个简单的黑白图像,其中路径为黑色,其余图像为白色背景。我有移动大理石的代码,但是当我尝试检查其下面的路径图像的颜色时,大理石上的动画停止并且它不再移动。如果我注释掉确定路径图像像素颜色的代码行,那么大理石就会移动得很好。当我检查像素的颜色时,我缺少什么来保持大理石移动?
- (void)viewDidLoad {
[super viewDidLoad];
// Movement of marble1
self.lastUpdateTime = [[NSDate alloc] init];
self.curPoint = CGPointMake(443, 350);
self.motionManager = [[CMMotionManager alloc] init];
self.queue = [[NSOperationQueue alloc] init];
self.motionManager.accelerometerUpdateInterval = kUpdateInterval;
[self.motionManager startAccelerometerUpdatesToQueue:self.queue withHandler:
^(CMAccelerometerData *accelerometerData, NSError *error) {
[(id) self setAcceleration:accelerometerData.acceleration];
[self performSelectorOnMainThread:@selector(update) withObject:nil waitUntilDone:NO];
}];
}
- (void)update {
NSTimeInterval secondsSinceLastDraw = -([self.lastUpdateTime timeIntervalSinceNow]);
self.marble1YVelocity = self.marble1YVelocity - (self.acceleration.x * secondsSinceLastDraw);
self.marble1XVelocity = self.marble1XVelocity - (self.acceleration.y * secondsSinceLastDraw);
CGFloat xDelta = secondsSinceLastDraw * self.marble1XVelocity * 500;
CGFloat yDelta = secondsSinceLastDraw * self.marble1YVelocity * 500;
self.curPoint = CGPointMake(self.curPoint.x + xDelta,
self.curPoint.y + yDelta);
[self moveMarble1];
self.lastUpdateTime = [NSDate date];
}
- (void)moveMarble1 {
[self collisionWithWalls];
[self collisionWithBoundaries];
self.prevPoint = self.curPoint;
CGRect frame = self.marble1.frame;
frame.origin.x = self.curPoint.x;
frame.origin.y = self.curPoint.y;
self.marble1.frame = frame;
}
- (void)collisionWithWalls {
CGRect frame = self.marble1.frame;
frame.origin.x = self.curPoint.x;
frame.origin.y = self.curPoint.y;
NSLog(@"x=%f,y=%f",frame.origin.x,frame.origin.y);
CALayer *pathLayer = [self.Path.layer presentationLayer];
//if I comment out the following line the marble moves with no issues on the screen
NSLog(@"Color of path is %@",[self pixelColorInImage:pathLayer atX:self.curPoint.x atY:self.curPoint.y]);
}
- (UIColor*)pixelColorInImage:(CALayer*)layer atX:(int)x atY:(int)y {
UIImage* image=[self imageFromLayer:layer];
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
const UInt8* data = CFDataGetBytePtr(pixelData);
int pixelInfo = ((image.size.width * y) + x ) * 4; // 4 bytes per pixel
UInt8 red = data[pixelInfo + 0];
UInt8 green = data[pixelInfo + 1];
UInt8 blue = data[pixelInfo + 2];
UInt8 alpha = data[pixelInfo + 3];
CFRelease(pixelData);
return [UIColor colorWithRed:red/255.0f
green:green/255.0f
blue:blue/255.0f
alpha:alpha/255.0f];
}
- (UIImage *)imageFromLayer:(CALayer *)layer{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions([layer frame].size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext([layer frame].size);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}