2017-09-20 1 views
1

원의 경계에 수직 파선이있는 원을 그리려합니다.원 안에 수직 파선이있는 원

나는 이런 식으로 시도했지만 원 안에 점을 찍었습니다.

CAShapeLayer *circle = [CAShapeLayer layer]; 

circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) cornerRadius:radius].CGPath; 

circle.fillColor = [UIColor clearColor].CGColor; 
circle.strokeColor = [UIColor redColor].CGColor; 
circle.lineWidth = 1; 

circle.lineDashPattern = @[@2, @3]; 
[[self.view layer] addSublayer:circle]; 
+0

작동 코드를 게시하십시오. 나는 당신의 코드로 시도했지만 작동하지 않는다. – ChenSmile

답변

2

아래 코드를 사용해보세요. 저에게 효과적입니다. 1) UIView의 서브 클래스를 타고, 그리고 .H 파일에 지금 your.m 파일의 코드

#import <UIKit/UIKit.h> 
@interface ViewClass : UIView 

@property (nonatomic) NSUInteger numberOfGraduations; 
@property (nonatomic) CGFloat arcDegreeStart; 
@property (nonatomic) CGFloat arcDegreeEnd; 

@property (nonatomic) CGPoint arcCenter; 
@property (nonatomic) CGFloat arcRadius; 

@property (nonatomic) CGFloat deltaArc; 

-(void)drawGraduation:(CGPoint)center Radius:(CGFloat)radius Angle:(CGFloat)angle Length:(CGFloat)length Width:(CGFloat)width colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue; 

@end 

2) 아래의 구현, 아래의 drawRect 방법입니다)

#import "ViewClass.h" 

@implementation ViewClass 
{ 
    CGContextRef c; 
} 
-(void)drawGraduation:(CGPoint)center Radius:(CGFloat)radius Angle:(CGFloat)angle Length:(CGFloat)length Width:(CGFloat)width colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue{ 
    c = UIGraphicsGetCurrentContext(); 

    CGFloat radius2 = radius+length; // The radius of the end points of the graduations 
    CGPoint p1 = (CGPoint){cos(angle)*radius+center.x, sin(angle)*radius+center.y}; // the start point of the graduation 
    CGPoint p2 = (CGPoint){cos(angle)*radius2+center.x, sin(angle)*radius2+center.y}; // the end point of the graduation 

    CGContextMoveToPoint(c, p1.x, p1.y); 
    CGContextAddLineToPoint(c, p2.x, p2.y); 
    CGContextSetLineCap(c, kCGLineCapRound); 
    CGContextSetRGBStrokeColor(c, red, green, blue, 1.0); 
    CGContextSetLineWidth(c, width); 
    CGContextSetBlendMode(c,kCGBlendModeNormal); 
    CGContextStrokePath(c); 

} 

3 코드

아래 구현 ..

- (void)drawRect:(CGRect)rect { 
    [super drawRect:rect]; 
    CGRect r = self.bounds; 

    _numberOfGraduations = 31; 
    _arcCenter = (CGPoint){r.size.width*0.5, r.size.height*0.5}; // center of arc 
    _arcRadius = (r.size.width*0.5)-20; // radius of arc 

    CGFloat maxGraduationWidth = 1.0; 

    CGFloat maxGraduationWidthAngle = maxGraduationWidth/_arcRadius; // the maximum graduation width angle (used to prevent the graduations from being stroked outside of the main arc) 

    _arcDegreeStart =-M_PI*0.2; 

    _arcDegreeEnd = -M_PI*0.8; 

    // draw graduations 
    _deltaArc = (_arcDegreeEnd-_arcDegreeStart+maxGraduationWidthAngle)/(_numberOfGraduations-1); // the change in angle of the arc 
    for (int i = 0; i < _numberOfGraduations; i++) { 

    [self drawGraduation:_arcCenter Radius:_arcRadius Angle:_arcDegreeStart+(i*_deltaArc) Length:14 Width:1 colorWithRed:0.0 green:0.0 blue:1.0]; 
     } 

} 
+0

C는 무엇인가? 그것은 원입니까? 내가 동그라미를 사용하면 오류 – ChenSmile

+0

@ChenSmile 미안 해요 내가 그것을 놓친 ... pls 편집 된 코드를 찾을 수 –

+0

내가 무엇을 입력하고 그것은 그림이 아니에요 – ChenSmile