Duplicate layers drawn with a UIImageView and CGAffinetransform
I am having an issue applying a transform to a UIImageView's layer. Using the code below, I am trying to rotate this needle, but I am getting duplicate copies drawn. I have checked that there is no other place where the needle image is added, and commenting out the addSubview
makes neither one show up, so it is definitely drawing twice.
The needle on the left is in the position that the image view should be started at, -20
. The needle on the right works in the aspect that it rotates and displays the correct value for the needleValue
. What am I doing wrong to get duplicate draws?
- (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];
}
Try to make breakpoint at the line [self addSubview:...]
and see if it got called twice. The _needle
variable might be set to nil somewhere else?
精彩评论