使用 UIImageView 和 CGAffinetransform 绘制的重复图层
我在将转换应用于 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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在
[self addSubview:...]
行下断点,看看它是否被调用了两次。_needle
变量可能在其他地方设置为 nil ?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?