繁体   English   中英

iOS,优美的方向变化

[英]iOS, Graceful Orientation Change

我在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];  
}

当视图确实发生变化时,其非常“粗糙”且根本不是非常优美。 我正在使用模拟器,但是我假设如果我要在设备上运行,它将是相同的。 我将如何使过渡更加平滑? 我会发布到用户体验页面,但我希望以编程方式执行此操作

除了学习之外,总体目的是从代码中获得与方向无关的图形(没有自动布局)。

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

- (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;
    }
}

对于精美的UX,请将帧更新包装在动画块中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM