菜鸟教程小白 发表于 2022-12-13 10:16:48

ios - 检测使用变换的层是否包含接触点


                                            <p><p>我一直在用这个敲我的头。我有一个基于 <a href="https://stackoverflow.com/questions/13528898/how-can-i-draw-an-arrow-using-core-graphics" rel="noreferrer noopener nofollow">this post</a> 的箭头.当我点击包含箭头的 View 时,我需要知道箭头层是否包含接触点。我已经搜索了两天的解决方案,但还没有找到任何可行的方法。如果有人能指出我正确的方向,那将不胜感激一千倍。 </p>

<pre><code>@implementation ArrowView
{
    CGFloat headLength;
    UIBezierPath *path;
}

- (id) initWithFrame:(CGRect)frame withColor: (UIColor *) color withWeight: (CGFloat) weight withStartPoint: (CGPoint) startPoint withEndPoint: (CGPoint) endPoint {

    if (self = ) {
      _arrowColor = color;
      _weight = weight;
      _startPoint = startPoint;
      _endPoint = endPoint;

      self.userInteractionEnabled = YES;

      UITapGestureRecognizer *tap = [ initWithTarget:self action:@selector(tapped:)];
      ;
    }

    return self;
}

- (void) tapped: (UITapGestureRecognizer *) sender {
    CGPoint location = ;

    if () {
      ;
    }
}

- (void) drawRect:(CGRect)rect {
    ;

    ;
    ;
    ;

    _tailWidth = 5 + _weight;
    _headWidth = 25 + _weight;
    headLength = 40;

    path = [UIBezierPath dqd_bezierPathWithArrowFromPoint:(CGPoint)_startPoint
                                                toPoint:(CGPoint)_endPoint
                                                tailWidth:(CGFloat)_tailWidth
                                                headWidth:(CGFloat)_headWidth
                                             headLength:(CGFloat)headLength];
    ;

    CGFloat width = _endPoint.x - _startPoint.x;
    CGFloat height = _endPoint.y - _startPoint.y;

    CGFloat offset = 5;
    if (ABS(width) &lt; offset) {
      width = width &gt; 0 ? offset : -offset;
    }

    if (ABS(height) &lt; offset) {
      height = height &gt; 0 ? offset : -offset;
    }

    _arrowLayer = ;
    _arrowLayer.bounds = CGRectMake(_startPoint.x, _startPoint.y, width, height);
    _arrowLayer.position = CGPointMake(((_endPoint.x - _startPoint.x) / 2), (_endPoint.y - _startPoint.y) / 2);

    _arrowLayer.path = path.CGPath;
    _arrowLayer.fillColor = _arrowColor.CGColor;
    //_arrowLayer.backgroundColor = .CGColor;

    if (!_isSelected) {
      _arrowLayer.strokeColor = _arrowColor.CGColor;
    } else {
      if (_arrowColor != ) {
            _arrowLayer.strokeColor = .CGColor;
      } else {
            _arrowLayer.strokeColor = .CGColor;
      }
    }

    _arrowLayer.borderColor = .CGColor;
    _arrowLayer.borderWidth = 1;

    ;
}

- (void) setIsSelected: (BOOL) isSelected {
    _isSelected = isSelected;
    ;
}

- (void) updateStartPoint: (CGPoint) startPoint {
    _startPoint = startPoint;
    ;
}

- (void) updateEndPoint: (CGPoint) endPoint {
    _endPoint = endPoint;
    ;
}

- (void) updateColor: (UIColor *) color {
    _arrowColor = color;
    ;
}

- (void) updateWeight: (CGFloat) weight {
    _weight = weight;
    ;
}

@end

#define kArrowPointCount 7

@implementation UIBezierPath (dqd_arrowhead)

+ (UIBezierPath *)dqd_bezierPathWithArrowFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint tailWidth:(CGFloat)tailWidth headWidth:(CGFloat)headWidth headLength:(CGFloat)headLength {

    CGFloat length = hypotf(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
    CGPoint points;
    ;
    CGAffineTransform transform = ;
    CGMutablePathRef cgPath = CGPathCreateMutable();
    CGPathAddLines(cgPath, &amp;transform, points, sizeof points / sizeof *points);
    CGPathCloseSubpath(cgPath);
    UIBezierPath *uiPath = ;
    CGPathRelease(cgPath);

    return uiPath;
}

+ (void)dqd_getAxisAlignedArrowPoints:(CGPoint)points
                            forLength:(CGFloat)length
                            tailWidth:(CGFloat)tailWidth
                            headWidth:(CGFloat)headWidth
                           headLength:(CGFloat)headLength {
    CGFloat tailLength = length - headLength;
    points = CGPointMake(0, tailWidth / 2);
    points = CGPointMake(tailLength, tailWidth / 2);
    points = CGPointMake(tailLength, headWidth / 2);
    points = CGPointMake(length, 0);
    points = CGPointMake(tailLength, -headWidth / 2);
    points = CGPointMake(tailLength, -tailWidth / 2);
    points = CGPointMake(0, -tailWidth / 2);
}

+ (CGAffineTransform)dqd_transformForStartPoint:(CGPoint)startPoint
                                       endPoint:(CGPoint)endPoint
                                       length:(CGFloat)length {
    CGFloat cosine = (endPoint.x - startPoint.x) / length;
    CGFloat sine = (endPoint.y - startPoint.y) / length;
    return (CGAffineTransform){ cosine, sine, -sine, cosine, startPoint.x, startPoint.y };
}

@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我找到了一个替代解决方案。 SpaceDog 让我以不同的方式思考这种情况。我只需要知道是否触摸了有颜色的区域。我找到了这篇文章并将其翻译为Objective C。它工作得很好。虽然我更喜欢检测点在箭头内,但这也适用于我的目的。谢谢大家的回复。我很感激。</p>

<p> <a href="https://stackoverflow.com/questions/27923232/how-to-know-that-if-the-only-visible-area-of-a-png-is-touched-in-xcode-swift-o" rel="noreferrer noopener nofollow">Post I got the code below from.</a> </p>

<pre><code>- (void) tapped: (UITapGestureRecognizer *) sender {
    CGPoint location = ;
    CGFloat p = ;
    if (p &gt; 0) {
      ;
    }
}

- (CGFloat) alphaFromPoint: (CGPoint) point {
    UInt8 pixel = {0,0,0,0};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo alphaInfo = (CGBitmapInfo)kCGImageAlphaPremultipliedLast;


    CGContextRef context = CGBitmapContextCreate(&amp;pixel, 1, 1, 8, 4, colorSpace, alphaInfo);

    CGContextTranslateCTM(context, -point.x, -point.y);

    ;
    CGFloat floatAlpha = pixel;
    return floatAlpha;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 检测使用变换的层是否包含接触点,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32367713/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32367713/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 检测使用变换的层是否包含接触点