我有一個與切割圖像有關的問題,以知道用戶點擊哪個部分。對於一些人來說,這是一個相當簡單的練習,我想,但是我一直在爲此感到頭痛,無法找到合適的方法。如何根據座標切割圖像
我想這樣做的是挖掘這個圖像上的身體部位,並有設備告訴我它是什麼身體的一部分:
但我對如何編寫這個絕對沒有線索。
我一直在考慮爲每個身體部位設置一個輪廓CGPoint數組,但如何獲得這些? Photoshop的座標?然後將接觸點計算到最近的CGPoint上?
有人給我提出了在不同區域切割圖像的想法,每個身體部位都有不同的圖像。但問題在於,每個圖像都是一個矩形,這使得很難知道究竟是什麼感動。還需要一組點,特別是對於重疊的圖像。
另一個人告訴我,這隻能使用SpriteKit。
讓我知道,智能一瞥:)
UPDATE: 我想它了!我做了很多包含指向手陣列,腿,...一個例子:
CGPoint p1 = CGPointMake(x + 432, y + 200);
CGPoint p2 = CGPointMake(x + 523, y + 188);
CGPoint p3 = CGPointMake(x + 530, y + 277);
CGPoint p4 = CGPointMake(x + 432, y + 277);
CGPoint p5 = CGPointMake(x + 523, y + 367);
CGPoint p6 = CGPointMake(x + 432, y + 354);
CGPoint p7 = CGPointMake(x + 325, y + 355);
CGPoint p8 = CGPointMake(x + 296, y + 362);
NSArray *handL = [NSArray arrayWithObjects:
[NSValue valueWithCGPoint:p1],
[NSValue valueWithCGPoint:p2],
[NSValue valueWithCGPoint:p3],
[NSValue valueWithCGPoint:p4], nil];
[shapes addObject:@{@"name":NSLocalizedString(@"Left Hand", @""), @"points":handL}];
NSArray *handR = [NSArray arrayWithObjects:
[NSValue valueWithCGPoint:p5],
[NSValue valueWithCGPoint:p6],
[NSValue valueWithCGPoint:p7],
[NSValue valueWithCGPoint:p8], nil];
[shapes addObject:@{@"name":NSLocalizedString(@"Right Hand", @""), @"points": handR}];
後來在我讀這些值和層他們:
NSArray *shapes = [[NumbersHelper sharedNumbersHelper] getScreenThreeShapes];
for (int outside = 0; outside < [shapes count]; outside++) {
CAShapeLayer *shape = [[CAShapeLayer alloc] init];
[self.scrollView.layer addSublayer:shape];
shape.opacity = 0.5;
shape.lineWidth = 2;
shape.lineJoin = kCALineJoinRound;
NSArray *points = [[[shapes valueForKey:@"points"] allObjects] objectAtIndex:outside];
UIBezierPath *path = [[UIBezierPath alloc] init];
for (int inside = 0; inside < points.count; inside++) {
if (inside == 0) {
[path moveToPoint:[[points objectAtIndex:inside] CGPointValue]];
} else {
[path addLineToPoint:[[points objectAtIndex:inside] CGPointValue]];
}
}
[path closePath];
shape.path = [path CGPath];
shape.name = [[[shapes valueForKey:@"name"] allObjects] objectAtIndex:outside];
}
當用戶點擊我做到這一點的看法:
- (void)tappedOnView:(UITapGestureRecognizer *)sender
{
if ([sender locationInView:self.scrollView].x > 2048 && [sender locationInView:self.scrollView].x < 3072) {
screenTwoTouchedPoint = [sender locationInView:self.scrollView];
CGPoint touchPoint = [sender locationInView:self.scrollView];
NSString *name;
for (id sublayers in self.scrollView.layer.sublayers) {
if ([sublayers isKindOfClass:[CAShapeLayer class]]) {
CAShapeLayer *shape = sublayers;
if (CGPathContainsPoint(shape.path, 0, touchPoint, YES)) {
name = shape.name;
break;
}
}
}
[self tappedBody:sender onPart:name];
}
}
出於好奇,你用它來做什麼? :) – 2015-03-02 09:30:42
看看這個 - http://stackoverflow.com/questions/27169845/how-to-detect-tap-on-small-nearest-buttons-in-ios/27170107#27170107 – Kampai 2015-03-02 09:53:06
@ iosDev82哈哈,可以不會告訴你,但它不適合人體:) – Yoko 2015-03-02 10:42:50