1

Here is my function for which I draw a metronome image within my iOS program. When I load it, the metronome image is loaded, but "flies" down from the top left of the screen to the desired position. This is not the behavior I wanted - I simply want the image to appear at the desired position. Could someone enlighten me why this happens, and how to fix it? Many thanks in advance.

Pier.

- (void) drawMetronomeForBeat: (int) beatNumber withNumberOfBeats:(int) noBeats
{

// remove any previous instances first
if (metronomeImageView) 
{
    [metronomeImageView removeFromSuperview];
}

NSString * imageName = @"metronome";
NSString * noBeatsStr = [NSString stringWithFormat:@"%d", noBeats];
NSString * beatNumberStr = [NSString stringWithFormat:@"%d", beatNumber];
imageName = [imageName stringByAppendingString:noBeatsStr]; 
imageName = [imageName stringByAppendingString:@"b"]; 
imageName = [imageName stringByAppendingString:beatNumberStr]; 

UIImage* image = [UIImage imageNamed: imageName];
UIImageView * symbolImageView = [[UIImageView alloc] initWithImage:image]; 

[symbolImageView setFrame:CGRectMake(410.0f, 215.0f, symbolImageView.frame.size.width, symbolImageView.frame.size.height)];
metronomeImageView = symbolImageView; 
[self.view addSubview:symbolImageView];
}
4

2 回答 2

0

Replace this line

[symbolImageView setFrame:CGRectMake(410.0f, 215.0f, symbolImageView.frame.size.width, symbolImageView.frame.size.height)]; 

with

[symbolImageView setFrame:CGRectMake(410.0f, 215.0f, 22, 22)];  //Change 22, 22 according to size that you need.

The image view will become larger or smaller according to the size of "real image" you are displaying. So it will move some where so that imageview will stay center aligned.

EDIT:

I think you are setting different frame for symbolImageView.

Try this

symbolImageView.frame = metronomeImageView.frame;
于 2012-07-24T08:31:57.257 回答
0

我正在使用控制器来显示图像。我重组了我的代码,以便图像总是在视图代码的 drawRect 中“绘制”,就像这样..

- (void)drawRect:(CGRect)rect
{  

if (metronomeImage)
{
    [metronomeImage drawInRect:CGRectMake(410.0f, 215.0f, metronomeImage.size.width, metronomeImage.size.height)];
}
}

这没有问题 - 请注意,我只使用了 drawInRect 和 UIImage 而不是 setFrame 和 UIImageView。我想我会遵循约定,任何图形都应该在视图代码中完成,而不是在控制器中完成。

于 2012-07-25T02:40:45.350 回答