繁体   English   中英

UIPicker的替代品

[英]Alternative for UIPicker

老虎机应用程序还有其他方法吗? 这是因为UIPICKER方法没有较慢的动画效果。

这可以使用CALayer动画来完成。

您的主要slotMachineView图层的类将需要为CATransformLayer以允许子图层的3D转换。

假设您有10个正方形图像,它们代表卷轴上的符号。 为您的每个图像创建一个CALayer ,其CALayer属性是您的图像之一。 然后,您需要对每个图层应用2个转换:

  • 首先,您需要围绕其X轴旋转每个图层(2 * PI)/ 10
  • 然后沿Z轴平移一些距离(您需要计算该距离)。

现在,将这些图层添加到视图的图层。 现在,您应该看到围绕X轴的图像的获取“圆柱体”。

要旋转圆柱体,您需要调整第一个变换-使用CAAnimation或使用计时器,并以计时器触发的偏移量调整X轴旋转。

我将留给您找出完整的实现细节-但这里有一些代码可以加载并创建初始的“卷轴”

    int imageCount = 16;

    for (int i = 0; i < imageCount; i++) {
        // Load image from the bundle

        NSString * fileName = [NSString stringWithFormat: @"Image-%d", i];
        NSString * filePath = [[NSBundle mainBundle] pathForResource: fileName ofType: @"jpeg"];

        UIImage * image = [UIImage imageWithContentsOfFile: filePath];

        // Create a layer with the image as content - My image size was 60x60

        CALayer * imageLayer = [CALayer layer];
        imageLayer.bounds = (CGRect){CGPointZero, {60.0, 60.0}};
        imageLayer.contents = (id)image.CGImage;
        imageLayer.position = (CGPoint){CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)};

        // Set the initial image transform - I've hardcoded the translate along the
        // z-axis to 150, but this will vary depending on the number of images
        // and their size - you'll need to do some maths to work it out... sines or 
        // cosines or somesuch
        CGFloat angle = (2.0 * M_PI) / imageCount * i;
        CATransform3D transform = CATransform3DMakeRotation(angle, 1.0, 0.0, 0.0);
        transform = CATransform3DTranslate(transform, 0.0, 0.0, 150.0);  
        imageLayer.transform = transform;

        [self.layers addObject: imageLayer];

        [self.view.layer addSublayer: imageLayer];
    }
}

要旋转它,您需要做的就是更改变换的旋转部分。 为了获得额外的信誉,您可以在图层从中心移开时为其增加阴影。

您可以为每个数字/符号使用UIImageView ,并对要显示的图像的运动进行动画处理。 使用UIPicker只能使用字母和数字。 使用UIImageView您可以添加其他典型的老虎机视觉效果,例如樱桃等。

我们在做功课吗?

并排使用垂直UIScrollViews,启用分页,视图使用UIImageViews。

默认情况下,它看起来很平坦-但在功能上是等效的。

您需要一些Core Animation / Core Graphics使其看起来更像UIPickerView。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM