如何顺时针和逆时针旋转圆圈(通过核心图形绘制)?

时间:2010-06-25 20:10:25

标签: iphone

这是我的代码。我想顺时针和逆时针旋转我的圆圈。如果有人知道如何使用核心图形绘制圆圈,请指导我如何操作。

- (void)drawRect:(CGRect)rect { 

    CGContextRef context = UIGraphicsGetCurrentContext();
    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);

}


 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

     [super touchesBegan:touches withEvent:event];
     UITouch *touch = [[event allTouches] anyObject]; 
     swipeBackward = [touch locationInView: [touch view]]; 
 }

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

     [super touchesEnded:touches withEvent:event]; 
     UITouch *touch = [[event allTouches] anyObject]; 
     swipeForward = [touch locationInView: [touch view]]; 

     double dx = swipeBackward.x - swipeForward.x;
    // double dy = swipeBackward.y - swipeForward.y;     

     if( (swipeBackward.x < 160) && (swipeForward.x < 160) && (swipeBackward.y <= 151) && (swipeForward.y > 151) ) 
     {  
             NSLog(@"Rotate Anticlockwise");
             mainSquare.transform = CGAffineTransformMakeRotation(63.0*radianConst);

     }  

     else if( (swipeBackward.x < 160) && (swipeForward.x < 160) && (swipeBackward.y >= 151) && (swipeForward.y < 151) )
     {
             NSLog(@"Rotate Clockwise");

     }

     else if( (swipeBackward.x > 160) && (swipeForward.x > 160) && (swipeBackward.y <= 151) && (swipeForward.y > 151) ) 
     {          
            NSLog(@"Rotate Clockwise"); 

     }  

     else if( (swipeBackward.x > 160) && (swipeForward.x > 160) &&  (swipeBackward.y >= 151) && (swipeForward.y < 151) )
     {
             NSLog(@"Rotate Anticlockwise");             

     }

     else if( (swipeBackward.y > 151) && (swipeForward.y > 151) ) 
     { 
         if (dx > 0 ) 
         {
             NSLog(@"Rotate Clockwise");

         }  

         else 
         {
             NSLog(@"Rotate Anticlockwise");


         }

     }
     else
     { 
         if (dx > 0 ) 
         {
             NSLog(@"Rotate Anticlockwise");


         }  

         else 
         {
             NSLog(@"Rotate Clockwise");


         }

     }


}

0 个答案:

没有答案