項(xiàng)目中要求子菜單的彈出動(dòng)畫(huà)為圍繞點(diǎn)擊的靠近點(diǎn)展開(kāi)放大,而不是從中心展開(kāi),下面是動(dòng)畫(huà)效果:
彈出動(dòng)畫(huà)效果如圖.gif
在iOS中,anchorPoint點(diǎn)的值是用一種相對(duì)bounds的比例值來(lái)確定的牲尺,在白紙的左上角、右下角幌蚊,anchorPoint分為為(0,0), (1, 1)谤碳。類(lèi)似地,可以得出在白紙的中心點(diǎn)溢豆、左下角和右上角的anchorPoint為(0.5,0.5), (0,1), (1,0)蜒简。
在實(shí)際情況中,可能還有這樣一種需求漩仙,我需要修改anchorPoint而不想移動(dòng)layer搓茬,在修改anchorPoint后再重新設(shè)置一遍frame就可以達(dá)到目的,這時(shí)position就會(huì)自動(dòng)進(jìn)行相應(yīng)的改變队他。
代碼:
- (void) setAnchorPoint:(CGPoint)anchorpoint forView:(UIView *)view{
CGRect oldFrame = view.frame;
view.layer.anchorPoint = anchorpoint;
view.frame = oldFrame;
}
我的彈出動(dòng)畫(huà)效果實(shí)現(xiàn)代碼如下:
- (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]; // 開(kāi)始時(shí)的倍率
animationScale.toValue = [NSNumber numberWithFloat:1.0]; // 結(jié)束時(shí)的倍率
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"];
}
關(guān)于anchorPoint的詳細(xì)解釋請(qǐng)參考如下鏈接卷仑。
參考鏈接:
徹底理解position與anchorPoint