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:...]看看它是否被调用了两次。该_needle变量可能在其他地方设置为零?

于 2010-11-18T05:29:23.080 回答