带 UIImages 指针的模拟时钟?

发布于 2024-10-13 15:42:23 字数 332 浏览 4 评论 0原文

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

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

I want to know how to make an analog clock in iPhone SDK. However, I want the hands of the clock to be custom images and not squares drawn over like in this tutorial: http://iphone-dev-tips.alterplay.com/2010/03/analog-clock-using-quartz-core.html

The problem with that tutorial is that the clock hands are drawn with Quarzt Core. I'm okay with that as long as the hands could be custom. What would be the easiest way to make an Analog Clock this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最好是你 2024-10-20 15:42:23

使用 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

更新:

我写了一个使用 CALayers 的 ClockView 示例,也许你会发现它很有用。

Do it with CALayers. is WAY much easier and performance is better this way.

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

UPDATE:

I wrote a ClockView sample using CALayers, maybe you find it useful.

燕归巢 2024-10-20 15:42:23

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

 函数开始(){
                画布 = 拉斐尔("时钟",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));                        

                    }别的{
                        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({笔画:"#888","笔画宽度":1})
                }    
                h_hand = canvas.path("M100 100L100 70");
                h_hand.attr({笔画: "#eee", "笔画宽度": 3});
                min_hand = canvas.path("M100 100L100 65");
                min_hand.attr({笔画: "#eee", "笔画宽度": 2});
                sec_hand = canvas.path("M100 110L100 55");
                sec_hand.attr({笔画: "#f00", "笔画宽度": 1}); 
                var pin = canvas.circle(100, 100, 2);
                pin.attr("填充", "#fff");    
                设置间隔(函数(){
                   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*分钟, 100, 100);
                   sec_hand.rotate(6*秒, 100, 100);
                },1000);
            } 

希望有用

this one uses 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>

hope useful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文