如何顺时针和逆时针旋转圆圈?
问题描述:
基本上这是我的代码,它绘制了一个有4个不同颜色和相等大小的象限的圆。我想要做的是将这个圆顺时针和逆时针旋转,即顺时针旋转,当我触摸圆并沿顺时针方向拖动它,反之亦然。 我已经通过核心图形做出了圆圈。还有一件事是圆圈必须以高速旋转,并逐渐减速直到停止。在这方面的任何帮助将不胜感激。 请尽快回覆。如何顺时针和逆时针旋转圆圈?
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
// CGRect mainSquare = self.bounds/2;
CGRect mainSquare = CGRectMake(X_SPACE, Y_SPACE, 220, 200);
CGContextSaveGState(context);
// Create circle path and clip
CGContextAddEllipseInRect(context, mainSquare);
CGContextClip(context);
// Define rects
NSLog(@"helll %f",mainSquare.size.width);
CGRect topLeft = CGRectMake(mainSquare.origin.x, mainSquare.origin.y, (mainSquare.size.width/2) , (mainSquare.size.height/2));
CGRect topRight = CGRectMake((mainSquare.size.width/2) + X_SPACE, mainSquare.origin.y, mainSquare.size.width/2, (mainSquare.size.height/2));
CGRect bottomLeft = CGRectMake(mainSquare.origin.x, (mainSquare.size.height/2) + Y_SPACE, mainSquare.size.width/2, (mainSquare.size.height/2));
CGRect bottomRight = CGRectMake((mainSquare.size.width/2) + X_SPACE, (mainSquare.size.height/2) + Y_SPACE, mainSquare.size.width/2, mainSquare.size.height/2);
// Define colors
CGColorRef topLeftColor = [[UIColor redColor] CGColor];
CGColorRef topRightColor = [[UIColor blueColor] CGColor];
CGColorRef bottomLeftColor = [[UIColor greenColor] CGColor];
CGColorRef bottomRightColor = [[UIColor yellowColor] CGColor];
// Fill rects with colors
CGContextSetFillColorWithColor(context, topLeftColor);
CGContextFillRect(context, topLeft);
CGContextSetFillColorWithColor(context, topRightColor);
CGContextFillRect(context, topRight);
CGContextSetFillColorWithColor(context, bottomLeftColor);
CGContextFillRect(context, bottomLeft);
CGContextSetFillColorWithColor(context, bottomRightColor);
CGContextFillRect(context, bottomRight);
CGContextRestoreGState(context);
}
答
您可以使用变换&动画:
[UIView animateWithDuration:0.5
animations:^{
self.transform = CGAffineTransformRotate(self.transform, 30);
}
completion:^(BOOL completed){
NSLog(@"Completed");
}];
只记得帧无效后转换。我认为你可以设置一些条件来根据年度要求相应地改变持续时间。任何进一步的问题只是在这里发布。
嗯,至少你在“回复尽快”之前说“请”,所以没有粗鲁的行为从我这里投降。 ;) – John 2010-06-24 22:05:08
嗯,如果它是一个圆圈,你不会注意到一个旋转的权利? – 2010-06-24 22:05:52
迈克尔,是的,除了里面的彩色象限。 – John 2010-06-24 22:08:31