利用纯代码写出一个秒表表盘的方法 —— #DF
@interface ViewController () @property (nonatomic, strong) CALayer *secLayer; // 秒针layer @property (nonatomic, strong) NSTimer *timer; // 定时器 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 创建一个表盘 UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; showView.center = self.view.center; showView.layer.borderWidth = 8.f; showView.layer.cornerRadius = 150; showView.layer.borderColor = [UIColor purpleColor].CGColor; [self.view addSubview:showView]; // 创建出秒针layer self.secLayer = [CALayer layer]; self.secLayer.anchorPoint = CGPointMake(0, 0); self.secLayer.frame = CGRectMake(150, 150, 3, 130); self.secLayer.backgroundColor = [UIColor orangeColor].CGColor; [showView.layer addSublayer:self.secLayer]; // 创建定时器 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(timerEvent) userInfo:nil repeats:YES]; } - (void)timerEvent { static int i = 1; self.secLayer.transform = CATransform3DMakeRotation((((360 / 60.f) * i++) / 180.0 * M_PI), 0, 0, 1); } @end
作者:SIBU iOS DEV
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.