我有时钟应用程序,我已将自定义图像添加到时钟指针(分钟和小时)。当我运行应用程序时,我的分针顺时针旋转正常,但问题是imageview不会围绕视图中心旋转;它绕着自己旋转。
#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)
[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
答案 0 :(得分:1)
Alex的答案的补充 -
如果您的图片如下所示:
_________
| ^ |
| | |
| | |
|___0___|
让它看起来像这样:
_________
| ^ |
| | |
| | |
| 0 |
| |
| |
|_______|
这样时钟指针的中心位于图像的中心。然后,它应该围绕中心旋转。
答案 1 :(得分:0)
您可以制作两倍大小的图像,并在另一侧使用alpha,因此可能会围绕中心旋转。