我正在尝试在iPad上实现一个由UIViews组成的3D旋转木马,一种类似于在here上显示的效果。
我在SO上经历了许多类似的问题,但没有找到任何静态的答案,或者根本没有答案。
我试图通过修改coverflow动画来达到这种效果,但它就是不能提供那种圆滑的效果。
有没有人实现了这一点?(通过quartz和openGL都公开征求建议)
发布于 2011-03-11 03:13:36
没有必要潜入石英或OpenGL,假设你不介意放弃模糊。你链接到的页面的视角是错误的(这就是为什么背景中的图像看起来比前景中的图像移动得更快),所以数学上可能会有一点模糊。
在底部有完整的示例代码。我所做的是使用正弦和余弦来移动一些视图。它背后的基本理论是,半径为r的圆在原点上的外部角度a的点是(a*sin(r),a*cos(r))。这是一个简单的从极坐标到笛卡尔坐标的转换,从大多数国家教给青少年的三角应该很清楚;考虑一个斜边为a的直角三角形-其他两个边的长度是多少?
然后,您可以做的是减小y部分的半径,将圆转换为椭圆。椭圆看起来有点像你从一个角度看的圆。这忽略了透视的可能性,但要顺其自然。
然后我通过使大小与y坐标成比例来伪造透视图。我正在调整alpha,就像你链接到的网站一样,希望这对你的应用程序来说足够好。
我通过调整我想要操纵的UIViews的仿射变换来影响位置和比例。我直接在UIView上设置了alpha。我还调整了视图层上的zPosition (这就是导入QuartzCore的原因)。zPosition类似于CSS位置;它不影响缩放,只影响绘制顺序。因此,通过将它设置为等于我计算的比例,它只是说“在较小的东西上绘制较大的东西”,给我们正确的绘制顺序。
手指跟踪通过触摸by /touchesMoved/touchesEnded循环一次跟随一个UITouch完成。如果没有手指被跟踪,并且某些触摸开始,则其中一个将成为被跟踪的手指。如果它移动了,那么旋转传送带就会旋转。
为了创建惯性,我有一个小方法,连接到计时器上,跟踪当前角度与一个滴答之前的角度。这种差异就像速度一样,同时按比例向下缩小以产生惯性。
计时器在手指向上时启动,因为这是旋转木马应该开始自己意愿旋转的时候。如果旋转木马停止或放置了新的手指,它就会停止。
让你自己来填,我的代码是:
#import <QuartzCore/QuartzCore.h>
@implementation testCarouselViewController
- (void)setCarouselAngle:(float)angle
{
// we want to step around the outside of a circle in
// linear steps; work out the distance from one step
// to the next
float angleToAdd = 360.0f / [carouselViews count];
// apply positions to all carousel views
for(UIView *view in carouselViews)
{
float angleInRadians = angle * M_PI / 180.0f;
// get a location based on the angle
float xPosition = (self.view.bounds.size.width * 0.5f) + 100.0f * sinf(angleInRadians);
float yPosition = (self.view.bounds.size.height * 0.5f) + 30.0f * cosf(angleInRadians);
// get a scale too; effectively we have:
//
// 0.75f the minimum scale
// 0.25f the amount by which the scale varies over half a circle
//
// so this will give scales between 0.75 and 1.25. Adjust to suit!
float scale = 0.75f + 0.25f * (cosf(angleInRadians) + 1.0);
// apply location and scale
view.transform = CGAffineTransformScale(CGAffineTransformMakeTranslation(xPosition, yPosition), scale, scale);
// tweak alpha using the same system as applied for scale, this time
// with 0.3 the minimum and a semicircle range of 0.5
view.alpha = 0.3f + 0.5f * (cosf(angleInRadians) + 1.0);
// setting the z position on the layer has the effect of setting the
// draw order, without having to reorder our list of subviews
view.layer.zPosition = scale;
// work out what the next angle is going to be
angle += angleToAdd;
}
}
- (void)animateAngle
{
// work out the difference between the current angle and
// the last one, and add that again but made a bit smaller.
// This gives us inertial scrolling.
float angleNow = currentAngle;
currentAngle += (currentAngle - lastAngle) * 0.97f;
lastAngle = angleNow;
// push the new angle into the carousel
[self setCarouselAngle:currentAngle];
// if the last angle and the current one are now
// really similar then cancel the animation timer
if(fabsf(lastAngle - currentAngle) < 0.001)
{
[animationTimer invalidate];
animationTimer = nil;
}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
// create views that are an 80x80 rect, centred on (0, 0)
CGRect frameForViews = CGRectMake(-40, -40, 80, 80);
// create six views, each with a different colour.
carouselViews = [[NSMutableArray alloc] initWithCapacity:6];
int c = 6;
while(c--)
{
UIView *view = [[UIView alloc] initWithFrame:frameForViews];
// We don't really care what the colours are as long as they're different,
// so just do anything
view.backgroundColor = [UIColor colorWithRed:(c&4) ? 1.0 : 0.0 green:(c&2) ? 1.0 : 0.0 blue:(c&1) ? 1.0 : 0.0 alpha:1.0];
// make the view visible, also add it to our array of carousel views
[carouselViews addObject:view];
[self.view addSubview:view];
}
currentAngle = lastAngle = 0.0f;
[self setCarouselAngle:currentAngle];
/*
Note: I've omitted viewDidUnload for brevity; remember to implement one and
clean up after all the objects created here
*/
}
- (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];
// ... and cancel any animation that may be ongoing
[animationTimer invalidate];
animationTimer = nil;
lastAngle = currentAngle;
}
}
- (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.view];
CGPoint locationThen = [trackingTouch previousLocationInView:self.view];
lastAngle = currentAngle;
currentAngle += (locationNow.x - locationThen.x) * 180.0f / self.view.bounds.size.width;
// the 180.0f / self.view.bounds.size.width just says "let a full width of my view
// be a 180 degree rotation"
// 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.02 target:self selector:@selector(animateAngle) userInfo:nil repeats:YES];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
// treat cancelled touches exactly like ones that end naturally
[self touchesEnded:touches withEvent:event];
}
@end所以相关的成员变量是一个可变数组,'carouselViews',一个计时器,'animationTimer',两个浮点数,'currentAngle‘和'lastAngle',以及一个UITouch,'trackingTouch’。显然,你可能想要使用不只是彩色方块的视图,你可能想要调整我为了定位而凭空拉出的数字。否则,它应该能正常工作。
编辑:我应该说,我使用Xcode中的iPhone“基于视图的应用程序”模板编写并测试了这段代码。创建该模板,将我的内容转储到创建的视图控制器中,并添加必要的成员变量进行测试。然而,我意识到触摸跟踪假设180度是视图的全宽,但setCarouselAngle:方法强制旋转木马始终为280点(即xPosition上的100乘以2,再加上视图的宽度)。因此,如果你在iPad上运行,手指跟踪将显得太慢。显然,解决方案不是假设视图宽度为180度,而是留下来作为练习!
发布于 2011-05-23 21:10:52
一个很棒的不同类型coverflow的开源代码,包括循环的https://github.com/demosthenese/iCarousel
编辑:
https://stackoverflow.com/questions/5243614
复制相似问题