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

问题描述 投票:0回答:1

出于某种原因,我做了一个自定义uiview,覆盖它的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];
....

它工作正常。

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

当我添加一个带字符串的UILabel时,它会在屏幕旋转时平滑过渡

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

ios objective-c uiview screen-rotation uianimation
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.