zoukankan      html  css  js  c++  java
  • 时钟

    @interface ViewController ()
    @property (nonatomic, weak) IBOutlet UIImageView *hourHand;
    @property (nonatomic, weak) IBOutlet UIImageView *minuteHand;
    @property (nonatomic, weak) IBOutlet UIImageView *secondHand;
    @property (nonatomic, weak) NSTimer *timer;
    @end
    @implementation ViewController
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        //adjust anchor points
        self.secondHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
        self.minuteHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
        self.hourHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
        //start timer
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick) userInfo:nil repeats:YES];
        //set initial hand positions
        [self updateHandsAnimated:NO];
    }
    - (void)tick
    {
        [self updateHandsAnimated:YES];
    }
    - (void)updateHandsAnimated:(BOOL)animated
    {
        //convert time to hours, minutes and seconds
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSUInteger units = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
        NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
        CGFloat hourAngle = (components.hour / 12.0) * M_PI * 2.0;
        //calculate hour hand angle //calculate minute hand angle
        CGFloat minuteAngle = (components.minute / 60.0) * M_PI * 2.0;
        //calculate second hand angle
        CGFloat secondAngle = (components.second / 60.0) * M_PI * 2.0;
        //rotate hands
        [self setAngle:hourAngle forHand:self.hourHand animated:animated];
        [self setAngle:minuteAngle forHand:self.minuteHand animated:animated];
        [self setAngle:secondAngle forHand:self.secondHand animated:animated];
    }
    - (void)setAngle:(CGFloat)angle forHand:(UIView *)handView animated:(BOOL)animated
    {
        //generate transform
        CATransform3D transform = CATransform3DMakeRotation(angle, 0, 0, 1);
        if (animated) {
            //create transform animation
            CABasicAnimation *animation = [CABasicAnimation animation];
            [self updateHandsAnimated:NO];
            animation.keyPath = @"transform";
            animation.toValue = [NSValue valueWithCATransform3D:transform];
            animation.duration = 0.5;
            animation.delegate = self;
            [animation setValue:handView forKey:@"handView"];
            [handView.layer addAnimation:animation forKey:nil];
        } else {
            //set transform directly
            handView.layer.transform = transform;
        }
    }
    - (void)animationDidStop:(CABasicAnimation *)anim finished:(BOOL)flag
    {
        //set final position for hand view
        UIView *handView = [anim valueForKey:@"handView"];
        handView.layer.transform = [anim.toValue CATransform3DValue];
    }
  • 相关阅读:
    [转载]DBA的特质第一部分:技术
    DPA/Ignite由于DNS问题导致连接不上被监控的数据库服务器
    ORACLE计算表引占用空间大小
    Reporting Services 错误案例一则
    SQL SERVER Transactional Replication中添加新表如何不初始化整个快照
    TNS-12540: TNS:internal limit restriction exceeded
    Error: 9001, Severity: 21, State: 5 The log for database 'xxxx' is not available
    ORA-01012: not logged on
    -bash: .bash_profile: command not found
    -bash: ulimit: pipe size: cannot modify limit: Invalid argument
  • 原文地址:https://www.cnblogs.com/jingdizhiwa/p/5591621.html
Copyright © 2011-2022 走看看