【问题标题】:Duplicate layers drawn with a UIImageView and CGAffinetransform使用 UIImageView 和 CGAffinetransform 绘制的重复图层
【发布时间】:2010-11-18 01:04:23
【问题描述】:

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

【问题讨论】:

    标签: iphone animation uiview transform cgaffinetransform


    【解决方案1】:

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

    【讨论】:

    • 我觉得自己像个白痴,没有早点尝试过……我只是使用了代码检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多