如何计算从3D旋转木马动画的视图位置?

时间:2011-12-13 15:33:42

标签: iphone objective-c ios carousel cgaffinetransform

我为iPhone开发了一个 psuedo 3D旋转木马,看起来非常像下图: enter image description here

我基本上创建了一个UIViews数组,然后注册触摸事件:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // if we're not already tracking a touch then...
    if(!trackingTouch)
    {
        // ... track any of the new touches, we don't care which ...
        trackingTouch = [touches anyObject];
    }
}

当运动仍在注册时,我会执行以下操作:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // if our touch moved then...
    if([touches containsObject:trackingTouch])
    {
        // use the movement of the touch to decide
        // how much to rotate the carousel
        CGPoint locationNow = [trackingTouch locationInView:self];
        CGPoint locationThen = [trackingTouch previousLocationInView:self];

        lastAngle = currentAngle;
        currentAngle += (locationNow.x - locationThen.x) * 180.0f / self.bounds.size.width;

        // and update the view positions
        [self setCarouselAngle:currentAngle];
    }
}

接着结束时我会打电话:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    // if our touch ended then...
    if([touches containsObject:trackingTouch])
    {
        // make sure we're no longer tracking it
        trackingTouch = nil;

        // and kick off the inertial animation
        animationTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(animateAngle) userInfo:nil repeats:YES];
    }
}

animageAngle方法基本上调用另一个方法,有效地将360度除以数组中的视图数,并使用余弦和正弦函数生成x和y坐标。

以上一切都很好,但我很难理解完成以下内容所需的数学:

  1. 点击主焦点外的视图,即不在前面,例如图像中的紫色视图
  2. 将紫色视图旋转到焦点,即前面
  3. 旋转完成后加载新的视图控制器
  4. 据我所知,我必须计算当前位置的视图与计划目的地之间的角度以进行旋转,但我不能为我的生活计算出来。

    对于之前的混乱道歉 - 我一直在关注这个问题,因为我开始失去这个阴谋。

    由于

1 个答案:

答案 0 :(得分:0)

终于......我设法破解了它。

我表现得非常愚蠢,并没有逻辑思考。我基本上重新运行了一个NSTimer调用一个方法,该方法在currentAngle属性上运行以下计算(在伪代码中):

CGFloat currentLocation;
CGFloat nextLocation;
CGFloat destinationLocation;

currentLocation = viewImInterestedIn.frame.origin.x;
nextLocation = currentLocation - 1; //or + 1 if the selected view is to the left of centre

destinationLocation = 115.0f //set this to a default value
currentAngle += (currentLocation - nextLocation ) * 180.0f / self.bounds.size.width;

// and update the view positions
[self setCarouselAngle:currentAngle];

Et瞧!

非常简单 - 我必须学会有时退后一步,回到头脑清醒的问题。

全部谢谢