2

why UIImageView not rotating around?

I need to render segments around center frame, but it's not rotating.

- (id)initWithFrame:(CGRect)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)drawRect:(CGRect)rect
{
    for (UIImageView *segment in _segmentCircleArray) {
        [segment drawRect: rect];
    }
}

thanks.

setixela
  • 23
  • 3

1 Answers1

1

That is not how you do drawing in Cocoa/Cocoa Touch. You don't keep image views floating around not in a view hierarchy and then send them a drawRect message. Image views are not meant to draw unless they are part of a view hierarchy.

You should add your image views as subviews of your primary view, and then the system will take care of drawing them for you.

In general you want to avoid using drawRect in Cocoa/Cocoa Touch if at all possible. It ends up being a very slow way to draw.

If you're determined to use drawRect, you would need to build an array of images, not image views. Then you'd draw each one in turn using drawAtPoint or drawInRect. You'll probably need to save the graphics context, then apply rotation to the current context before drawing each image, then restore the graphics context

Duncan C
  • 128,072
  • 22
  • 173
  • 272