旋转CALayers

时间:2013-02-20 13:32:02

标签: objective-c xcode calayer quartz-graphics

我已经完成了以下教程,用UIKit创建了一个旋转的Wheel Control。 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];
}

Screenshot of the wheel as it's now

1 个答案:

答案 0 :(得分:0)

未经测试,但您无法添加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];
}
相关问题