Iphone 如何计算从三维旋转木马设置动画的视图的位置?
我为iPhone开发了一个psuedo 3D旋转木马,它看起来非常像下图: 我基本上创建一个UIView数组,然后注册触摸事件:Iphone 如何计算从三维旋转木马设置动画的视图的位置?,iphone,objective-c,ios,carousel,cgaffinetransform,Iphone,Objective C,Ios,Carousel,Cgaffinetransform,我为iPhone开发了一个psuedo 3D旋转木马,它看起来非常像下图: 我基本上创建一个UIView数组,然后注册触摸事件: -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // if we're not already tracking a touch then... if(!trackingTouch) { // ... track any of the new
-(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坐标
以上各项都很好,但我很难理解完成以下内容所需的数学:
- (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];
}
}
单击主焦点以外的视图,即不在前面的视图,例如图像中的紫色视图
将紫色视图旋转到焦点位置,即向前
旋转完成后加载新的视图控制器
我知道我必须计算当前位置的视图与计划目的地之间的角度来进行旋转,但我一生都无法计算出来
为之前的混乱道歉-我已经关注这个问题很长时间了,我开始失去这个情节
谢谢终于…我设法破解了它 我表现得非常愚蠢,没有逻辑思维。我基本上重新运行了一个NSTimer,调用了一个方法,该方法在psuedo代码中对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];
瞧
非常简单——我必须学会有时退一步,头脑清醒地回到问题上来
谢谢大家您需要提供某种图像来描述这一点,因为我无法在脑海中对您的案例形成良好的印象。另外,CGAffineTransform如何实现这一点,因为它只是一个二维变换?看起来您需要在这里使用CATTransform3D。感谢Brad的回复。我在最初的帖子中有误导性,因为这不仅仅是3D效果的印象。希望这张图片能对你有所帮助