scénario:
J'ai un ensemble de Comment puis-je vérifier si l'utilisateur tapé près de l'un des chemins? P> Voici ce que j'avais travail: p> cgpath code> s. Ils sont pour la plupart des lignes seulement (à savoir pas de formes fermées). Ils sont dessinés sur l'écran dans un
UIView code> méthode de tirage. P>
if (CGPathContainsPoint(clickArea,NULL,point,NO)) { ...
3 Réponses :
Eh bien, j'ai compris une réponse. Il utilise CGPATHAPPLY:
clickArea = CGPathCreateMutable(); CGPathApply(path,clickArea,&createClickArea); void createClickArea (void *info, const CGPathElement *elem) { CGPathElementType type = elem->type; CGMutablePathRef path = (CGMutablePathRef)info; static CGPoint last; static CGPoint subpathStart; switch (type) { case kCGPathElementAddCurveToPoint: case kCGPathElementAddQuadCurveToPoint: break; case kCGPathElmentCloseSubpath: case kCGPathElementMoveToPoint: { CGPoint p = type == kCGPathElementAddLineToPoint ? elem->points[0] : subpathStart; if (CGPointEqualToPoint(p,last)) { return; } CGFloat rad = atan2(p.y - last.y, p.x - last.x); CGFloat xOff = CLICK_DIST * cos(rad); CGFloat yOff = CLICK_DIST * sin(rad); CGPoint a = CGPointMake(last.x - xOff, last.y - yOff); CGPoint b = CGPointMake(p.x + xOff, p.y + yOff); rad += M_PI_2; xOff = CLICK_DIST * cos(rad); yOff = CLICK_DIST * sin(rad); CGPathMoveToPoint(path, NULL, a.x - xOff, a.y - yOff); CGPathAddLineToPoint(path, NULL, a.x + xOff, a.y + yOff); CGPathAddLineToPoint(path, NULL, b.x + xOff, b.y + yOff); CGPathAddLineToPoint(path, NULL, b.x - xOff, b.y - yOff); CGPathCloseSubpath(path); last = p; break; } case kCGPathElementMoveToPoint: subpathStart = last = elem->points[0]; break; } }
Merci beaucoup pour avoir posté ça! Je n'aurais pas moi-même un indice à ce sujet. Bien que la réponse acceptée fonctionne bien, mais cette fonction m'a essentiellement sauvé car il vous donne une flexibilité - je peux traiter des éléments de chemin de chemin différemment: exactement ce dont j'avais besoin. Merci encore!
dans iOS 5.0 et plus tard, cela peut être fait plus simplement en utilisant cgpathCreatecopyBystrokingPath code>:
Dans Swift 3.0: Chemin.copy (CauseWithWithWidth: CGFLOAT ...) Code>
dans SWIFT
let area = stroke.copy(strokingWithWidth: 15, lineCap: .round, lineJoin: .round, miterLimit: 1) if (area.contains(point)) { ... }