2010-07-24 5 views
4

나는 3 개의 png의 "320 x 480px"를 가지고 있는데, 이것은 별도의 UIImageViews에로드하고있다. PNG의 이름은 몸, 입, 모자입니다. 서로의 위에 이미지를 쌓아 올리면 신체 부분을 쉽게 바꿀 수있는 캐릭터를 만듭니다. 참조 사진>png 투명도 등록 터치 이벤트 iphone?

http://www.1976inc.com/dev/iphone/beast.jpg

내 문제는 당신이 터치 할 때 투명도를 포함한 전체 이미지가 터치 이벤트를 등록 UIImageView에 가장 상단. 내가하고 싶은 것은 터치 이벤트가 투명하지 않은 PNG 부분에만 등록되도록 만드는 것입니다. 따라서 사용자가 세 개의 UIImageViews와 상호 작용할 수 있도록하십시오.

저는 이것이 간단하지만 아이폰 개발을 처음 접했을뿐입니다. 그래서 나는 내가 원하는 것을 달성 할 수있는 가장 쉬운 방법을 깨달았다 업데이트


을 통해 루프를 만들고 각 터치 이벤트가 발생한 픽셀의 색상 데이터를 얻을 후 PNG로에 대한 컨텍스트를 만드는 것입니다 . 픽셀이 투명한 영역을 나타내는 경우 다음 이미지로 이동하여 같은 것을 시도합니다. 이것은 처음에는 작동합니다. 06.285 colorTest [21,501 : 207] 모자
2010-07-26 15 : 50 : 50 : 예를 들어 I 메인 뷰 클릭 처음 I이 출력을

2010-07-26 15 얻을 06.286 colorTest [21501 : 207] 오프셋 : 227024 색 : RGB A 0 0 0 0
2010-07-26 15:50 : 06.293 colorTest [21501 : 207] 입
2010-07-26 15:50 : 06.293 colorTest [ 21501 : 207] 오프셋 : 227024 색 : RGB A 0 0 0 0
2010-07-26 15:50 : 06.298 colorTest [21501 : 207] 본문
2010-07-26 15:50 : 06.299 colorTest [21501 : 207] 오프셋 : 227024 색 : RGB A 255 255 255 255

정확하게보고 싶습니다. 그러나 같은 영역을 다시 클릭하면 얻을 수 있습니다. 51 :

2010-07-26 15

colorTest 21.625 [21,501 : 207] 모자
2010-07-26 15 : 51 : 21.626 colorTest [21,501 : 207] 오프셋 : 283,220 컬러 : RGB 255 255 255 255
2010-07-26 15:51:21.628 colorTest [21501 : 207] 입
2010-07-26 15:51:21.628 colorTest [21501 : 207] 오프셋 : 283220 색 : RGB A 255 255 255 255 51 : 15 2010-07-26
colorTest 21.630 [21,501 : 207] 체
2010-07-26 15 : 51 : 21.631 colorTest [21,501 : 207] 오프셋 : 283,220 컬러 : RGB 255 255 255 255을

다음은 현재 사용중인 코드입니다.

터치 이벤트 앱

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
NSLog(@"Touched balls"); 
UITouch *touch = [touches anyObject]; 
CGPoint point = [touch locationInView:self.view]; 

UIColor *transparent = [UIColor colorWithRed:0 green:0 blue:0 alpha:0]; 

for(viewTest *currentView in imageArray){ 
    //UIColor *testColor = [self getPixelColorAtLocation:point image:currentView.image]; 
    [currentView getPixelColorAtLocation:point]; 

} 

} 

그것은 기능이 touchEvent 아래에있는 픽셀의 색상을 반환 이미지 뷰 확장하는 사용자 정의 클래스의 메소드에 대한 호출을위한 MAINVIEW 존재합니다.빠른 응답

- (UIColor*) getPixelColorAtLocation:(CGPoint)point 
{ 
UIColor *color = nil; 
CGImageRef inImage = self.image.CGImage; 

CGContextRef context = [self createARGBBitmapContextFromImage:inImage]; 

if(context == NULL) return nil; 

size_t w = CGImageGetWidth(inImage); 
size_t h = CGImageGetHeight(inImage); 
CGRect rect = {{0,0},{w,h}}; 

// Draw the image to the bitmap context. Once we draw, the memory 
// allocated for the context for rendering will then contain the 
// raw image data in the specified color space. 
CGContextDrawImage(context, rect, inImage); 

// Now we can get a pointer to the image data associated with the bitmap 
// context. 
unsigned char* data = CGBitmapContextGetData (context); 
if (data != NULL) { 
    //offset locates the pixel in the data from x,y. 
    //4 for 4 bytes of data per pixel, w is width of one row of data. 
    int offset = 4*((w*round(point.y))+round(point.x)); 
    int alpha = data[offset]; 
    int red = data[offset+1]; 
    int green = data[offset+2]; 
    int blue = data[offset+3]; 
    NSLog(@"%@",name); 
    NSLog(@"offset: %i colors: RGB A %i %i %i %i ",offset,red,green,blue,alpha); 
    color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)]; 
} 

// When finished, release the context 
CGContextRelease(context); 

// Free image data memory for the context 
if (data) { free(data); } 

return color; 
} 

- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage { 

CGContextRef context = NULL; 
CGColorSpaceRef colorSpace; 
void *   bitmapData; 
int    bitmapByteCount; 
int    bitmapBytesPerRow; 

// Get image width, height. We'll use the entire image. 
size_t pixelsWide = CGImageGetWidth(inImage); 
size_t pixelsHigh = CGImageGetHeight(inImage); 

// Declare the number of bytes per row. Each pixel in the bitmap in this 
// example is represented by 4 bytes; 8 bits each of red, green, blue, and 
// alpha. 
bitmapBytesPerRow = (pixelsWide * 4); 
bitmapByteCount  = (bitmapBytesPerRow * pixelsHigh); 

// Use the generic RGB color space. 
colorSpace = CGColorSpaceCreateDeviceRGB();//CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); 
if (colorSpace == NULL) 
{ 
    fprintf(stderr, "Error allocating color space\n"); 
    return NULL; 
} 

// Allocate memory for image data. This is the destination in memory 
// where any drawing to the bitmap context will be rendered. 
bitmapData = malloc(bitmapByteCount); 
if (bitmapData == NULL) 
{ 
    fprintf (stderr, "Memory not allocated!"); 
    CGColorSpaceRelease(colorSpace); 
    return NULL; 
} 

// Create the bitmap context. We want pre-multiplied ARGB, 8-bits 
// per component. Regardless of what the source image format is 
// (CMYK, Grayscale, and so on) it will be converted over to the format 
// specified here by CGBitmapContextCreate. 
context = CGBitmapContextCreate (bitmapData, 
      pixelsWide, 
      pixelsHigh, 
      8,  // bits per component 
      bitmapBytesPerRow, 
      colorSpace, 
      kCGImageAlphaPremultipliedFirst); 
if (context == NULL) 
{ 
    free (bitmapData); 
    fprintf (stderr, "Context not created!"); 
} 

// Make sure and release colorspace before returning 
CGColorSpaceRelease(colorSpace); 

return context; 
} 

업데이트

2 감사합니다. 내가 너를 따라갈 지 모르겠다. 숨김을 true로 변경하면 UIImageView "레이어"가 숨겨집니다. 내가 원하는 건 png의 투명한 부분이 터치 이벤트를 등록하지 않는 것입니다. 그래서 예를 들어, 내가 그 글에 포함 된 이미지를 본다면. 웜을 클릭하거나 "같은 png의 모든 부분"인 줄기 나 잎을 클릭하면 해당 ImageView에 의해 터치 이벤트가 시작되지만 원을 터치하면 해당 ImageView에서 터치 이벤트가 시작됩니다. 여기 BTW보기에서 그들을 배치하는 데 사용하는 코드입니다.

UIView *tempView = [[UIView alloc] init]; 
[self.view addSubview:tempView]; 


UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"body.png"] ]; 
[imageView1 setUserInteractionEnabled:YES]; 
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mouth.png"] ]; 
[imageView2 setUserInteractionEnabled:YES]; 
UIImageView *imageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hat.png"] ]; 
[imageView3 setUserInteractionEnabled:YES]; 

[tempView addSubview:imageView1]; 
[tempView addSubview:imageView2]; 
[tempView addSubview:imageView3]; 

[self.view addSubview:tempView]; 
+0

진지하게 말하자면, 이것은 누구에게나 파싱하기에 조금 있습니다. 아주 좁은 도메인에서 매우 구체적인 질문을한다면, 1500 줄의 코드를 쓸어 버리는 것보다 대답을 얻는 것이 훨씬 더 쉽습니다. BTW, 당신이 이것에 대한 괜찮은 대답을 얻을 수 없다면, 며칠 후 당신은 그것에 현상금을 제공 할 수 있습니다. 둘째, 계정을 만드십시오. 이 질문에 등록되지 않은 두 개의 계정을 만들었습니다. 마지막으로, 업데이트가 필요할 때 질문에 답변을 추가하지 마십시오. StackOverflow는 포럼이나 토론 웹 사이트가 아닙니다. 필요한 도움을 얻으십시오. – Will

답변

0

첫째는 :

당신은 투명성을 사용하지만 이미지가 아마 당신의 요구에 맞게됩니다 숨어 있습니다.

는 다음과 같은 명령을 사용하여 이미지를 숨길 수 있습니다 :이 이미지를 숨기거나하지 않은 경우 myImage.hidden==NO 여부를 확인하기 때문에 이미지 클릭에 투명하지 확인합니다 [myImage setHidden:YES]; 또는 myImage.hidden = YES;

if (CGRectContainsPoint(myImage.frame, touchPosition)==true && myImage.hidden==NO) 
{ 
} 

.