0

Hey there need your help,

I'am trying to create an analog clock for my iPhone. The Problem is, the UIImage does not move in the right way or doesn't move, most time it is spinnig all over the View. Is there any Framework missing, I converted Degree into Radians... Here is the Code:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // Getting a Timer
    NSTimer *timer = [[NSTimer alloc]init];
    timer = [NSTimer scheduledTimerWithTimeInterval: 1 target:self
                                           selector:@selector(tick:) userInfo:nil repeats:YES];
    [timer fire];
    self.ticker = 0;
    self.minutes = 0;
    self.hours = 0;
}

- (void)tick:(NSTimer *)theTimer
{
    //Turning Seconds into Minutes and Hours
    self.ticker ++;
    self.timerLabel.text = [NSString stringWithFormat:@"%d",self.ticker];
    self.seconds = self.ticker;
    if (self.seconds == 60){
        self.ticker = 0;
        self.seconds = 0;
        self.minutes ++;
    }
    if (self.minutes == 60){
        self.minutes = 0;
        self.hours ++;
    }

    //Main issue here...
    // 360° / 60 second = 6° per second
    double grad = self.ticker * 6;
    //Converting Grad into Rad
    double rad = M_PI * grad / 180;
    //Set Transformation ---> CGAffineTransformMakeRotation does not work eighter
    CGAffineTransform transform = CGAffineTransformRotate(self.pointerImage.transform, rad);
    //Set Transformation to UIImageView caleld pointerImage
    self.pointerImage.transform = transform;
    //self.pointerImage.center = CGPointMake(160, 420);
    //NSLog(@"%f", grad);



    if ( self.ticker == 60){
        self.ticker = 0;
    }

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

1

Rotations about the center of a circle require an initial transform to offset the bounds - so the transform is at the circle center. You will surely find example code on this site. I highly suggest that you create a single view project, add a colored square view to it, and experiment on getting that to rotate properly. Only then take that code and integrate into your real project.

David H
  • 40,852
  • 12
  • 92
  • 138
  • It is an experimental project, but what do you mean with "Rotations about the center of a circle require an initial transform to offset the bounds" – user2738907 Oct 29 '13 at 12:51
  • So, i tried it with an UIIMage Rectangle and it's look like the Image is walking on the ground, like a cube falling over. But i didn't changed the anchorPoint. – user2738907 Oct 29 '13 at 13:52
  • Try first with just a UIView - create it with 100x100, set its background to red, and then try to rotate it. There are oodles of examples how to do this - look in (I recall) Apple's Core Animation document, but 100% for sure you can find them. If you just don't want to be bothered put a 100pt bounty on your question, you'll get more code than you can read. – David H Oct 29 '13 at 15:14
  • Thanks for everyones help. Solved the Problem: There is an Option for Storyboard called "Use Autolayout". Here is how to disable it: http://stackoverflow.com/questions/13694972/disable-constraints-in-xcode-storyboard – user2738907 Oct 29 '13 at 19:20
0

The transforms are cumulative, so you only need to add in the incremental change on each tick:

CGAffineTransform transform = CGAffineTransformRotate(self.pointerImage.transform, (M_PI * 6 / 180));

as you had it, your clock hand jumping this far on each tick {1,3,6,10,15,21,28,36,45,55,6,18,31...} which is why it appeared to jump around so much.

The relevant line from the CGAffineTransform class Reference is this, taken from within the CGAffineTransformRotate method definition:

You use this function to create a new affine transformation matrix by adding a rotation value to an existing affine transform. The resulting structure represents a new affine transform, which you can use (and reuse, if you want) to rotate a coordinate system.

ryan cumley
  • 1,901
  • 14
  • 11
  • 1
    That sequence may seem clearer if I write it like this {1, 2+1, 3+(2+1), 4+(3+2+1), 5+(4+3+2+1)...} or the pure way to write it would be n!mod(60) which would of course look a lot nicer here if I knew how to use LaTeX in stackoverflow... – ryan cumley Oct 29 '13 at 12:34
  • Okay I understand, the Problem ist die pointerImage is doing nothing. It is standing at the same point without any rotation – user2738907 Oct 29 '13 at 12:43
  • So there is an issue with the UIImageView, I tried it with a Label and it started rotating, but it is moving the wrong way. It does not move around the center, it is spinning upwards, downwards in any kind of direction... – user2738907 Oct 29 '13 at 12:48
  • The CALayer has an "anchorPoint" property that usually defaults to the top left corner (0.0, 0.0) If you can move it to the center of your rotating object, things should work much better. As far as the rotation direction, just invert the sign (*-1). The signs are flipped for OSX and iOS – ryan cumley Oct 29 '13 at 12:59
  • You are right, i tried it with an other project and an UISlider and the rotation have been similar to the first one. I don't know where to change that. – user2738907 Oct 29 '13 at 13:18
  • Not everything has a CALayer that you can play with, but if the object you are trying to rotate does (for example simple UIView's do) then you can access the anchor point by someView.layer.anchorPoint – ryan cumley Oct 29 '13 at 13:22
  • But the default Point of image.layer.anchorPoint is (0.5, 0.5) so i tried this self.image.layer.anchorPoint = CGPointMake(0.5, 0.5); and the rotation was still the same. I also tried (1,1) and (0,0) non perfect rotation around the center. Maybe they changed it with iOS 7 – user2738907 Oct 29 '13 at 13:28
  • Can you post the code for how you setup the object you're trying to rotate? – ryan cumley Oct 29 '13 at 14:22
  • No, i can't i need 10 (something) to post pictures...can I send them to you? – user2738907 Oct 29 '13 at 14:40
  • It is an UIIMage View: No Image selected, Nothing Hilighted, View Mode is Center, Tag = 0, Interaction both off, Alpha = 1, Background = Orange, Tint = Blue, Drawing Selcected( Opaque, Clears Graphic Context, Autoresize Subviews) other ones are off, Streching x = 0, y = 0, Width = 1, Height = 1 – user2738907 Oct 29 '13 at 14:43
  • I tried moving this discussion to chat, but same problem with your reputation... contact info is at ryancumley dot com. – ryan cumley Oct 29 '13 at 14:45
  • Sended Pictures via Skype – user2738907 Oct 29 '13 at 14:56