项目中要求子菜单的弹出动画为围绕点击的靠近点展开放大,而不是从中心展开,下面是动画效果:
![](https://img.haomeiwen.com/i3278444/05094713b5cea057.gif)
在iOS中,anchorPoint点的值是用一种相对bounds的比例值来确定的,在白纸的左上角、右下角,anchorPoint分为为(0,0), (1, 1)。类似地,可以得出在白纸的中心点、左下角和右上角的anchorPoint为(0.5,0.5), (0,1), (1,0)。
在实际情况中,可能还有这样一种需求,我需要修改anchorPoint而不想移动layer,在修改anchorPoint后再重新设置一遍frame就可以达到目的,这时position就会自动进行相应的改变。
代码:
- (void) setAnchorPoint:(CGPoint)anchorpoint forView:(UIView *)view{
CGRect oldFrame = view.frame;
view.layer.anchorPoint = anchorpoint;
view.frame = oldFrame;
}
我的弹出动画效果实现代码如下:
- (void)changeScaleAnimationToView:(UIView *)view {
view.alpha = 0;
CABasicAnimation *animationScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animationScale.duration = 0.2;
animationScale.repeatCount = 1;
animationScale.autoreverses = NO;
animationScale.fromValue = [NSNumber numberWithFloat:0.0]; // 开始时的倍率
animationScale.toValue = [NSNumber numberWithFloat:1.0]; // 结束时的倍率
animationScale.removedOnCompletion = YES;
CGRect frame = view.frame;
view.layer.anchorPoint = CGPointMake(1.0, 0.3);
view.frame = frame;
view.alpha = 1.0;
[view.layer addAnimation:animationScale forKey:@"scale-layer"];
}
关于anchorPoint的详细解释请参考如下链接。
参考链接:
彻底理解position与anchorPoint
网友评论