UIBezierPath轮廓由于某些原因一直出现在视图周围



下面的代码是我用来创建我的子视图"theSubview",我把它添加到父视图"parentView"。

假设parentView有一个框架{{0.0,0.0},{100.0,100.0}}和subview有框架{{20.0,20.0},{20.0,20.0}}

问题是,当我绘制完成后,我不仅结束了蓝色箭头标记,而且在subview的框架上也有一个蓝色的轮廓。

你知道我做错了什么吗?

谢谢!


// theSubview
// My UIView subclass that is added to another view
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.opaque = NO;
    }
    return self;
}
- (void)drawRect:(CGRect)rect {
    [self drawArrow];        
}
- (void)drawArrow {    
    CGRect arrowRect;
    arrowRect = self.bounds;
    UIBezierPath *arrowPath = [UIBezierPath bezierPathWithRect:arrowRect];
//    UIColor *backgrColor = [UIColor grayColor];
//    [backgrColor setFill];
//    [arrowPath fillWithBlendMode:kCGBlendModeNormal alpha:0.9f];
    UIColor *strokeColor = [UIColor blueColor];
    [strokeColor setStroke];

    CGFloat thirdOfWidth = floorf(CGRectGetWidth(self.bounds) / 3);
    CGFloat thirdOfHeight = floorf(CGRectGetHeight(self.bounds) / 3);
    [arrowPath moveToPoint:CGPointMake(thirdOfWidth, thirdOfHeight)];
    [arrowPath addLineToPoint:CGPointMake(thirdOfWidth * 2, thirdOfHeight + (floorf(thirdOfHeight/2)))];
    [arrowPath addLineToPoint:CGPointMake(thirdOfWidth, thirdOfHeight * 2)];
    [arrowPath setLineWidth:3.0f];
    [arrowPath stroke];

}

我明白了。bezierPathWithRect实际上使用该rect作为路径创建了一个bezierPath。矩形不是帧,b/c bezerpath没有帧。b/c它不是一个UIView。

把上面的代码改成

UIBezierPath *arrowPath = [UIBezierPath bezierPath];

修复它。

最新更新