gpt4 book ai didi

iOS,优雅的方向变化

转载 作者:行者123 更新时间:2023-11-29 02:21:37 25 4
gpt4 key购买 nike

我在 viewDidLoaddidRotateFromInterfaceOrientation 调用这个函数

-(void) makeBox{
[self.view1 removeFromSuperview];

float viewWidth = CGRectGetWidth(self.view.frame);
float viewHeight = CGRectGetHeight(self.view.frame);
float startx = (viewWidth / 100) * 10;
float starty = (viewHeight /100) * 20;

float width = (viewWidth / 100) * 80;
float height = (viewHeight /100) * 60;

CGRect frame1 = CGRectMake(startx, starty, width, height);
self.view1 = [[UIView alloc] initWithFrame:frame1];
[self.view1 setBackgroundColor:[UIColor redColor]];

[self.view addSubview:self.view1];
}

当 View 确实发生变化时,它非常“粗略”并且一点也不优雅。我正在使用模拟器,但我认为如果我在设备上运行它会是一样的。 我将如何着手让这个过渡更顺畅?我会发布到用户体验页面,但我希望以编程方式执行此操作

除了学习之外,整体目的是通过代码实现与方向无关的图形(没有自动布局)。

最佳答案

如果您唯一要更改的是 View ,则无需删除 View 并创建具有所需框架的新 View 。只需就地修改 View 框架:

- (void)makeBox {
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
CGFloat viewHeight = CGRectGetHeight(self.view.frame);
CGFloat startx = (viewWidth / 100) * 10;
CGFloat starty = (viewHeight /100) * 20;

CGFloat width = (viewWidth / 100) * 80;
CGFloat height = (viewHeight /100) * 60;

CGRect view1Frame = CGRectMake(startx, starty, width, height);

if (!self.view1) {
// The view doesn't exists yet, we create it
self.view1 = [[UIView alloc] initWithFrame:view1Frame];
[self.view1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:self.view1];
}
else {
// Just update the frame
self.view1.frame = view1Frame;
}
}

为了更好的用户体验,将帧更新包装在动画 block 中:

// ... snip ...
else {
[UIView animateWithDuration:0.3 animations:^() {
self.view1.frame = view1Frame;
}];
}

关于iOS,优雅的方向变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28072457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com