2013-02-20 60 views
2

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

部分中“摆出轮”的教程,车轮被绘制顺时针开始圆的左侧部位(见的红色标签的截图)。所以它从左边的网站开始。

但是,我想从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

回答

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]; 
} 
+0

Hello trojanfoe!感谢您的回答,现在就按照我的意愿开始。只有图像没有正确旋转,hmm不是用CGAffineTransformMakeRotation完成的吗? – ihkawiss 2013-02-20 13:45:03

+0

是的,我的建议是将'M_PI'加入到'CGAffineTransformMakeRotation()'的调用中。你能否确认你有我发布的确切代码(减去'===='中的评论)? – trojanfoe 2013-02-20 13:48:19

+0

我的代码有点不同,因为我使用图像而不是标签。 我的drawWheel函数现在看起来像本教程的“添加图形”部分。 (在页面底部) – ihkawiss 2013-02-20 14:00:37