float scaleFactor = 0.5;
self.bubble.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
[self.bubble setNeedsDisplay];
[UIView animateWithDuration:2.0 animations:^{
self.frame = _rectToMoveTo;
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.0f animations:^{
self.bubble.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
}];
上面的代码执行的动画大部分是正确的。如您所见,self.bubble
缩小到 ½,然后动画恢复正常。问题在于 self.bubble 有一个用于绘制圆的 drawRect
方法。问题是这个圆从一开始就缩小到 ¼!当第二个动画运行时,self.bubble
的 subview 会正常缩放,但圆圈只会缩放到 ½。我试过使用 setNeedsDisplay
重绘圆圈,但它只会在动画完成后才起作用,所以效果不佳。你如何解决这个问题?
编辑:这里是 _bubble drawRect 方法。
CGContextRef c = UIGraphicsGetCurrentContext();
if (!_colour) _colour = [UIColor darkGrayColor];
[_colour setFill];
[[UIColor clearColor] setStroke];
CGContextAddEllipseInRect(c, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
CGContextFillPath(c);
[self addSubview:_title];
[self addSubview:_icon];
最佳答案
self.frame
在父 View 的坐标系中,因此会受到您不希望的变换的影响。
在 drawRect:
中,您应该只使用 self.bounds
,而不是 self.frame
。
此外,您不应该在 drawRect:
中添加 subview 。
关于iOS 在 drawRect 内容上使用 CGAffineTransformMakeScale 缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21060531/