【问题标题】:Analog clock with UIImages for hands?带有 UIImages 的模拟时钟?
【发布时间】:2011-01-22 12:26:42
【问题描述】:

我想知道如何在 iPhone SDK 中制作模拟时钟。但是,我希望时钟的指针是自定义图像,而不是像本教程中那样绘制的正方形:http://iphone-dev-tips.alterplay.com/2010/03/analog-clock-using-quartz-core.html

该教程的问题在于时钟指针是用 Quarzt Core 绘制的。只要手可以定制,我就可以了。以这种方式制作模拟时钟的最简单方法是什么?

【问题讨论】:

标签: iphone ios nstimer


【解决方案1】:

使用 CALayers 来实现。这样更容易,性能也更好。

CALayer *handLayer = [CALayer layer];
handLayer.contents = (id)[UIImage imageNamed:@"hand.png"].CGImage;
handLayer.anchorPoint = CGPointMake(0.5,0.0)];
[myview.layer addSublayer:handLayer];

//i.e.: if handLayer represents the seconds hand then repeat this every second ;)
handLayer.transform = CGAffineTransformMakeRotation (angle); //set the angle here


更新:

我写了一个ClockView sample using CALayers,也许你觉得有用。

【讨论】:

  • 谢谢你的回答,我去看看CALayers/Core Animation Programming Guide。
  • 它说 .contents 和 .anchorPoint 不是结构或洋葱。我做错了什么?
  • 将 QuartzCore.Framework 添加到您的项目和#import 到您的文件;)
【解决方案2】:

这个使用 raphael js http://jsfiddle.net/8srjq/

            function start(){
                canvas = Raphael("clock",200, 200);
                var h_sign;
                for(i=0;i<24;i++){
                    var p = Math.round(40*Math.cos(15*i*Math.PI/180));
                    if(p==40 || p==-40 || p==0){
                        var start_x = 100+Math.round(35*Math.cos(15*i*Math.PI/180));
                        var start_y = 100+Math.round(35*Math.sin(15*i*Math.PI/180));                        

                    }else{
                        var start_x = 100+Math.round(39*Math.cos(15*i*Math.PI/180));
                        var start_y = 100+Math.round(39*Math.sin(15*i*Math.PI/180));  
                    }

                    var end_x = 100+Math.round(47*Math.cos(15*i*Math.PI/180));
                    var end_y = 100+Math.round(47*Math.sin(15*i*Math.PI/180));    
                    h_sign = canvas.path("M"+start_x+" "+start_y+"L"+end_x+" "+end_y);
                    h_sign.attr({stroke:"#888","stroke-width":1})
                }    
                h_hand = canvas.path("M100 100L100 70");
                h_hand.attr({stroke: "#eee", "stroke-width": 3});
                min_hand = canvas.path("M100 100L100 65");
                min_hand.attr({stroke: "#eee", "stroke-width": 2});
                sec_hand = canvas.path("M100 110L100 55");
                sec_hand.attr({stroke: "#f00", "stroke-width": 1}); 
                var pin = canvas.circle(100, 100, 2);
                pin.attr("fill", "#fff");    
                setInterval(function(){
                   var now = new Date();
                   var h = now.getHours();
                   var min = now.getMinutes();
                   var sec = now.getSeconds();
                   h_hand.rotate(30*h+(min/2.5), 100, 100);
                   min_hand.rotate(6*min, 100, 100);
                   sec_hand.rotate(6*sec, 100, 100);
                },1000);
            }​ </script>

希望有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-19
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    相关资源
    最近更新 更多