设置view缩放动画围绕view中的某一点开始

项目中要求子菜单的弹出动画为围绕点击的靠近点展开放大,而不是从中心展开,下面是动画效果:


弹出动画效果如图.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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容