Cocoa touch 将两个UIImageView旋转360度
我一直在尝试两个固定大小的UIImageView,上面有图像(覆盖所有视图),但我一直在努力(我放弃!)以循环方式旋转每个视图。因此,如果我有一个UIView,另一个就在它旁边,我希望能够旋转第一个UIView(另一个是中间的,没有间隙),然后以360度的方式旋转,这是不可能的Cocoa touch 将两个UIImageView旋转360度,cocoa-touch,Cocoa Touch,我一直在尝试两个固定大小的UIImageView,上面有图像(覆盖所有视图),但我一直在努力(我放弃!)以循环方式旋转每个视图。因此,如果我有一个UIView,另一个就在它旁边,我希望能够旋转第一个UIView(另一个是中间的,没有间隙),然后以360度的方式旋转,这是不可能的 有人能帮我吗?我写的第一段iPhone代码中有一段是显示一个用树叶做成的时钟 一个时钟是由三片叶子,小时,分钟和秒组成的,我们有一个单一的叶子图像,它是用不同的比例,不透明度等绘制的,以给出时钟的外观 下面的UIView
有人能帮我吗?我写的第一段iPhone代码中有一段是显示一个用树叶做成的时钟 一个时钟是由三片叶子,小时,分钟和秒组成的,我们有一个单一的叶子图像,它是用不同的比例,不透明度等绘制的,以给出时钟的外观 下面的UIView在视图的中心绘制了一个时钟,通过平移和缩放将树叶放置在正确的位置。保存并还原CTM以保存重复翻译 您可能想看看它是否有助于您处理坐标系和旋转
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame])
{
// Initialization code
self.minutes = 49;
self.seconds = 0;
// clear to transparent
self.clearsContextBeforeDrawing = YES;
self.backgroundColor = [UIColor clearColor];
[self tick:nil];
}
return self;
}
- (void)tick:(NSTimer *)timer
{
// get time
NSDate * time = [NSDate date];
NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents * comp = [gregorian components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:time];
// update the time
self.seconds = [comp second];
self.minutes = [comp minute];
self.hours = [comp hour];
// redisplay
[self setNeedsDisplay];
}
- (float)toRadians:(float)deg
{
return deg * 3.14/180.0;
}
- (void)drawClock:(CGPoint)pos hours:(NSInteger)theHours minutes:(NSInteger)theMinutes seconds:(NSInteger)theSeconds
{
UIImage * leaf = [UIImage imageNamed:@"leaf.png"];
// context
CGContextRef myContext = UIGraphicsGetCurrentContext();
// save original state
CGContextSaveGState(myContext);
// set alpha and move it to centre of clock
CGContextSetAlpha(myContext, 0.8);
CGContextTranslateCTM (myContext, pos.x, pos.y);
// save centred state
CGContextSaveGState(myContext);
// rotate and translate the hour 'hand'
CGContextRotateCTM (myContext, [self toRadians:(theHours-3.0+theMinutes/60.0)*360/12.0 - 10] );
CGContextTranslateCTM (myContext, -5, -[leaf size].height/12);
// draw the hour hand and restore to translated
CGContextDrawImage(myContext, CGRectMake(0, 0, [leaf size].width/6, [leaf size].height/6), [leaf CGImage]);
// restore centred state and resave
CGContextRestoreGState(myContext);
CGContextSaveGState(myContext);
// rotate and transform the minute 'hand'
CGContextRotateCTM (myContext, [self toRadians:((theMinutes-15)*360.0 /60.0) - 10]);
CGContextTranslateCTM (myContext, -5, -[leaf size].height/10);
// draw the minute hand and restore original context
CGContextDrawImage(myContext, CGRectMake(0, 0, [leaf size].width/5, [leaf size].height/5), [leaf CGImage]);
// restore centred state
CGContextRestoreGState(myContext);
// rotate and transform the second 'hand'
CGContextSetAlpha(myContext, 0.5);
CGContextRotateCTM (myContext, [self toRadians:((theSeconds-15)*360 /60.0) - 10]);
CGContextTranslateCTM (myContext, -5, -[leaf size].height/10);
// draw the second hand and restore original context
CGContextDrawImage(myContext, CGRectMake(0, 0, [leaf size].width/5, [leaf size].height/5), [leaf CGImage]);
CGContextRestoreGState(myContext);
}
- (void)drawRect:(CGRect)rect
{
// draw clock in clock view
[self drawClock:CGPointMake(rect.size.width/2,rect.size.height/2) hours:self.hours minutes:self.minutes seconds:self.seconds];
// test code for centering hands
// [self drawClock:CGPointMake(rect.size.width/2,rect.size.height/2) hours:12 minutes:00 seconds:00];
}