我创建简单的益智游戏并使用下一个:
我有一个名为Piece的课程。 Piece类继承自UIImageView
并包含下一个字段:
@property(nonatomic, strong) NSString *pieceName;
@property(nonatomic) CGFloat pos_x;
@property(nonatomic) CGFloat pos_y;
@property(nonatomic) BOOL snapped;
在我的游戏中,每件都有颜色(红色,绿色,蓝色和其他)。每件都有不规则的形状。这是带有alpha颜色的图像。当我点击阿尔法颜色时,我不必选择这个颜色。
为此我使用下一个方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSArray *views = [self.view subviews];
for (UIView *v in views) {
if([v isKindOfClass:[Piece class]]){
if (CGRectContainsPoint(v.frame, touchLocation) && ((Piece *)v).snapped == FALSE) {
UITouch* touchInPiece = [touches anyObject];
CGPoint point = [touchInPiece locationInView:(Piece *)v];
BOOL solidColor = [self verifyAlphaPixelImage:(Piece *)v atX:point.x * resolution atY:point.y * resolution];
if (solidColor) {
NSArray *tempViews = [self.view subviews];
[tempViews reverseObjectEnumerator];
for (UIView *tv in tempViews) {
if (tv == v) {
[tv setExclusiveTouch:YES];
dragging = YES;
oldX = touchLocation.x;
oldY = touchLocation.y;
piece = (Piece *)v;
[self.view bringSubviewToFront:piece];
break;
}
}
}
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (dragging) {
CGRect frame = piece.frame;
frame.origin.x = piece.frame.origin.x + touchLocation.x - oldX;
frame.origin.y = piece.frame.origin.y + touchLocation.y - oldY;
if ([self verifyOutOfScopeCoordX:touchLocation.x CoordY:touchLocation.y]) {
piece.frame = frame;
}
[self snapPiece];
}
oldX = touchLocation.x;
oldY = touchLocation.y;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
dragging = NO;
}
- (BOOL)verifyAlphaPixelImage:(Piece *)image atX:(int)x atY:(int)y{
CGImageRef imageRef = [image.image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
int byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
/*
NSLog(@"red: %f", red);
NSLog(@"green: %f", green);
NSLog(@"blue: %f", blue);
NSLog(@"alpha: %f", alpha);
*/
free(rawData);
if(alpha==0 && red == 0 && green == 0 && blue == 0) return NO;
else return YES;
}
我没有任何问题地移动我的作品。但是有时我在这一行verifyAlphaPixelImage
中使用CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
方法会出现奇怪错误,我得EXC_BAD_ACCESS
。
verifyAlphaPixelImage - 我需要这种方法来分析alpha像素。如果我点击alpha像素,我就不必选择这个。
现在主要的问题是EXC_BAD_ACCESS
在线,我使用rawData。
现在我用它:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSArray *views = [self.view subviews];
for (UIView *v in views) {
if([v isKindOfClass:[Piece class]]){
CGFloat pos_x = touchLocation.x - v.frame.origin.x;
CGFloat pos_y = touchLocation.y - v.frame.origin.y;
if (CGRectContainsPoint(v.frame, touchLocation) && ((Piece *)v).snapped == FALSE) {
BOOL solidColor = [self verifyAlphaPixelImage:(Piece *)v atX:pos_x * resolution atY:pos_y * resolution];
if (solidColor) {
NSArray *tempViews = [self.view subviews];
[tempViews reverseObjectEnumerator];
for (UIView *tv in tempViews) {
if (tv == v) {
[tv setExclusiveTouch:YES];
dragging = YES;
oldX = touchLocation.x;
oldY = touchLocation.y;
piece = (Piece *)v;
[self.view bringSubviewToFront:piece];
break;
}
}
}
}
}
}
}
这项工作......
答案 0 :(得分:1)
请注意,在您的第一种方法中,您使用两个不同的触摸用于相同的目的。
首先你做:
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
用于检查您的触摸是否在片段视图内。
但接下来你再接触一下:
UITouch* touchInPiece = [touches anyObject];
CGPoint point = [touchInPiece locationInView:(Piece *)v];
如果您有多个触摸事件,第二次触摸可能不是第一次触摸,甚至可能不在检测到第一次触摸的视图内。 对于第一次触摸检测到的片段视图,第二次触摸可能超出范围