2

UIImageView のレイヤーに変換を適用する際に問題が発生しています。以下のコードを使用して、この針を回転させようとしていますが、重複したコピーが描画されています。他に針の画像が追加されている箇所がないことを確認し、コメントアウトするaddSubviewとどちらも表示されないので確実に二重描画です。

左側の針は、画像ビューを開始する位置にあり-20ます。右の針は回転し、 の正しい値を表示する働きがありますneedleValue。ドローを重複して取得するために何が間違っていますか?

代替テキスト

- (UIImageView *)needle {
 if (_needle == nil) {
  UIImage *image = [UIImage imageNamed:@"needle.png"];
  self.needle = [[[UIImageView alloc] initWithImage:image] autorelease];
  [_needle sizeToFit];
  [_needle setFrame:CGRectMake(floor((self.width - _needle.width)/2),
          self.height - _needle.height + 68, // this - offset comes from the lowering of the numbers on the meter
          _needle.width, _needle.height)];
  _needle.contentMode = UIViewContentModeCenter;
  _needle.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  _needle.autoresizesSubviews = YES;

  // CALayer's transform property is a CATransform3D.
  // rotate around a vector (x, y, z) = (0, 0, 1) where positive Z points 
  // out of the device's screen.
  _needle.layer.anchorPoint = CGPointMake(0.05,1);

  [self addSubview:_needle];
 }
 return _needle;
}



- (void)updateNeedle {
 [UIView beginAnimations:nil context:NULL]; // arguments are optional
 [UIView setAnimationDuration:VU_METER_FREQUENCY];
 CGFloat radAngle = [self radianAngleForValue:needleValue];
 self.needle.transform = CGAffineTransformMakeRotation(radAngle);
 [UIView commitAnimations];
}
4

1 に答える 1

2

その行にブレークポイントを作成して、[self addSubview:...]2回呼び出されたかどうかを確認してください。変数は_needleどこか他の場所でnilに設定されている可能性がありますか?

于 2010-11-18T05:29:23.080 に答える