使用UIImageView和CGAffinetransform绘制的重复图层

时间:2010-11-18 01:04:23

标签: iphone animation uiview transform cgaffinetransform

我在将变换应用到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];
}

1 个答案:

答案 0 :(得分:2)

尝试在第[self addSubview:...]行创建断点,看看它是否被调用了两次。 _needle变量可能在其他地方设置为nil?