使用CATransform3DMakeRotation左右旋转UIView

时间:2011-12-04 03:05:43

标签: ipad uiview image-rotation catransform3d

在我的应用程序中,我正在使用CATransform3DMakeRotation旋转UIView在其上移动手指,尝试想象一个旋钮。 我的问题是一切都运转良好,我想要的动作但是当我触摸它时开始旋转它在中心旋转180度,我不明白为什么。

我告诉你我的代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
        {
        if (Tag ==subView.tag)

            {
                   CGPoint Location  = [[touches anyObject] locationInView:subView.superview];
                 int x = subView.center.x;
                 int y = subView.center.y;
                 float dx = Location.x - x;
                 float dy = Location.y - y;
                 double a = atan2f(-dx, dy);

                subView.layer.position  = subView.center;
                 subView.layer.transform = CATransform3DMakeRotation(a, 0, 0, 1);
        }}




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

if (Tag ==subView.tag) 

    {   
        [self touchesBegan:touches withEvent:event];
}}

编辑:这里我是如何解决问题的

CGPoint Location  = [withTheTouch locationInView:theViewToRotate.superview];
    NSLog(@"location x%f",Location.x);
    NSLog(@"location y%f",Location.y);
    int x = theViewToRotate.center.x;
    int y = theViewToRotate.center.y;
    NSLog(@" X %i",x);
    NSLog(@" Y %i",y);
    float dy;
    float dx;

    if (Location.y < theViewToRotate.center.y) 
    {
        dx = x- Location.x;
        dy = y- Location.y;

        NSLog(@"dx %f",dx);
        NSLog(@"dy %f",dy);
        NSLog(@"sopra il View.Center");

    } else {

        dx = Location.x - x;
        dy = Location.y - y;
        NSLog(@"sotto il View.Center");

    }
    float ang = atan2(-dx, dy);
    NSLog(@"anchorPoint %@",NSStringFromCGPoint(theViewToRotate.layer.anchorPoint));
    NSLog(@"Position %@",NSStringFromCGPoint(theViewToRotate.layer.position));
    NSLog(@"angle %f",ang);
    //theViewToRotate.layer.position  = theViewToRotate.center;


if(ang)

     {
        theViewToRotate.layer.transform = CATransform3DMakeRotation(ang, 0, 0, 1);  //ruoto sul vettore Z

    } 
    else 
    {
        //theViewToRotate.frame = CGRectMake(theViewToRotate.frame.origin.x, theViewToRotate.frame.origin.y , theViewToRotate.frame.size.width, theViewToRotate.frame.size.height );
    }

问题出现在我触摸视图的位置。

现在我需要了解如何锁定两个角度之间的旋转。我没有360°旋转。

1 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为视图本身具有现有轮换。你需要获得当前的旋转并从那里开始旋转。

你在做什么总是从这里开始CATransform3DMakeRotation(a,0,0,1);

具有自己的预定义旋转。因此,当你触摸它时,180度移动。如果从当前视图的旋转开始旋转,则不会发生这种情况。

希望有所帮助