2010-11-18 69 views
2

我在將圖層應用到UIImageView的圖層時出現問題。使用下面的代碼,我試圖旋轉這個針,但我得到了重複的繪製。我檢查了沒有其他地方添加針圖像,並且註釋掉addSubview使得兩個都沒有出現,所以它肯定會畫兩次。使用UIImageView和CGAffinetransform繪製重複的圖層

左邊的針位於圖像視圖應該在-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]; 
} 

回答

2

儘量讓斷點在該行​​,看它是否得到了叫了兩聲。 _needle變量可能被設置爲零其他地方?

+0

我覺得自己像一個白癡沒有嘗試這個更早......我只是使用代碼檢查。 – coneybeare 2010-11-18 13:30:32