0

私のアプリでは、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 );
    }

問題は、私がビューに触れたところにありました。

ここで、2 つの角度の間で回転をロックする方法を理解する必要があります。360°回転は必要ありません。

4

1 に答える 1

0

こんにちは、これが起こった理由は、ビュー自体に既存の回転があるためです。現在の回転を取得し、そこから回転を開始する必要があります。

あなたがしていることは常にここから始まります CATransform3DMakeRotation(a, 0, 0, 1);

独自の事前定義された回転があります。したがって、あなたがそれに触れると180度シフトします。現在のビューの回転から回転を開始すると、これは発生しません。

それが役立つことを願っています

于 2012-09-04T09:50:50.717 に答える