【问题标题】:Object angle based on current time基于当前时间的物体角度
【发布时间】:2011-06-18 03:09:39
【问题描述】:

仔细听,因为这是具体的。我正在尝试在 iPhone SDK 中制作模拟时钟。我已经尝试完成这个三周了,我已经问了五个关于 SO 的问题,但我仍然几乎没有发现任何问题。我需要帮助。我想要一个时钟指针,它可以根据当前时间简单地旋转。我现在主要专注于二手,因为它似乎是最简单的。

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

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

【问题讨论】:

  • 我接受了更多的答案,虽然它没有改变:(

标签: iphone uiview calayer rotation clock


【解决方案1】:

你问了same question,我说Layers 好多了...这次我在不到三周的时间内编写了洞程序;)。 如果您想使用图像,请获取图像并将其设置在 initWithFrame:

接口: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

#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];

}

【讨论】:

  • 哇。感谢这一切!我可以在应用程序中实际使用它吗?对我来说,这感觉有点像作弊...... :( 有什么办法可以帮助你吗?我怀疑,因为你真的很好......
  • 完美运行!虽然它是颠倒的......我会尝试自己解决这个问题。
  • 我刚刚更正了代码,现在它可以正确显示小时;)
【解决方案2】:

三周?认真的吗?
我的时钟很丑,但它是一个时钟,看起来很好,可以从中猜出当前时间。

static inline float PGVmyRadians(double degrees) { return degrees * M_PI / 180; }

- (void)drawRect:(CGRect)rect {
    CGFloat boundsWidth = self.bounds.size.width;
    CGFloat boundsHeight = self.bounds.size.height;
    CGPoint center = CGPointMake(boundsWidth/2, boundsHeight/2);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, self.bounds);
    CGContextSetFillColorWithColor(context, [UIColor magentaColor].CGColor);
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 10);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextAddArc(context, boundsWidth/2, boundsHeight/2, boundsWidth/2, PGVmyRadians(0), PGVmyRadians(360), 0);

    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
    NSInteger seconds = [dateComponents second];
    NSInteger minutes = [dateComponents minute];
    NSInteger hour = [dateComponents hour];

    if (hour > 12) {
        hour -= 12;
    }

    CGFloat angleSec = ((seconds - 15) / 60.0) * 360.0;
    CGFloat thetaSec = PGVmyRadians(angleSec);
    CGFloat lengthSec = 0.9 * boundsHeight/2;
    CGFloat Xsec = center.x + lengthSec * cos(thetaSec);
    CGFloat Ysec = center.y + lengthSec * sin(thetaSec);

    CGContextMoveToPoint(context, center.x, center.y);
    CGContextAddLineToPoint(context, Xsec, Ysec);

    CGFloat angleMin = ((minutes - 15) / 60.0) * 360.0;
    CGFloat thetaMin = PGVmyRadians(angleMin);
    CGFloat lengthMin = 0.7 * boundsHeight/2;
    CGFloat Xmin = center.x + lengthMin * cos(thetaMin);
    CGFloat Ymin = center.y + lengthMin * sin(thetaMin);

    CGContextMoveToPoint(context, center.x, center.y);
    CGContextAddLineToPoint(context, Xmin, Ymin);

    CGFloat angleHour = ((hour - 3) / 12.0) * 360.0;
    CGFloat thetaHour = PGVmyRadians(angleHour);
    CGFloat lengthHour = 0.5 * boundsHeight/2;
    CGFloat Xhour = center.x + lengthHour * cos(thetaHour);
    CGFloat Yhour = center.y + lengthHour * sin(thetaHour);

    CGContextMoveToPoint(context, center.x, center.y);
    CGContextAddLineToPoint(context, Xhour, Yhour);
    CGContextStrokePath(context);
}

【讨论】:

  • 感谢回复,但我忘了说我需要手是图像而不是画出来。 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
相关资源
最近更新 更多