开发者社区> 问答> 正文

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

AppDelegate.m

- (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

#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

screenshot

展开
收起
a123456678 2016-07-28 14:12:37 2279 0
1 条回答
写回答
取消 提交回答
  • view.center = self.window.center

    2019-07-17 20:01:23
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载