2010-07-24 60 views
4

我有三個PNG的「320 x 480px」,我加載到單獨的UIImageViews。 PNG的名字是身體,嘴巴,帽子。通過將圖像堆疊在一起,我創建了一個角色,其身體部位可以輕鬆地換出。見照片>png透明度註冊觸摸事件iphone?

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

我的問題是,當你摸頂最UIImageView的整幅圖像,包括透明度註冊觸摸事件。我想要做的是讓觸摸事件只註冊在不透明的png部分。因此,使用戶可以與所有三個UIImageView進行交互。

我敢肯定這很簡單,但我是iphone新手,我似乎無法弄清楚。


更新 所以我已經意識到,以完成我想要做的最簡單的方法就是通過創建循環,併爲每個png格式,然後獲取觸摸事件發生的像素顏色數據創建上下文。如果像素代表一個透明區域,我將移動到下一個圖像並嘗試相同的操作。這項工作,但只有第一次。比如我第一次點擊主視圖我得到這個輸出

2010-07-26 15:50:06.285 colorTest [21501:207]帽子
2010-07-26 15:50:06.286 colorTest [21501:207]偏移量:227024顏色:RGB A 0 0 0 0
2010-07-26 15:50:06.293 colorTest [21501:207] mouth
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] body
2010-07-26 15:50:06.299 colorTest [21501: 207]偏移量:227024顏色:RGB A 255 255 255 255

這正是我想看到的。但是,如果我再次點擊相同的區域,我會得到。

2010-07-26 15:51:21.625 colorTest [21501:207]帽子
2010-07-26 15:51:21.626 colorTest [21501:207]偏移量:283220種顏色:RGB甲255 255 255 255
2010-07-26 15:51:21.628 colorTest [21501:207] mouth
2010-07-26 15:51:21.628 colorTest [21501:207] offset:283220 colors:RGB A 255 255 255 255
2010-07-26 15:51:21.630 colorTest [21501:207]體
2010-07-26 15:51:21.631 colorTest [21501:207]偏移量:283220種顏色:RGB甲255 255 255 255

這是我使用的代碼;

觸摸事件在MAINVIEW存在該應用

- (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]; 

} 

} 

它使得在延伸的ImageView 該函數返回的TouchEvent下像素的顏色的自定義類的方法的調用。

- (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會觸發一個觸摸事件。順便說一句,這裏是我用來把它們放在視圖中的代碼。

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行代碼更有可能得到答案。順便說一句,如果你不能得到一個體面的答案,幾天後,你可以提供一個賞金。其次,請創建一個帳戶;您在此問題中創建了兩個未註冊的帳戶。最後,當你需要更新時,不要爲你的問題添加答案。 StackOverflow不是論壇或討論網站。祝你好運得到你需要的幫助! – Will 2010-09-20 12:10:56

回答

0

第一關:

可以使用透明,但隱藏的圖像可能會滿足您的需求。

您可以隱藏使用以下命令將圖片:[myImage setHidden:YES];myImage.hidden = YES;

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

這可以確保你的形象是不是點擊透明的,因爲看myImage.hidden==NO檢查圖像被隱藏。