繁体   English   中英

如何顺时针和逆时针旋转一个圆?

[英]How to rotate a circle clockwise and anticlockwise?

基本上这是我的代码,它绘制了一个具有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");
                 }];

只记得转换后帧无效。 我认为您可以设置一些条件来根据自己的要求相应地更改持续时间。 如有其他疑问,请在此处发布。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM