物体角度基于当前时间 [英] Object angle based on current time

查看:160
本文介绍了物体角度基于当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请仔细聆听,因为这是具体的。我正在尝试在iPhone SDK中制作模拟时钟。我已经尝试了三个星期了,我已经问了五个关于SO的问题,我仍然发现几乎没有。我需要帮助。我希望有一个根据当前时间旋转的钟针。我现在主要专注于秒针,因为它看起来最简单。

Listen carefully, because this is specific. I'm trying to make an analog clock in iPhone SDK. I've been trying to accomplish this for three weeks now, I've asked five questions on SO, and I still have found almost NOTHING. I need help. I want to have a clock hand that simply rotates depending on the current time. I'm mostly focused on the second hand right now because it seems like the easiest.

我不一定关心用于旋转时钟指针(CALayers,UIView等)的东西,只要它做它需要做的事情。非常感谢任何形式的帮助。谢谢。

I don't necessarily care what is used to rotate the clock hand (CALayers, UIView, etc.), just as long as it does what it needs to do. Any sort of help is greatly, greatly appreciated. Thanks.

编辑:我还需要双手才能成像。希望这不是太多。 :(

I also need the hands to be images. Hopefully that isn't too much. :(

推荐答案

你问了一种如果你想使用图像然后获取你的图像并在里面设置它们 initWithFrame:

接口: ClockView.h

Interface: ClockView.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ClockView : UIView {
    CALayer *containerLayer;
    CALayer *hourHand;
    CALayer *minHand;
    CALayer *secHand;
    NSTimer *timer;
}
- (void) start;
- (void) stop;
@end

实施:ClockView.m

Implementation: ClockView.m

#import "ClockView.h"

@implementation ClockView

float Degrees2Radians(float degrees) { return degrees * M_PI / 180; }

- (void) start{
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self 
                                                    selector:@selector(updateClock) 
                                                    userInfo:nil repeats:YES];
}
- (void) stop{
    [timer invalidate];
    timer = nil;
}

- (void) updateClock{
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) 
                                                                       fromDate:[NSDate date]];
    NSInteger seconds = [dateComponents second];
    NSInteger minutes = [dateComponents minute];
    NSInteger hours = [dateComponents hour];
    //correction of inverted clock
    seconds += 30; seconds %=60;
    minutes += 30; minutes %=60;
    hours += 6; hours %=12;

    hourHand.transform = CATransform3DMakeRotation (Degrees2Radians(hours*360/12), 0, 0, 1);
    minHand.transform = CATransform3DMakeRotation (Degrees2Radians(minutes*360/60), 0, 0, 1);
    secHand.transform = CATransform3DMakeRotation (Degrees2Radians(seconds*360/60), 0, 0, 1);
}
- (void) layoutSubviews{
    [super layoutSubviews];

    containerLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);

    float length = MIN(self.frame.size.width, self.frame.size.height)/2;
    CGPoint c = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    hourHand.position = minHand.position = secHand.position = c;

    hourHand.bounds = CGRectMake(0,0,10,length*0.5);
    minHand.bounds = CGRectMake(0,0,8,length*0.8);
    secHand.bounds = CGRectMake(0,0,4,length);

    hourHand.anchorPoint = CGPointMake(0.5,0.0);
    minHand.anchorPoint = CGPointMake(0.5,0.0);
    secHand.anchorPoint = CGPointMake(0.5,0.0);

}

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

        containerLayer = [[CALayer layer] retain];

        hourHand = [[CALayer layer] retain];
        minHand = [[CALayer layer] retain];
        secHand = [[CALayer layer] retain];

        //paint your hands: simple colors
        hourHand.backgroundColor = [UIColor blackColor].CGColor;
        hourHand.cornerRadius = 3;
        minHand.backgroundColor = [UIColor grayColor].CGColor;
        secHand.backgroundColor = [UIColor whiteColor].CGColor;
        secHand.borderWidth = 1.0;
        secHand.borderColor = [UIColor grayColor].CGColor;

        //put images
        //hourHand.contents = (id)[UIImage imageNamed:@"hour.png"].CGImage;
        //minHand.contents = (id)[UIImage imageNamed:@"min.png"].CGImage;
        //secHand.contents = (id)[UIImage imageNamed:@"sec.png"].CGImage;

        [containerLayer addSublayer:hourHand];
        [containerLayer addSublayer:minHand];
        [containerLayer addSublayer:secHand];
        [self.layer addSublayer:containerLayer];

        self.layer.borderColor = [UIColor greenColor].CGColor;
        self.layer.borderWidth = 1.0;

    }
    return self;
}
- (void)dealloc {
    [self stop];
    [hourHand release];
    [minHand release];
    [secHand release];
    [containerLayer release];
    [super dealloc];
}
@end

用法:

#import "ClockView.h"

- (void)viewDidLoad {
    [super viewDidLoad];

    ClockView *clockView = [[ClockView alloc] initWithFrame:CGRectMake(0,0,100,100)];
    [self.view addSubview:clockView];
    [clockView start];
    [clockView release];

}

这篇关于物体角度基于当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆