Meiner Meinung nach habe ich einige Unteransichten. Dies ist UIImageView.Verify Pixel mit Koord (iOS)
Jedes UIImageView enthält ein Bild mit Alpha-Kanal.
Dies ist ein Bild:
ich diese Methode verwenden, unten für meine Berührung in Ortsansicht Erkennung:
- (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 atY:point.y];
if (solidColor) {
dragging = YES;
oldX = touchLocation.x;
oldY = touchLocation.y;
piece = (Piece *)v;
[self.view bringSubviewToFront:piece];
break;
}
}
}
}
}
und diese Methode für alpha Pixel
- (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 = malloc(height * width * 4);
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);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
// CGFloat red = (rawData[byteIndex] ) ;
// CGFloat green = (rawData[byteIndex + 1]) ;
// CGFloat blue = (rawData[byteIndex + 2]) ;
CGFloat alpha = (rawData[byteIndex + 3]) ;
NSLog(@"%f", alpha);
free(rawData);
if(alpha==255.0) return NO;
else return YES;
}
überprüfen, ob Alpha-Pixel gegründet Ich muss andere UIImageView unter UIImageView berühren, dass ich Tapps zuvor habe.
Zum Beispiel, wenn ich UIImageView gestapelt und ich berühre auf dem ersten:
jetzt soll ich zuerst UIImageView
überprüfen, ob ich auf Alpha-Pixel berühren -> ich zum nächsten UIImageView mit diesem coord bewegen soll und überprüfen es für Alpha-Pixel auch.
Wenn zweite 3., 4. oder 5. nicht alpha-Pixel mit meiner Koordinatierung habe, sollte ich diese UIImageView wählen.
Für jetzt verifiziere ich meine Pixel - aber meine Methode gibt einen falschen Wert zurück.
danke, du antwortest mir wirklich geholfen! –