利用纯代码写出一个秒表表盘的方法 —— #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

 

posted @ 2015-09-28 17:02  sixindev  阅读(544)  评论(0编辑  收藏  举报