使用 CATransform3DMakeRotation 左右旋转 UIView

发布于 2024-12-19 13:09:36 字数 2296 浏览 1 评论 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 );
    }

问题出在我在视图中触摸的点上。

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

In my app I'm using CATransform3DMakeRotation to rotate a UIView moving a finger on it, try to imagine a Knob.
My problem is that everything is working good, the moves as I want but when I touch on it ot start rotating it rotates by 180 degrees on the center, I can't understand why.

I show you my code:

-(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];
}}

edit : here how i solve the problem

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 );
    }

the problem was on the point where I touch in the view.

Now I need to understand how to lock the rotation between 2 angles. I don't ned 360° rotation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩动你心 2024-12-26 13:09:36

您好,发生这种情况的原因是视图本身已有旋转。您需要获取当前的旋转并从那里开始旋转。

你所做的总是从这里开始 CATransform3DMakeRotation(a, 0, 0, 1);

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

希望有帮助

hi the reason this was happening is because the view itself has an existing rotation. you need to get the current rotation and start rotating from there.

what youre doing is always starting from here CATransform3DMakeRotation(a, 0, 0, 1);

which has its own pre defined rotation. hence the 180 degree shift when you touch it. this wont happen if you start the rotation from the rotation of the current view.

hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文