如何在iPhone中使我的imageview旋转到视图中心?

如何在iPhone中使我的imageview旋转到视图中心?,iphone,objective-c,Iphone,Objective C,我有一个时钟应用程序,我在其中添加了自定义图像到时钟指针(分钟和小时)。当我运行应用程序时,我的分针以顺时针方向正确旋转,但问题是imageview没有围绕视图中心旋转;它绕着自己旋转 #import "AnalogClocktestViewController.h" @implementation AnalogClocktestViewController #import <QuartzCore/QuartzCore.h> #import <QuartzCore/CALa

我有一个时钟应用程序,我在其中添加了自定义图像到时钟指针(分钟和小时)。当我运行应用程序时,我的分针以顺时针方向正确旋转,但问题是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
#导入“AnalogClocktestViewController.h”
@ClockTestViewController的实现
#进口
#进口
#进口
@综合数字开关;
@综合年龄;
@合成微小图像;
@合成背景图像;
@合成钟标;
-(无效)表演活动{
NSCalendar*格里高利=[[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
无符号unitFlags=NSYearCalendarUnit | NSMonthCalendarUnit|
NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit
|国家统计局;
NSDate*日期=[NSDate日期];
NSDateComponents*comps=[gregorian components:unitFlags fromDate:date];
int h=[comps hour];
int m=[comps minute];
时间=小时*30*-1;
时间分钟=m*6*-1;
cgaRotateHr=
CGAffineTransformMakeRotation(时间\小时);
cgaRotateMin=
CGAffineTransformMakeRotation(时间分钟);
//看起来很糟糕,但它可以工作(带有透明的PNG有阻塞的边缘)
[hourimage setTransform:cgaRotateHr];
//hourimage是小时和时钟的图像视图
[minuteimage setTransform:cGaroteMin];
//minuteimage是时钟分针的图像视图。
NSDateFormatter*格式化程序=[[[NSDateFormatter alloc]init]autorelease];
[格式化程序setTimeStyle:nsDateFormatTermeLiumStyle];
[clockLabel setText:[formatter stringFromDate:date]];
}
-(无效)StartClock更新
{
[自我展示活动];
计时器=[NSTimer scheduledTimerWithTimeInterval:0.5目标:自选择器:@selector(showActivity)userInfo:nil repeats:YES];
}
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad{
[超级视图下载];
[自启动锁定更新];
}
-(无效)解除锁定{
[super dealoc];
}
@结束

你可以制作一个两倍大小的图像,另一侧有alpha,因此看起来会围绕中心旋转。

这是Alex答案的补充-

如果您的图像如下所示:

_________
|   ^   |
|   |   |
|   |   |
|___0___|
_________
|   ^   |
|   |   |
|   |   |
|   0   |
|       |
|       |
|_______|
让它看起来像这样:

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

因此,时钟指针的中心位于图像的中心。然后,它应该围绕中心旋转。

您需要将平移和旋转结合起来才能实现。Alex我希望我的imageview在中心旋转,并且它的x轴应该固定在UIView的中心。这是什么意思如果你想创建一个标准的模拟时钟,你所要做的就是在一个图像编辑器中,将图像的画布大小增加到2倍,移动指针使其从图像的中心移动到输出端,然后在IB位置将其移动到视图的中心,然后在代码中旋转它?谢谢这个问题解决了我还有一个问题,当我的时钟指针开始旋转时,每次需要2秒一圈,当它达到60秒时,即从59秒开始旋转时,需要20秒一圈。可能是什么问题。请帮我解决这个问题。首先,谢谢,你的措辞令人困惑。其次,由于这是另一个问题,请在关于堆栈溢出的单独问题中提问。谢谢