Аналоговые часы с UIImages для рук?

Я хочу знать, как сделать аналоговые часы в iPhone SDK. Однако я хочу, чтобы стрелки часов были пользовательскими изображениями, а не нарисованными квадратами, как в этом руководстве: http://iphone-dev-tips.alterplay.com/2010/03/analog-clock-using-quartz-core.html

Проблема этого туториала в том, что стрелки часов нарисованы с помощью Quarzt Core. Я согласен с этим, пока руки могут быть нестандартными. Какой самый простой способ сделать аналоговые часы таким образом?


person Andy B    schedule 22.01.2011    source источник
comment
Ответ здесь также довольно точен. stackoverflow.com/ вопросы/6499996/ (не нужны CALayers)   -  person Jonny    schedule 22.08.2011


Ответы (2)


Сделайте это с 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 с использованием CALayers, возможно, вы найдете его полезным.

person nacho4d    schedule 22.01.2011
comment
Спасибо за ответ, я посмотрю CALayers/Core Animation Programming Guide. - person Andy B; 22.01.2011
comment
В нем говорится, что .contents и .anchorPoint не находятся в структуре или луковице. Что я делаю неправильно? - person Andy B; 22.01.2011
comment
Добавьте QuartzCore.Framework в свой проект и #import ‹QuartzCore/QuartzCore.h› в свой файл ;) - person nacho4d; 22.01.2011

этот использует 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>

надеюсь полезно

person Marco Allori    schedule 27.11.2012