首页 > 写了一个绘制同心圆,设置结果view 偏了,没找到原因,大家给我瞅瞅啊

写了一个绘制同心圆,设置结果view 偏了,没找到原因,大家给我瞅瞅啊

AppDelegate.m

ios- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 初始化 window
    self.window = [[UIWindow alloc] initWithFrame : [[UIScreen mainScreen] bounds]];
    // 铺满全屏
    CGRect frame = self.window.bounds;
    // 初始化 view
    BNRHypnosisView *view = [[BNRHypnosisView alloc] initWithFrame:frame];
    // 给当前 window 添加 子view
    [self.window addSubview:view];
    // 设置屏幕背景
    // 设置该 window 为主window, 设置为可见
    [self.window makeKeyAndVisible];
    return YES;
}

BNRHypnosisView.m

ios#import "BNRHypnosisView.h"

@implementation BNRHypnosisView

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    CGRect bounds = self.bounds;

    // 计算圆心
    CGPoint center;
    center.x = bounds.origin.x + bounds.size.width / 2.0;
    center.y = bounds.origin.y + bounds.size.height / 2.0 ;

    // 使最外层圆形称为视图的外接圆
    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;

    // 初始化路径绘制工具
    UIBezierPath *path = [[UIBezierPath alloc] init];
    path.lineWidth = 10;
    [[UIColor lightGrayColor] setStroke];

    // 根据 圆心 半径 起至弧度 绘制路径
    for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) {
        [path moveToPoint:CGPointMake(center.x + currentRadius, center.y)];
        [path addArcWithCenter:center radius:currentRadius startAngle:0.0 endAngle: M_PI * 4.0 clockwise: YES];
    }

    // 绘制
    [path stroke];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }

    return self;
}
@end

结果运行之后这个样子


view.center = self.window.center

【热门文章】
【热门文章】