iOS 点击button 弹出 View 怎么设置从下往上 弹出的动画

2025-03-10 13:11:45
推荐回答(1个)
回答1:

//这里关键的地方是把view的y坐标设到屏幕下方

/** objective-c代码*/
let aView = UIView.init();

let yOffset = UIScreen.main().bounds.height;
let yTargetOffset = 200; //你想要最终放置的y坐标

let originalRect = CGRect.init(x: 0, y: yOffset, width: 100, height: 200);
let targetRect = CGRect.init(x: 0, y: yTargetOffset, width: 100, height: 200);

//初始位置
aView.frame = originalRect;

//动画
UIView.animate(withDuration: 0.3) {
//赋值最终位置
aView.frame = targetRect;
}

/** oc代码*/
UIView *aView = [UIView new];

CGFloat yOffset = [UIScreen mainScreen].bounds.size.height; //初始y值
CGFloat yTargetOffset = 200; //目标y值

CGRect originalRect = CGRectMake(0, yOffset, 100, 200);
CGRect targetRect = CGRectMake(0, yTargetOffset, 100, 200);

//初始位置
aView.frame = originalRect;

//动画
[UIView animateWithDuration:.3 animations:^{
aView.frame = targetRect;
}];