繁体   English   中英

自定义UIView如何处理屏幕旋转

[英]How custom UIView handle screen rotation

出于某种原因,我创建了一个自定义视图,覆盖了drawrect方法,以绘制一些字符串和img。 就像这样:

@implementation WaterMarkView

- (void)drawRect:(CGRect)rect {
    NSLog(@"drawRect");

    NSString* test = @"This is a test string";
    NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:22],
                           NSForegroundColorAttributeName:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0],
                           };

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
    CGSize strsize = [[[NSAttributedString alloc] initWithString:test attributes:attributes] size];

    CGRect screentBounds = [[UIScreen mainScreen] bounds];
    [test drawAtPoint:CGPointMake((screentBounds.size.width - strsize.width)/2,
                                  (screentBounds.size.height - strsize.height)/2) withAttributes: dict];
}


@end

将其添加到viewController

....
WaterMarkView* view = [[WaterMarkView alloc] init];
view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:view];
....

它工作正常。

但是,当屏幕旋转时,会清楚地看到字符串过渡不平滑而是难看。

当我添加一个UILable连字串时,屏幕旋转时它将平滑过渡

屏幕旋转时如何使自定义UIView过渡平滑?

只需将contentMode设置为UIViewContentModeRedraw ,默认模式UIViewContentModeScaleToFill将缩放内容。

暂无
暂无

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

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