OGeek|极客世界-中国程序员成长平台

标题: ios - 为什么 UIImageView 不旋转? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:20
标题: ios - 为什么 UIImageView 不旋转?

为什么 UIImageView 不旋转?

我需要围绕中心框架渲染片段,但它没有旋转。

- (id)initWithFrameCGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _baseImage = [UIImage imageNamed"setAlarmSegment.png"];   
        _segmentCircleArray = [[NSMutableArray alloc] initWithCapacity:16];    
        for (int i=0; i<16; i++) {
            UIImageView *imageViewSegment = [[UIImageView alloc] initWithImage:_baseImage];
            [imageViewSegment setFrame:self.frame];
            imageViewSegment.transform = CGAffineTransformMakeRotation((M_PI*2/16.0f)*i);
            [_segmentCircleArray addObject:imageViewSegment];
        }
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];     
    }
    return self;
}


- (void)drawRectCGRect)rect
{
    for (UIImageView *segment in _segmentCircleArray) {
        [segment drawRect: rect];
    }
}

谢谢。



Best Answer-推荐答案


这不是您在 Cocoa/Cocoa Touch 中进行绘图的方式。您不要让 ImageView 在 View 层次结构中 float ,然后向它们发送 drawRect 消息。除非它们是 View 层次结构的一部分,否则 ImageView 并不意味着绘制。

您应该将 ImageView 添加为主视图的 subview ,然后系统会为您绘制它们。

一般来说,如果可能的话,您希望避免在 Cocoa/Cocoa Touch 中使用 drawRect。它最终是一种非常缓慢的绘制方式。

如果您决定使用 drawRect,则需要构建图像数组,而不是 ImageView 。然后,您将使用 drawAtPoint 或 drawInRect 依次绘制每一个。您可能需要保存图形上下文,然后在绘制每个图像之前对当前上下文应用旋转,然后恢复图形上下文

关于ios - 为什么 UIImageView 不旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18647176/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4