【问题标题】:Rotate CALayers旋转 CALayers
【发布时间】:2013-02-20 13:32:01
【问题描述】:

我已经完成了使用 UIKit 创建旋转滚轮控制的教程。 http://www.raywenderlich.com/9864/how-to-create-a-rotating-wheel-control-with-uikit

在教程的“放置轮子”部分,轮子从圆圈的左侧开始顺时针绘制(参见带有红色标签的屏幕截图)。所以它从左侧站点的 0 开始。

但是。我想从值为 0 的圆圈的正确位置开始 - 现在是屏幕截图上的 4。不幸的是,我不知道如何实现这一目标。当然数字或图片的旋转应该和现在完全相反。所以现在 4 旋转 180 度的地方应该是正常的。

有人可以帮我吗?会很棒。

问候

当前drawWheel函数:

// 3 - Create the sectors
for (int i = 0; i < _numberOfSections; i++) {
    // 4 - Create image view
    UIImageView *im = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"segment.png"]];
    im.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
    im.layer.position = CGPointMake(_container.bounds.size.width/2.0-_container.frame.origin.x,
                                    _container.bounds.size.height/2.0-_container.frame.origin.y);
    im.transform = CGAffineTransformMakeRotation((angleSize * i) + M_PI);
    im.alpha = minAlphavalue;
    im.tag = i;
    if (i == 0) {
        im.alpha = maxAlphavalue;
    }
    // 5 - Set sector image
    UIImageView *sectorImage = [[UIImageView alloc] initWithFrame:CGRectMake(12, 100, 40, 40)];
    sectorImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"icon%i.png", i]];
    [im addSubview:sectorImage];
    // 6 - Add image view to container
    [_container addSubview:im];
}

【问题讨论】:

    标签: objective-c xcode calayer quartz-graphics


    【解决方案1】:

    未经测试,但你不能只添加M_PI吗?

    - (void) drawWheel {
        container = [[UIView alloc] initWithFrame:self.frame];
        CGFloat angleSize = 2*M_PI/numberOfSections;
        for (int i = 0; i < numberOfSections; i++) {
            UILabel *im = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
            im.backgroundColor = [UIColor redColor];
            im.text = [NSString stringWithFormat:@"%i", i];
            im.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
            im.layer.position = CGPointMake(container.bounds.size.width/2.0, 
                                            container.bounds.size.height/2.0); 
            // ===== Add M_PI Here! =====
            im.transform = CGAffineTransformMakeRotation((angleSize * i) + M_PI);
            // ==========================
            im.tag = i;
            [container addSubview:im];
        }
        container.userInteractionEnabled = NO;
        [self addSubview:container];
    }
    

    【讨论】:

    • 你好木马!感谢您的回答,现在开始如我所愿。只有图像没有正确旋转,嗯,这不是用CGAffineTransformMakeRotation完成的吗?
    • 是的,我的建议是将M_PI 添加到对CGAffineTransformMakeRotation() 的调用中。您能否确认您拥有我发布的确切代码(减去带有 ==== 的 cmets)?
    • 我的代码有点不同,因为我使用图像而不是标签。我的 drawWheel 函数现在看起来像本教程的“添加图形”部分。 (在页面底部)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多