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