我正在将转换应用于UIImageView的层,这是一个问题。使用下面的代码,我试图旋转这根针,但是我正在绘制重复的副本。我已经检查了没有其他地方添加针头图像的地方,并评论了 addSubview 没有一个人出现,所以它肯定是在绘制两次。

左侧的针头是应在图像视图开始的位置, -20. 。右侧的针头在旋转并显示正确值的方面 needleValue. 。我做错了什么以获得重复的抽奖?

alt text

- (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];
}
有帮助吗?

解决方案

尝试在线路上进行断点 [self addSubview:...] 看看它是否被称为两次。这 _needle 可以将变量设置为其他地方吗?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top