如何在UIView中绘制具有特定角度的开放式三角形?
问题描述:
我很难过,需要一些帮助。出于测试目的,我有一个UIView,我通过drawRect(下面的代码)绘制。我能够绘制一个具有特定角度的圆,但它被填充和关闭。我需要它开放和抚摸。我目前使用UIBezierPath来做到这一点,但有没有更好的方法来做到这一点?我可以在UIBezierPath中绘制像我想要的开放式三角形,但是我无法指定我需要的角度(代码也在下面)。任何援助与此将不胜感激。谢谢。如何在UIView中绘制具有特定角度的开放式三角形?
下面的图片是当我用特定的角度绘制时,我希望它看起来如何。
#import "AngleView.h"
#define DEGREES_TO_RADIANS(degrees) ((M_PI * degrees)/ 180)
@implementation AngleView
-(void)drawRect:(CGRect)rect {
UIBezierPath *aPath = [UIBezierPath bezierPathWithArcCenter:CGPointZero
radius:50
startAngle:0
endAngle:DEGREES_TO_RADIANS(45)
clockwise:NO];
[aPath fill];
}
@end
,这里是我怎么能画它没有一个特定的角度。
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(86.5, 33.5)];
[bezierPath addLineToPoint: CGPointMake(60.5, 62.5)];
[bezierPath addLineToPoint: CGPointMake(87.5, 62.5)];
[[UIColor blackColor] setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];
答
而不是试图绘制一条线,你可以绘制两个单独的起源于同一点,你可以给其中的一个角度。
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, 119.5); //here you can set originating point of line
CGContextRotateCTM(context, -45 * M_PI/180); //Change Angle here -45
UIBezierPath* bezierPath = UIBezierPath.bezierPath;
[bezierPath moveToPoint: CGPointMake(0, 0)];
[bezierPath addCurveToPoint: CGPointMake(39, 0) controlPoint1: CGPointMake(39, 0) controlPoint2: CGPointMake(39, 0)];
[UIColor.blackColor setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];
CGContextRestoreGState(context);
//Second UIBezierPath with 0 angle
context = UIGraphicsGetCurrentContext();
UIBezierPath* bezier2Path = UIBezierPath.bezierPath;
[bezier2Path moveToPoint: CGPointMake(0, 119.5)];
[bezier2Path addCurveToPoint: CGPointMake(38.5, 119.5) controlPoint1: CGPointMake(38.5, 119.5) controlPoint2: CGPointMake(38.5, 119.5)];
[UIColor.blackColor setStroke];
bezier2Path.lineWidth = 1;
[bezier2Path stroke];
CGContextRestoreGState(context);
}
你有没有考虑过只画两条线? –
是的,这就是我从一开始就想到的,但我认为这是一种错误的方式去解决它,并放弃了这个想法,但我想这就是我应该这样做的吧?但是,这不会使角度的角落看起来没有连接而且不锋利吗?谢谢。 – 0SX