时钟实例(CALayer和NSCalendar运用)

#import "ViewController.h"

#define perSecAngle 6
#define perHourAngle 30
#define perMiniteOffsetAngle 0.1
#define perHourOffsetAngle 0.5
#define angleToRadius(angle) ((angle) / 180.0 * M_PI)

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UIImageView *clockImage;
@property (strong, nonatomic) IBOutlet UILabel *timeLabel;

@property (strong, nonatomic) CALayer *secHand;
@property (strong, nonatomic) CALayer *minHand;
@property (strong, nonatomic) CALayer *hourHand;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setHands];

    [self timeChange];
}

- (void)setHands
{
    CGPoint clockCenter = CGPointMake(self.clockImage.bounds.size.width * 0.5, self.clockImage.bounds.size.height * 0.5);

    self.hourHand = [CALayer layer];
    self.hourHand.bounds = CGRectMake(0, 0, 3, 40);
    self.hourHand.backgroundColor = [UIColor blackColor].CGColor;
    [self.clockImage.layer addSublayer:self.hourHand];
    self.hourHand.position = clockCenter;
    self.hourHand.anchorPoint = CGPointMake(0.5, 0.9);

    self.minHand = [CALayer layer];
    self.minHand.bounds = CGRectMake(0, 0, 3, 60);
    self.minHand.backgroundColor = [UIColor blackColor].CGColor;
    [self.clockImage.layer addSublayer:self.minHand];
    self.minHand.position = clockCenter;
    self.minHand.anchorPoint = CGPointMake(0.5, 0.9);

    self.secHand = [CALayer layer];
    self.secHand.bounds = CGRectMake(0, 0, 1, 80);
    self.secHand.backgroundColor = [UIColor redColor].CGColor;
    [self.clockImage.layer addSublayer:self.secHand];
    self.secHand.position = clockCenter;
    self.secHand.anchorPoint = CGPointMake(0.5, 0.8);

    [NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [self timeChange];
    }];
}

- (void)timeChange
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *comp = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];

    NSInteger currentSecond = comp.second;
    CGFloat currentSecondAngle = perSecAngle * currentSecond;
    self.secHand.transform = CATransform3DMakeRotation(angleToRadius(currentSecondAngle), 0, 0, 1);

    NSInteger currentMinute = comp.minute;
    CGFloat currentMinuteAngle = perSecAngle * currentMinute + (currentSecond * perMiniteOffsetAngle) / 2;
    self.minHand.transform = CATransform3DMakeRotation(angleToRadius(currentMinuteAngle), 0, 0, 1);

    NSInteger currentHour = comp.hour;
    CGFloat currentHourAngle = perHourAngle * currentHour + (currentMinute * perHourOffsetAngle) / 2;
    self.hourHand.transform = CATransform3DMakeRotation(angleToRadius(currentHourAngle), 0, 0, 1);

    self.timeLabel.text = [NSString stringWithFormat:@"%ld:%ld:%ld",comp.hour,comp.minute,comp.second];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

results matching ""

    No results matching ""