- [UIView animateWithDuration:animations:]
块中和在一次每秒触发多次的捏合手势识别器动作中。基于SO上的其他答案,我尝试了三种方法,但都不适合:1. 在
layoutSubviews
中设置视图的层的角半径可使平移更加顺滑,但是在动画期间,该视图不会保持圆形;似乎cornerRadius不可动画化。
2. 在 drawRect:
中绘制圆可获得始终为圆形的视图,但如果圆变得太大,则在捏合手势中调整大小会变得不流畅,因为设备花费太多时间重新绘制圆。
3. 添加CAShapeLayer并在layoutSublayersOfLayer
中设置其路径属性,由于路径不是隐式可动画化的,因此在UIView动画中不会产生动画效果。有没有方法可以创建一个始终为圆形且可平滑调整大小的视图?是否有其他类型的图层可用于利用硬件加速?
更新: 评论者要求我详细说明当我说我想在
- [UIView animateWithDuration:animations:]
块中改变边界时我的意思。在我的代码中,我有一个包含圆形视图的视图。圆形视图(使用cornerRadius的版本)覆盖了 - [setBounds:]
以设置角半径。-(void)setBounds:(CGRect)bounds
{
self.layer.cornerRadius = fminf(bounds.size.width, bounds.size.height) / 2.0;
[super setBounds:bounds];
}
圆形视图的边界在-[layoutSubviews]
中设置:
-(void)layoutSubviews
{
// some other layout is performed and circleRadius and circleCenter are
// calculated based on the properties and current size of the view.
self.circleView.bounds = CGRectMake(0, 0, circleRadius*2, circleRadius*2);
self.circleView.center = circleCenter;
}
在动画中,视图有时会被调整大小,就像这样:
[UIView animateWithDuration:0.33 animations:^(void) {
myView.frame = CGRectMake(x, y, w, h);
[myView setNeedsLayout];
[myView layoutIfNeeded];
}];
但是在这些动画期间,如果我使用具有cornerRadius的图层来绘制圆形视图,它会变成奇怪的形状。我无法将动画持续时间传递到layoutSubviews中,因此我需要在-[setBounds]
中添加正确的动画。