开发者

How to make my imageview rotate to center of view in iPhone?

I have clock application in which I have added custom images to the clock hands (minute and hour). When I run the application, my minute hand rotate properly in the clockwise but the problem is the imageview does not rotate around the center of the view; it rotates around itself.

#import "AnalogClocktestViewController.h"

@implementation AnalogClocktestViewController

#import <QuartzCore/QuartzCore.h>
#import <QuartzCore/CALayer.h>
#import <QuartzCore/CoreAnimation.h>

@synthesize digitalswitch;
@synthesize hourimage;
@synthesize minuteimage;
@synthesize backimage;
@synthesize clockLabel;


- (void) showActivity {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |
    NSDayCalendarUnit | NSHourCalendarUnit| NSMinuteCalendarUnit
    |NSSecondCalendarUnit;
    NSDate *date = [NSDate date];
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:date];
    int h = [comps hour];
    int m = [comps minute];

    time_hour = h * 30*-1;
    time_minut = m * 6*-1;
        CGAffineTransform cgaRotateHr  =
    CGAffineTransformMakeRotation(time_hour);
    CGAffineTransform cgaRotateMin =
    CGAffineTransformMakeRotation(time_minut);

    //Looks awful but it works (PNGs with transparency have blocked edges)
开发者_Python百科    [hourimage setTransform:cgaRotateHr];
//hourimage is the imageview for hourhand of clock
    [minuteimage setTransform:cgaRotateMin];
//minuteimage is the imageview for minute hand of clock.


    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setTimeStyle:NSDateFormatterMediumStyle];
    [clockLabel setText:[formatter stringFromDate:date]];
}




-(void)startClockUpdates
{
    [self showActivity];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    [self startClockUpdates];

}







- (void)dealloc {
    [super dealloc];
}

@end


An addition to Alex's answer -

If your image looks like this:

_________
|   ^   |
|   |   |
|   |   |
|___0___|

Make it look like this:

_________
|   ^   |
|   |   |
|   |   |
|   0   |
|       |
|       |
|_______|

So that the center of the clock hand is in the center of the image. Then, it should rotate around the center.


You could make an image that is twice the size, and has alpha on the other side, therefore would be appear to rotate around the center.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜